mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 02:56:22 +00:00
fix: 修复评论回复计数逻辑,确保正确显示评论的回复数量
This commit is contained in:
@ -10,7 +10,7 @@ type CommentDto struct {
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
User UserDto `json:"user"` // 评论的
|
||||
ReplyCount int64 `json:"reply_count"` // 回复数量
|
||||
ReplyCount uint64 `json:"reply_count"` // 回复数量
|
||||
LikeCount uint64 `json:"like_count"` // 点赞数量
|
||||
IsLiked bool `json:"is_liked"` // 当前用户是否点赞
|
||||
IsPrivate bool `json:"is_private"`
|
||||
|
@ -88,7 +88,7 @@ func (cr *CommentRepo) CreateComment(comment *model.Comment) error {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
depth = parentComment.Depth + 1
|
||||
|
@ -1,12 +1,12 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"errors"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"gorm.io/gorm"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type likeRepo struct{}
|
||||
@ -14,96 +14,96 @@ type likeRepo struct{}
|
||||
var Like = &likeRepo{}
|
||||
|
||||
func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) (bool, error) {
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var finalStatus bool
|
||||
err = GetDB().Transaction(func(tx *gorm.DB) error {
|
||||
isLiked, err := l.IsLiked(userID, targetID, targetType)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return err
|
||||
}
|
||||
if isLiked {
|
||||
if err :=
|
||||
tx.Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).
|
||||
Delete(&model.Like{TargetType: targetType, TargetID: targetID, UserID: userID}).
|
||||
Error; err != nil {
|
||||
logrus.Error(err)
|
||||
return err
|
||||
}
|
||||
finalStatus = false
|
||||
} else {
|
||||
like := &model.Like{
|
||||
TargetType: targetType,
|
||||
TargetID: targetID,
|
||||
UserID: userID,
|
||||
}
|
||||
if err := tx.Create(like).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
finalStatus = true
|
||||
}
|
||||
// 重新计算点赞数量
|
||||
var count int64
|
||||
if err := tx.Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 更新目标的点赞数量
|
||||
switch targetType {
|
||||
case constant.TargetTypePost:
|
||||
if err := tx.Model(&model.Post{}).Where("id = ?", targetID).Update("like_count", count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
case constant.TargetTypeComment:
|
||||
if err := tx.Model(&model.Comment{}).Where("id = ?", targetID).Update("like_count", count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errors.New("invalid target type")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return finalStatus, err
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var finalStatus bool
|
||||
err = GetDB().Transaction(func(tx *gorm.DB) error {
|
||||
isLiked, err := l.IsLiked(userID, targetID, targetType)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return err
|
||||
}
|
||||
if isLiked {
|
||||
if err :=
|
||||
tx.Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).
|
||||
Delete(&model.Like{TargetType: targetType, TargetID: targetID, UserID: userID}).
|
||||
Error; err != nil {
|
||||
logrus.Error(err)
|
||||
return err
|
||||
}
|
||||
finalStatus = false
|
||||
} else {
|
||||
like := &model.Like{
|
||||
TargetType: targetType,
|
||||
TargetID: targetID,
|
||||
UserID: userID,
|
||||
}
|
||||
if err := tx.Create(like).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
finalStatus = true
|
||||
}
|
||||
// 重新计算点赞数量
|
||||
var count int64
|
||||
if err := tx.Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 更新目标的点赞数量
|
||||
//switch targetType {
|
||||
//case constant.TargetTypePost:
|
||||
// if err := tx.Model(&model.Post{}).Where("id = ?", targetID).UpdateColumn("like_count", count).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
//case constant.TargetTypeComment:
|
||||
// if err := tx.Model(&model.Comment{}).Where("id = ?", targetID).UpdateColumn("like_count", count).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
//default:
|
||||
// return errors.New("invalid target type")
|
||||
//}
|
||||
return nil
|
||||
})
|
||||
return finalStatus, err
|
||||
}
|
||||
|
||||
// IsLiked 检查是否点赞
|
||||
func (l *likeRepo) IsLiked(userID, targetID uint, targetType string) (bool, error) {
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var like model.Like
|
||||
err = GetDB().Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).First(&like).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var like model.Like
|
||||
err = GetDB().Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).First(&like).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Count 点赞计数
|
||||
func (l *likeRepo) Count(targetID uint, targetType string) (int64, error) {
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var count int64
|
||||
err = GetDB().Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var count int64
|
||||
err = GetDB().Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (l *likeRepo) checkTargetType(targetType string) error {
|
||||
switch targetType {
|
||||
case constant.TargetTypePost, constant.TargetTypeComment:
|
||||
return nil
|
||||
default:
|
||||
return errors.New("invalid target type")
|
||||
}
|
||||
switch targetType {
|
||||
case constant.TargetTypePost, constant.TargetTypeComment:
|
||||
return nil
|
||||
default:
|
||||
return errors.New("invalid target type")
|
||||
}
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ func (cs *CommentService) GetCommentList(ctx context.Context, req *dto.GetCommen
|
||||
commentDtos := make([]dto.CommentDto, 0)
|
||||
|
||||
for _, comment := range comments {
|
||||
replyCount, _ := repo.Comment.CountReplyComments(currentUserID, comment.ID)
|
||||
//replyCount, _ := repo.Comment.CountReplyComments(currentUserID, comment.ID)
|
||||
isLiked := false
|
||||
if currentUserID != 0 {
|
||||
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(),
|
||||
Depth: comment.Depth,
|
||||
User: comment.User.ToDto(),
|
||||
ReplyCount: replyCount,
|
||||
ReplyCount: comment.CommentCount,
|
||||
LikeCount: comment.LikeCount,
|
||||
IsLiked: isLiked,
|
||||
IsPrivate: comment.IsPrivate,
|
||||
|
Reference in New Issue
Block a user