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
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/snowykami/neo-blog/internal/model"
|
"github.com/snowykami/neo-blog/internal/model"
|
||||||
"github.com/snowykami/neo-blog/pkg/constant"
|
"github.com/snowykami/neo-blog/pkg/constant"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type likeRepo struct{}
|
type likeRepo struct{}
|
||||||
@ -14,96 +14,96 @@ type likeRepo struct{}
|
|||||||
var Like = &likeRepo{}
|
var Like = &likeRepo{}
|
||||||
|
|
||||||
func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) (bool, error) {
|
func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) (bool, error) {
|
||||||
err := l.checkTargetType(targetType)
|
err := l.checkTargetType(targetType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
var finalStatus bool
|
var finalStatus bool
|
||||||
err = GetDB().Transaction(func(tx *gorm.DB) error {
|
err = GetDB().Transaction(func(tx *gorm.DB) error {
|
||||||
isLiked, err := l.IsLiked(userID, targetID, targetType)
|
isLiked, err := l.IsLiked(userID, targetID, targetType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if isLiked {
|
if isLiked {
|
||||||
if err :=
|
if err :=
|
||||||
tx.Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).
|
tx.Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).
|
||||||
Delete(&model.Like{TargetType: targetType, TargetID: targetID, UserID: userID}).
|
Delete(&model.Like{TargetType: targetType, TargetID: targetID, UserID: userID}).
|
||||||
Error; err != nil {
|
Error; err != nil {
|
||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
finalStatus = false
|
finalStatus = false
|
||||||
} else {
|
} else {
|
||||||
like := &model.Like{
|
like := &model.Like{
|
||||||
TargetType: targetType,
|
TargetType: targetType,
|
||||||
TargetID: targetID,
|
TargetID: targetID,
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
}
|
}
|
||||||
if err := tx.Create(like).Error; err != nil {
|
if err := tx.Create(like).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
finalStatus = true
|
finalStatus = true
|
||||||
}
|
}
|
||||||
// 重新计算点赞数量
|
// 重新计算点赞数量
|
||||||
var count int64
|
var count int64
|
||||||
if err := tx.Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error; err != nil {
|
if err := tx.Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error; err != nil {
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsLiked 检查是否点赞
|
// IsLiked 检查是否点赞
|
||||||
func (l *likeRepo) IsLiked(userID, targetID uint, targetType string) (bool, error) {
|
func (l *likeRepo) IsLiked(userID, targetID uint, targetType string) (bool, error) {
|
||||||
err := l.checkTargetType(targetType)
|
err := l.checkTargetType(targetType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
var like model.Like
|
var like model.Like
|
||||||
err = GetDB().Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).First(&like).Error
|
err = GetDB().Where("target_type = ? AND target_id = ? AND user_id = ?", targetType, targetID, userID).First(&like).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count 点赞计数
|
// Count 点赞计数
|
||||||
func (l *likeRepo) Count(targetID uint, targetType string) (int64, error) {
|
func (l *likeRepo) Count(targetID uint, targetType string) (int64, error) {
|
||||||
err := l.checkTargetType(targetType)
|
err := l.checkTargetType(targetType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
var count int64
|
var count int64
|
||||||
err = GetDB().Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error
|
err = GetDB().Model(&model.Like{}).Where("target_type = ? AND target_id = ?", targetType, targetID).Count(&count).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return count, nil
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *likeRepo) checkTargetType(targetType string) error {
|
func (l *likeRepo) checkTargetType(targetType string) error {
|
||||||
switch targetType {
|
switch targetType {
|
||||||
case constant.TargetTypePost, constant.TargetTypeComment:
|
case constant.TargetTypePost, constant.TargetTypeComment:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return errors.New("invalid target type")
|
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)
|
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