mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
fix: 修复评论回复计数逻辑,确保正确显示评论的回复数量
This commit is contained in:
@ -10,7 +10,7 @@ type CommentDto struct {
|
|||||||
CreatedAt string `json:"created_at"`
|
CreatedAt string `json:"created_at"`
|
||||||
UpdatedAt string `json:"updated_at"`
|
UpdatedAt string `json:"updated_at"`
|
||||||
User UserDto `json:"user"` // 评论的
|
User UserDto `json:"user"` // 评论的
|
||||||
ReplyCount int64 `json:"reply_count"` // 回复数量
|
ReplyCount uint64 `json:"reply_count"` // 回复数量
|
||||||
LikeCount uint64 `json:"like_count"` // 点赞数量
|
LikeCount uint64 `json:"like_count"` // 点赞数量
|
||||||
IsLiked bool `json:"is_liked"` // 当前用户是否点赞
|
IsLiked bool `json:"is_liked"` // 当前用户是否点赞
|
||||||
IsPrivate bool `json:"is_private"`
|
IsPrivate bool `json:"is_private"`
|
||||||
|
@ -88,7 +88,7 @@ func (cr *CommentRepo) CreateComment(comment *model.Comment) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
parentComment.CommentCount += 1
|
parentComment.CommentCount += 1
|
||||||
if err := tx.Save(&parentComment).Error; err != nil {
|
if err := tx.Model(&parentComment).UpdateColumn("CommentCount", parentComment.CommentCount).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
depth = parentComment.Depth + 1
|
depth = parentComment.Depth + 1
|
||||||
|
@ -51,18 +51,18 @@ func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) (bool, e
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 更新目标的点赞数量
|
// 更新目标的点赞数量
|
||||||
switch targetType {
|
//switch targetType {
|
||||||
case constant.TargetTypePost:
|
//case constant.TargetTypePost:
|
||||||
if err := tx.Model(&model.Post{}).Where("id = ?", targetID).Update("like_count", count).Error; err != nil {
|
// if err := tx.Model(&model.Post{}).Where("id = ?", targetID).UpdateColumn("like_count", count).Error; err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
case constant.TargetTypeComment:
|
//case constant.TargetTypeComment:
|
||||||
if err := tx.Model(&model.Comment{}).Where("id = ?", targetID).Update("like_count", count).Error; err != nil {
|
// if err := tx.Model(&model.Comment{}).Where("id = ?", targetID).UpdateColumn("like_count", count).Error; err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
default:
|
//default:
|
||||||
return errors.New("invalid target type")
|
// return errors.New("invalid target type")
|
||||||
}
|
//}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return finalStatus, err
|
return finalStatus, err
|
||||||
|
@ -138,7 +138,7 @@ func (cs *CommentService) GetCommentList(ctx context.Context, req *dto.GetCommen
|
|||||||
commentDtos := make([]dto.CommentDto, 0)
|
commentDtos := make([]dto.CommentDto, 0)
|
||||||
|
|
||||||
for _, comment := range comments {
|
for _, comment := range comments {
|
||||||
replyCount, _ := repo.Comment.CountReplyComments(currentUserID, comment.ID)
|
//replyCount, _ := repo.Comment.CountReplyComments(currentUserID, comment.ID)
|
||||||
isLiked := false
|
isLiked := false
|
||||||
if currentUserID != 0 {
|
if currentUserID != 0 {
|
||||||
isLiked, _ = repo.Like.IsLiked(currentUserID, comment.ID, constant.TargetTypeComment)
|
isLiked, _ = repo.Like.IsLiked(currentUserID, comment.ID, constant.TargetTypeComment)
|
||||||
@ -154,7 +154,7 @@ func (cs *CommentService) GetCommentList(ctx context.Context, req *dto.GetCommen
|
|||||||
UpdatedAt: comment.UpdatedAt.String(),
|
UpdatedAt: comment.UpdatedAt.String(),
|
||||||
Depth: comment.Depth,
|
Depth: comment.Depth,
|
||||||
User: comment.User.ToDto(),
|
User: comment.User.ToDto(),
|
||||||
ReplyCount: replyCount,
|
ReplyCount: comment.CommentCount,
|
||||||
LikeCount: comment.LikeCount,
|
LikeCount: comment.LikeCount,
|
||||||
IsLiked: isLiked,
|
IsLiked: isLiked,
|
||||||
IsPrivate: comment.IsPrivate,
|
IsPrivate: comment.IsPrivate,
|
||||||
|
Reference in New Issue
Block a user