🎨 improvement of comment-related code and database structure
This commit is contained in:
@@ -2,7 +2,9 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
|
||||
@@ -24,7 +26,34 @@ func NewDislikeCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Di
|
||||
}
|
||||
|
||||
func (l *DislikeCommentLogic) DislikeComment(req *types.CommentDisLikeRequest) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
uid, ok := l.ctx.Value("user_id").(string)
|
||||
if !ok {
|
||||
return nil, errors.New("user_id not found in context")
|
||||
}
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
commentLike := l.svcCtx.DB.ScaCommentLike
|
||||
resultInfo, err := tx.ScaCommentLike.Where(commentLike.TopicID.Eq(req.TopicId), commentLike.CommentID.Eq(req.CommentId), commentLike.UserID.Eq(uid)).Delete()
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
if resultInfo.RowsAffected == 0 {
|
||||
_ = tx.Rollback()
|
||||
return response.ErrorWithI18n(l.ctx, "comment.CancelLikeError"), nil
|
||||
}
|
||||
comment := l.svcCtx.DB.ScaCommentReply
|
||||
updates, err := tx.ScaCommentReply.Where(comment.TopicID.Eq(req.TopicId), comment.ID.Eq(req.CommentId)).Update(comment.Likes, comment.Likes.Sub(1))
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
if updates.RowsAffected == 0 {
|
||||
_ = tx.Rollback()
|
||||
return response.ErrorWithI18n(l.ctx, "comment.LikeError"), nil
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.Success(), nil
|
||||
}
|
||||
|
@@ -2,17 +2,28 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/chenmingyong0423/go-mongox/v2/builder/query"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gen/field"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mongodb/collection"
|
||||
)
|
||||
|
||||
type GetCommentListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewGetCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentListLogic {
|
||||
@@ -25,11 +36,128 @@ func NewGetCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
|
||||
func (l *GetCommentListLogic) GetCommentList(req *types.CommentListRequest) (resp *types.Response, err error) {
|
||||
// 获取用户ID
|
||||
// uid, ok := l.ctx.Value("user_id").(string)
|
||||
// if !ok {
|
||||
// return nil, errors.New("user_id not found in context")
|
||||
// }
|
||||
uid, ok := l.ctx.Value("user_id").(string)
|
||||
if !ok {
|
||||
return nil, errors.New("user_id not found in context")
|
||||
}
|
||||
var commentQueryList []types.CommentListQueryResult
|
||||
comment := l.svcCtx.DB.ScaCommentReply
|
||||
user := l.svcCtx.DB.ScaAuthUser
|
||||
var orderConditions []field.Expr
|
||||
|
||||
// 查询评论列表
|
||||
return
|
||||
if req.IsHot {
|
||||
orderConditions = append(orderConditions, comment.Likes.Desc(), comment.ReplyCount.Desc())
|
||||
} else {
|
||||
orderConditions = append(orderConditions, comment.CreatedAt.Desc())
|
||||
}
|
||||
count, err := comment.Select(
|
||||
comment.ID,
|
||||
comment.UserID,
|
||||
comment.TopicID,
|
||||
comment.Content,
|
||||
comment.CreatedAt,
|
||||
comment.Author,
|
||||
comment.Likes,
|
||||
comment.ReplyCount,
|
||||
comment.Browser,
|
||||
comment.OperatingSystem,
|
||||
comment.Location,
|
||||
user.Avatar,
|
||||
user.Nickname,
|
||||
).LeftJoin(user, comment.UserID.EqCol(user.UID)).
|
||||
Where(comment.TopicID.Eq(req.TopicId), comment.CommentType.Eq(constant.COMMENT)).
|
||||
Order(orderConditions...).
|
||||
ScanByPage(&commentQueryList, (req.Page-1)*req.Size, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 || len(commentQueryList) == 0 {
|
||||
return response.SuccessWithData(types.CommentListPageResponse{
|
||||
Total: count,
|
||||
Size: req.Size,
|
||||
Current: req.Page,
|
||||
}), nil
|
||||
}
|
||||
// **************** 获取评论Id和用户Id ************
|
||||
commentIds := make([]int64, 0, len(commentQueryList))
|
||||
for _, commentList := range commentQueryList {
|
||||
commentIds = append(commentIds, commentList.ID)
|
||||
}
|
||||
l.wg.Add(2)
|
||||
|
||||
// *************** 获取评论点赞状态 **********
|
||||
likeMap := make(map[int64]bool)
|
||||
go func() {
|
||||
defer l.wg.Done()
|
||||
commentLike := l.svcCtx.DB.ScaCommentLike
|
||||
likeList, err := commentLike.Where(
|
||||
commentLike.TopicID.Eq(req.TopicId),
|
||||
commentLike.UserID.Eq(uid),
|
||||
commentLike.CommentID.In(commentIds...)).
|
||||
Find()
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return
|
||||
}
|
||||
for _, like := range likeList {
|
||||
likeMap[like.CommentID] = true
|
||||
}
|
||||
}()
|
||||
// ***************获取评论图片 **********
|
||||
commentImageMap := make(map[int64][]string)
|
||||
go func() {
|
||||
defer l.wg.Done()
|
||||
newCollection := collection.MustNewCollection[types.CommentImages](l.svcCtx, constant.COMMENT_IMAGES)
|
||||
commentImages, err := newCollection.Finder().
|
||||
Filter(query.Eq("topic_id", req.TopicId)).
|
||||
Filter(query.In("comment_id", commentIds...)).
|
||||
Find(l.ctx)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, image := range commentImages {
|
||||
if len(image.Images) == 0 {
|
||||
continue
|
||||
}
|
||||
imagesBase64 := make([]string, len(image.Images))
|
||||
for i, img := range image.Images {
|
||||
imagesBase64[i] = fmt.Sprintf("data:%s;base64,%s", utils.GetMimeType(img), base64.StdEncoding.EncodeToString(img))
|
||||
}
|
||||
commentImageMap[image.CommentId] = imagesBase64
|
||||
}
|
||||
}()
|
||||
l.wg.Wait()
|
||||
|
||||
// *************** 组装数据 **********
|
||||
result := make([]types.CommentContent, 0, len(commentQueryList))
|
||||
for _, commentData := range commentQueryList {
|
||||
commentContent := types.CommentContent{
|
||||
Avatar: commentData.Avatar,
|
||||
NickName: commentData.Nickname,
|
||||
Content: commentData.Content,
|
||||
CreatedTime: commentData.CreatedAt,
|
||||
Level: 0,
|
||||
Id: commentData.ID,
|
||||
UserId: commentData.UserID,
|
||||
TopicId: commentData.TopicID,
|
||||
IsAuthor: commentData.Author,
|
||||
Likes: commentData.Likes,
|
||||
ReplyCount: commentData.ReplyCount,
|
||||
Location: commentData.Location,
|
||||
Browser: commentData.Browser,
|
||||
OperatingSystem: commentData.OperatingSystem,
|
||||
IsLiked: likeMap[commentData.ID],
|
||||
Images: commentImageMap[commentData.ID],
|
||||
}
|
||||
result = append(result, commentContent)
|
||||
}
|
||||
commentListPageResponse := types.CommentListPageResponse{
|
||||
Total: count,
|
||||
Size: req.Size,
|
||||
Current: req.Page,
|
||||
Comments: result,
|
||||
}
|
||||
return response.SuccessWithData(commentListPageResponse), nil
|
||||
}
|
||||
|
@@ -2,9 +2,19 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/chenmingyong0423/go-mongox/v2/builder/query"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mongodb/collection"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -13,6 +23,7 @@ type GetReplyListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewGetReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetReplyListLogic {
|
||||
@@ -24,7 +35,134 @@ func NewGetReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetR
|
||||
}
|
||||
|
||||
func (l *GetReplyListLogic) GetReplyList(req *types.ReplyListRequest) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 获取用户ID
|
||||
uid, ok := l.ctx.Value("user_id").(string)
|
||||
if !ok {
|
||||
return nil, errors.New("user_id not found in context")
|
||||
}
|
||||
var replyQueryList []types.ReplyListQueryResult
|
||||
reply := l.svcCtx.DB.ScaCommentReply
|
||||
user := l.svcCtx.DB.ScaAuthUser
|
||||
commentUser := user.As("comment_user")
|
||||
replyUser := user.As("reply_user")
|
||||
|
||||
return
|
||||
count, err := reply.Select(
|
||||
reply.ID,
|
||||
reply.UserID,
|
||||
reply.TopicID,
|
||||
reply.Content,
|
||||
reply.CreatedAt,
|
||||
reply.Author,
|
||||
reply.ReplyCount,
|
||||
reply.Likes,
|
||||
reply.Browser,
|
||||
reply.OperatingSystem,
|
||||
reply.Location,
|
||||
reply.ReplyUser,
|
||||
reply.ReplyTo,
|
||||
reply.ReplyID,
|
||||
commentUser.Avatar,
|
||||
commentUser.Nickname,
|
||||
replyUser.Nickname.As("reply_nickname"),
|
||||
).LeftJoin(commentUser, reply.UserID.EqCol(commentUser.UID)).
|
||||
LeftJoin(replyUser, reply.ReplyUser.EqCol(replyUser.UID)).
|
||||
Where(reply.TopicID.Eq(req.TopicId), reply.CommentType.Eq(constant.REPLY)).
|
||||
Order(reply.Likes.Desc(), reply.CreatedAt.Desc()).
|
||||
ScanByPage(&replyQueryList, (req.Page-1)*req.Size, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 || len(replyQueryList) == 0 {
|
||||
return response.SuccessWithData(types.CommentListPageResponse{
|
||||
Total: count,
|
||||
Size: req.Size,
|
||||
Current: req.Page,
|
||||
}), nil
|
||||
}
|
||||
// **************** 获取评论Id和用户Id ************
|
||||
commentIds := make([]int64, 0, len(replyQueryList))
|
||||
for _, commentList := range replyQueryList {
|
||||
commentIds = append(commentIds, commentList.ID)
|
||||
}
|
||||
l.wg.Add(2)
|
||||
|
||||
// *************** 获取评论点赞状态 **********
|
||||
likeMap := make(map[int64]bool)
|
||||
go func() {
|
||||
defer l.wg.Done()
|
||||
commentLike := l.svcCtx.DB.ScaCommentLike
|
||||
likeList, err := commentLike.Where(
|
||||
commentLike.TopicID.Eq(req.TopicId),
|
||||
commentLike.UserID.Eq(uid),
|
||||
commentLike.CommentID.In(commentIds...)).
|
||||
Find()
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return
|
||||
}
|
||||
for _, like := range likeList {
|
||||
likeMap[like.CommentID] = true
|
||||
}
|
||||
}()
|
||||
// ***************获取评论图片 **********
|
||||
commentImageMap := make(map[int64][]string)
|
||||
go func() {
|
||||
defer l.wg.Done()
|
||||
newCollection := collection.MustNewCollection[types.CommentImages](l.svcCtx, constant.COMMENT_IMAGES)
|
||||
commentImages, err := newCollection.Finder().
|
||||
Filter(query.Eq("topic_id", req.TopicId)).
|
||||
Filter(query.In("comment_id", commentIds...)).
|
||||
Find(l.ctx)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, image := range commentImages {
|
||||
if len(image.Images) == 0 {
|
||||
continue
|
||||
}
|
||||
imagesBase64 := make([]string, len(image.Images))
|
||||
for i, img := range image.Images {
|
||||
imagesBase64[i] = fmt.Sprintf("data:%s;base64,%s", utils.GetMimeType(img), base64.StdEncoding.EncodeToString(img))
|
||||
}
|
||||
commentImageMap[image.CommentId] = imagesBase64
|
||||
}
|
||||
}()
|
||||
l.wg.Wait()
|
||||
|
||||
// *************** 组装数据 **********
|
||||
result := make([]types.CommentContent, 0, len(replyQueryList))
|
||||
for _, replyData := range replyQueryList {
|
||||
commentContent := types.CommentContent{
|
||||
Avatar: replyData.Avatar,
|
||||
NickName: replyData.Nickname,
|
||||
Content: replyData.Content,
|
||||
CreatedTime: replyData.CreatedAt,
|
||||
Level: 0,
|
||||
Id: replyData.ID,
|
||||
UserId: replyData.UserID,
|
||||
TopicId: replyData.TopicID,
|
||||
IsAuthor: replyData.Author,
|
||||
Likes: replyData.Likes,
|
||||
ReplyCount: replyData.ReplyCount,
|
||||
Location: replyData.Location,
|
||||
Browser: replyData.Browser,
|
||||
OperatingSystem: replyData.OperatingSystem,
|
||||
IsLiked: likeMap[replyData.ID],
|
||||
Images: commentImageMap[replyData.ID],
|
||||
ReplyUser: replyData.ReplyUser,
|
||||
ReplyTo: replyData.ReplyTo,
|
||||
ReplyId: replyData.ReplyId,
|
||||
ReplyNickname: replyData.ReplyNickname,
|
||||
}
|
||||
result = append(result, commentContent)
|
||||
}
|
||||
commentListPageResponse := types.CommentListPageResponse{
|
||||
Total: count,
|
||||
Size: req.Size,
|
||||
Current: req.Page,
|
||||
Comments: result,
|
||||
}
|
||||
return response.SuccessWithData(commentListPageResponse), nil
|
||||
}
|
||||
|
@@ -2,9 +2,13 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -24,7 +28,35 @@ func NewLikeCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeC
|
||||
}
|
||||
|
||||
func (l *LikeCommentLogic) LikeComment(req *types.CommentLikeRequest) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
uid, ok := l.ctx.Value("user_id").(string)
|
||||
if !ok {
|
||||
return nil, errors.New("user_id not found in context")
|
||||
}
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
commentLike := &model.ScaCommentLike{
|
||||
CommentID: req.CommentId,
|
||||
TopicID: req.TopicId,
|
||||
UserID: uid,
|
||||
LikeTime: time.Now(),
|
||||
}
|
||||
err = tx.ScaCommentLike.Create(commentLike)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
comment := l.svcCtx.DB.ScaCommentReply
|
||||
updates, err := tx.ScaCommentReply.Where(comment.TopicID.Eq(req.TopicId), comment.ID.Eq(req.CommentId)).Update(comment.Likes, comment.Likes.Add(1))
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
if updates.RowsAffected == 0 {
|
||||
_ = tx.Rollback()
|
||||
return response.ErrorWithI18n(l.ctx, "comment.LikeError"), nil
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.Success(), nil
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mongodb/collection"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
@@ -59,7 +60,7 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
operatingSystem := ua.OS()
|
||||
var isAuthor int64 = 0
|
||||
session, err := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uid, ok := session.Values["uid"].(string)
|
||||
@@ -99,14 +100,16 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commentImages := types.CommentImages{
|
||||
commentImages := &types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: comment.ID,
|
||||
Images: imagesData,
|
||||
CreatedAt: comment.CreatedAt.String(),
|
||||
}
|
||||
if _, err = l.svcCtx.MongoClient.Collection(constant.COMMENT_IMAGES).InsertOne(l.ctx, commentImages); err != nil {
|
||||
|
||||
newCollection := collection.MustNewCollection[types.CommentImages](l.svcCtx, constant.COMMENT_IMAGES)
|
||||
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mongodb/collection"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
@@ -99,7 +100,7 @@ func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types
|
||||
return nil, err
|
||||
}
|
||||
commentReply := l.svcCtx.DB.ScaCommentReply
|
||||
update, err := tx.ScaCommentReply.Updates(commentReply.ReplyCount.Add(1))
|
||||
update, err := tx.ScaCommentReply.Where(commentReply.ID.Eq(req.ReplyId)).Update(commentReply.ReplyCount, commentReply.ReplyCount.Add(1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -113,14 +114,16 @@ func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types
|
||||
return nil, err
|
||||
}
|
||||
|
||||
commentImages := types.CommentImages{
|
||||
commentImages := &types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: reply.ID,
|
||||
Images: imagesData,
|
||||
CreatedAt: reply.CreatedAt.String(),
|
||||
}
|
||||
if _, err = l.svcCtx.MongoClient.Collection(constant.COMMENT_IMAGES).InsertOne(l.ctx, commentImages); err != nil {
|
||||
|
||||
newCollection := collection.MustNewCollection[types.CommentImages](l.svcCtx, constant.COMMENT_IMAGES)
|
||||
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import (
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mongodb/collection"
|
||||
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
|
||||
)
|
||||
|
||||
@@ -110,7 +111,7 @@ func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.Rep
|
||||
return nil, err
|
||||
}
|
||||
commentReply := l.svcCtx.DB.ScaCommentReply
|
||||
update, err := tx.ScaCommentReply.Updates(commentReply.ReplyCount.Add(1))
|
||||
update, err := tx.ScaCommentReply.Where(commentReply.ID.Eq(req.ReplyId)).Update(commentReply.ReplyCount, commentReply.ReplyCount.Add(1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,14 +125,16 @@ func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.Rep
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commentImages := types.CommentImages{
|
||||
commentImages := &types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: replyReply.ID,
|
||||
Images: imagesData,
|
||||
CreatedAt: replyReply.CreatedAt.String(),
|
||||
}
|
||||
if _, err = l.svcCtx.MongoClient.Collection(constant.COMMENT_IMAGES).InsertOne(l.ctx, commentImages); err != nil {
|
||||
|
||||
newCollection := collection.MustNewCollection[types.CommentImages](l.svcCtx, constant.COMMENT_IMAGES)
|
||||
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@@ -107,7 +107,7 @@ func (l *GiteeCallbackLogic) GiteeCallback(w http.ResponseWriter, r *http.Reques
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
|
||||
userSocial := l.svcCtx.DB.ScaAuthUserSocial
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(Id), userSocial.Source.Eq(constant.OAuthSourceGitee), userSocial.Deleted.Eq(constant.NotDeleted)).First()
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(Id), userSocial.Source.Eq(constant.OAuthSourceGitee)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -123,7 +123,6 @@ func (l *GiteeCallbackLogic) GiteeCallback(w http.ResponseWriter, r *http.Reques
|
||||
Nickname: giteeUser.Name,
|
||||
Blog: giteeUser.Blog,
|
||||
Email: giteeUser.Email,
|
||||
Deleted: constant.NotDeleted,
|
||||
Gender: constant.Male,
|
||||
}
|
||||
err = tx.ScaAuthUser.Create(addUser)
|
||||
@@ -154,7 +153,7 @@ func (l *GiteeCallbackLogic) GiteeCallback(w http.ResponseWriter, r *http.Reques
|
||||
} else {
|
||||
authUser := l.svcCtx.DB.ScaAuthUser
|
||||
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID), authUser.Deleted.Eq(constant.NotDeleted)).First()
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
|
@@ -106,7 +106,7 @@ func (l *GithubCallbackLogic) GithubCallback(w http.ResponseWriter, r *http.Requ
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
|
||||
userSocial := l.svcCtx.DB.ScaAuthUserSocial
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(Id), userSocial.Source.Eq(constant.OAuthSourceGithub), userSocial.Deleted.Eq(constant.NotDeleted)).First()
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(Id), userSocial.Source.Eq(constant.OAuthSourceGithub)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -116,7 +116,6 @@ func (l *GithubCallbackLogic) GithubCallback(w http.ResponseWriter, r *http.Requ
|
||||
uid := idgen.NextId()
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
|
||||
notDeleted := constant.NotDeleted
|
||||
male := constant.Male
|
||||
addUser := &model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
@@ -125,8 +124,8 @@ func (l *GithubCallbackLogic) GithubCallback(w http.ResponseWriter, r *http.Requ
|
||||
Nickname: gitHubUser.Name,
|
||||
Blog: gitHubUser.Blog,
|
||||
Email: gitHubUser.Email,
|
||||
Deleted: notDeleted,
|
||||
Gender: male,
|
||||
|
||||
Gender: male,
|
||||
}
|
||||
err = tx.ScaAuthUser.Create(addUser)
|
||||
if err != nil {
|
||||
@@ -157,7 +156,7 @@ func (l *GithubCallbackLogic) GithubCallback(w http.ResponseWriter, r *http.Requ
|
||||
} else {
|
||||
authUser := l.svcCtx.DB.ScaAuthUser
|
||||
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID), authUser.Deleted.Eq(constant.NotDeleted)).First()
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
|
@@ -103,7 +103,7 @@ func (l *QqCallbackLogic) QqCallback(w http.ResponseWriter, r *http.Request, req
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
|
||||
userSocial := l.svcCtx.DB.ScaAuthUserSocial
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(authQQme.OpenID), userSocial.Source.Eq(constant.OAuthSourceQQ), userSocial.Deleted.Eq(constant.NotDeleted)).First()
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(authQQme.OpenID), userSocial.Source.Eq(constant.OAuthSourceQQ)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -113,14 +113,12 @@ func (l *QqCallbackLogic) QqCallback(w http.ResponseWriter, r *http.Request, req
|
||||
uid := idgen.NextId()
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
|
||||
notDeleted := constant.NotDeleted
|
||||
male := constant.Male
|
||||
addUser := &model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Avatar: qqUserInfo.FigureurlQq1,
|
||||
Username: authQQme.OpenID,
|
||||
Nickname: qqUserInfo.Nickname,
|
||||
Deleted: notDeleted,
|
||||
Gender: male,
|
||||
}
|
||||
err = tx.ScaAuthUser.Create(addUser)
|
||||
@@ -153,7 +151,7 @@ func (l *QqCallbackLogic) QqCallback(w http.ResponseWriter, r *http.Request, req
|
||||
} else {
|
||||
authUser := l.svcCtx.DB.ScaAuthUser
|
||||
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID), authUser.Deleted.Eq(constant.NotDeleted)).First()
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
|
@@ -117,7 +117,7 @@ func (l *WechatCallbackLogic) HandlerWechatLogin(openId string, clientId string,
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
|
||||
userSocial := l.svcCtx.DB.ScaAuthUserSocial
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(openId), userSocial.Source.Eq(constant.OAuthSourceWechat), userSocial.Deleted.Eq(constant.NotDeleted)).First()
|
||||
socialUser, err := tx.ScaAuthUserSocial.Where(userSocial.OpenID.Eq(openId), userSocial.Source.Eq(constant.OAuthSourceWechat)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
@@ -129,14 +129,12 @@ func (l *WechatCallbackLogic) HandlerWechatLogin(openId string, clientId string,
|
||||
avatar := utils.GenerateAvatar(uidStr)
|
||||
name := randomname.GenerateName()
|
||||
|
||||
notDeleted := constant.NotDeleted
|
||||
male := constant.Male
|
||||
addUser := &model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Avatar: avatar,
|
||||
Username: openId,
|
||||
Nickname: name,
|
||||
Deleted: notDeleted,
|
||||
Gender: male,
|
||||
}
|
||||
err = tx.ScaAuthUser.Create(addUser)
|
||||
@@ -181,7 +179,7 @@ func (l *WechatCallbackLogic) HandlerWechatLogin(openId string, clientId string,
|
||||
} else {
|
||||
authUser := l.svcCtx.DB.ScaAuthUser
|
||||
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID), authUser.Deleted.Eq(constant.NotDeleted)).First()
|
||||
authUserInfo, err := tx.ScaAuthUser.Where(authUser.UID.Eq(socialUser.UserID)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
|
@@ -45,11 +45,11 @@ func (l *AccountLoginLogic) AccountLogin(w http.ResponseWriter, r *http.Request,
|
||||
|
||||
switch {
|
||||
case utils.IsPhone(req.Account):
|
||||
selectedUser = user.Where(user.Phone.Eq(req.Account), user.Deleted.Eq(constant.NotDeleted))
|
||||
selectedUser = user.Where(user.Phone.Eq(req.Account))
|
||||
case utils.IsEmail(req.Account):
|
||||
selectedUser = user.Where(user.Email.Eq(req.Account), user.Deleted.Eq(constant.NotDeleted))
|
||||
selectedUser = user.Where(user.Email.Eq(req.Account))
|
||||
case utils.IsUsername(req.Account):
|
||||
selectedUser = user.Where(user.Username.Eq(req.Account), user.Deleted.Eq(constant.NotDeleted))
|
||||
selectedUser = user.Where(user.Username.Eq(req.Account))
|
||||
default:
|
||||
return response.ErrorWithI18n(l.ctx, "login.invalidAccount"), nil
|
||||
}
|
||||
|
@@ -45,7 +45,7 @@ func (l *PhoneLoginLogic) PhoneLogin(r *http.Request, w http.ResponseWriter, req
|
||||
return response.ErrorWithI18n(l.ctx, "login.captchaError"), nil
|
||||
}
|
||||
authUser := l.svcCtx.DB.ScaAuthUser
|
||||
userInfo, err := authUser.Where(authUser.Phone.Eq(req.Phone), authUser.Deleted.Eq(constant.NotDeleted)).First()
|
||||
userInfo, err := authUser.Where(authUser.Phone.Eq(req.Phone)).First()
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
@@ -61,14 +61,12 @@ func (l *PhoneLoginLogic) PhoneLogin(r *http.Request, w http.ResponseWriter, req
|
||||
uidStr := strconv.FormatInt(uid, 10)
|
||||
avatar := utils.GenerateAvatar(uidStr)
|
||||
name := randomname.GenerateName()
|
||||
notDeleted := constant.NotDeleted
|
||||
male := constant.Male
|
||||
user := &model.ScaAuthUser{
|
||||
UID: uidStr,
|
||||
Phone: req.Phone,
|
||||
Avatar: avatar,
|
||||
Nickname: name,
|
||||
Deleted: notDeleted,
|
||||
Gender: male,
|
||||
}
|
||||
err := tx.ScaAuthUser.Create(user)
|
||||
|
@@ -1,6 +1,19 @@
|
||||
package types
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/chenmingyong0423/go-mongox/v2"
|
||||
)
|
||||
|
||||
// CommentImages 评论 图片
|
||||
type CommentImages struct {
|
||||
mongox.Model `bson:",inline"`
|
||||
TopicId string `json:"topic_id" bson:"topic_id"`
|
||||
CommentId int64 `json:"comment_id" bson:"comment_id"`
|
||||
UserId string `json:"user_id" bson:"user_id"`
|
||||
Images [][]byte `json:"images" bson:"images"`
|
||||
}
|
||||
|
||||
// CommentResponse 评论响应
|
||||
type CommentResponse struct {
|
||||
@@ -18,11 +31,72 @@ type CommentResponse struct {
|
||||
ReplyTo int64 `json:"reply_to,omitempty"`
|
||||
}
|
||||
|
||||
// CommentImages 评论图片
|
||||
type CommentImages struct {
|
||||
TopicId string `json:"topic_id" bson:"topic_id"`
|
||||
CommentId int64 `json:"comment_id" bson:"comment_id"`
|
||||
UserId string `json:"user_id" bson:"user_id"`
|
||||
Images [][]byte `json:"images" bson:"images"`
|
||||
CreatedAt string `json:"created_at" bson:"created_at"`
|
||||
// CommentContent 评论内容
|
||||
type CommentContent struct {
|
||||
NickName string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
Level int64 `json:"level,omitempty" default:"0"`
|
||||
Id int64 `json:"id"`
|
||||
UserId string `json:"user_id"`
|
||||
TopicId string `json:"topic_id"`
|
||||
Content string `json:"content"`
|
||||
ReplyTo int64 `json:"reply_to,omitempty"`
|
||||
ReplyId int64 `json:"reply_id,omitempty"`
|
||||
ReplyUser string `json:"reply_user,omitempty"`
|
||||
ReplyNickname string `json:"reply_nickname,omitempty"`
|
||||
IsAuthor int64 `json:"is_author"`
|
||||
Likes int64 `json:"likes"`
|
||||
ReplyCount int64 `json:"reply_count"`
|
||||
CreatedTime time.Time `json:"created_time"`
|
||||
Location string `json:"location"`
|
||||
Browser string `json:"browser"`
|
||||
OperatingSystem string `json:"operating_system"`
|
||||
IsLiked bool `json:"is_liked" default:"false"`
|
||||
Images []string `json:"images,omitempty"`
|
||||
}
|
||||
|
||||
// CommentListPageResponse 评论返回值
|
||||
type CommentListPageResponse struct {
|
||||
Size int `json:"size"`
|
||||
Total int64 `json:"total"`
|
||||
Current int `json:"current"`
|
||||
Comments []CommentContent `json:"comments"`
|
||||
}
|
||||
|
||||
// CommentListQueryResult 评论列表查询结果
|
||||
type CommentListQueryResult struct {
|
||||
ID int64 `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
TopicID string `json:"topic_id"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Author int64 `json:"author"`
|
||||
Likes int64 `json:"likes"`
|
||||
ReplyCount int64 `json:"reply_count"`
|
||||
Browser string `json:"browser"`
|
||||
OperatingSystem string `json:"operating_system"`
|
||||
Location string `json:"location"`
|
||||
Avatar string `json:"avatar"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
|
||||
// ReplyListQueryResult 回复列表查询结果
|
||||
type ReplyListQueryResult struct {
|
||||
ID int64 `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
TopicID string `json:"topic_id"`
|
||||
Content string `json:"content"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Author int64 `json:"author"`
|
||||
Likes int64 `json:"likes"`
|
||||
ReplyCount int64 `json:"reply_count"`
|
||||
Browser string `json:"browser"`
|
||||
OperatingSystem string `json:"operating_system"`
|
||||
Location string `json:"location"`
|
||||
Avatar string `json:"avatar"`
|
||||
Nickname string `json:"nickname"`
|
||||
ReplyUser string `json:"reply_user"`
|
||||
ReplyId int64 `json:"reply_id"`
|
||||
ReplyTo int64 `json:"reply_to"`
|
||||
ReplyNickname string `json:"reply_nickname"`
|
||||
}
|
||||
|
@@ -23,8 +23,8 @@ type CommentLikeRequest struct {
|
||||
|
||||
type CommentListRequest struct {
|
||||
TopicId string `json:"topic_id"`
|
||||
Page int64 `json:"page,default=1,optional"`
|
||||
Size int64 `json:"size,default=5,optional"`
|
||||
Page int `json:"page,default=1,optional"`
|
||||
Size int `json:"size,default=5,optional"`
|
||||
IsHot bool `json:"is_hot,default=true,optional"`
|
||||
}
|
||||
|
||||
@@ -78,8 +78,8 @@ type ReplyCommentRequest struct {
|
||||
type ReplyListRequest struct {
|
||||
TopicId string `json:"topic_id"`
|
||||
CommentId int64 `json:"comment_id"`
|
||||
Page int64 `json:"page,default=1,optional"`
|
||||
Size int64 `json:"size,default=5,optional"`
|
||||
Page int `json:"page,default=1,optional"`
|
||||
Size int `json:"size,default=5,optional"`
|
||||
}
|
||||
|
||||
type ReplyReplyRequest struct {
|
||||
|
Reference in New Issue
Block a user