mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-23 17:46:22 +00:00
feat: implement advanced comment features including reply and like functionality
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 13s
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 13s
- Added support for nested comments with reply functionality. - Implemented like/unlike feature for comments and posts. - Enhanced comment DTO to include reply count, like count, and like status. - Updated comment and like services to handle new functionalities. - Created new API endpoints for toggling likes and listing comments. - Improved UI components for comments to support replies and likes with animations. - Added localization for new comment-related messages. - Introduced a TODO list for future enhancements in the comment module.
This commit is contained in:
@ -9,6 +9,7 @@ import (
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"github.com/snowykami/neo-blog/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -92,16 +93,15 @@ func (cr *CommentRepo) CreateComment(comment *model.Comment) error {
|
||||
}
|
||||
depth = parentComment.Depth + 1
|
||||
}
|
||||
|
||||
if depth > utils.Env.GetAsInt(constant.EnvKeyMaxReplyDepth, constant.MaxReplyDepthDefault) {
|
||||
return errs.New(http.StatusBadRequest, "exceeded maximum reply depth", nil)
|
||||
}
|
||||
comment.Depth = depth
|
||||
|
||||
if err := tx.Create(comment).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@ -172,7 +172,7 @@ func (cr *CommentRepo) GetComment(commentID string) (*model.Comment, error) {
|
||||
return &comment, nil
|
||||
}
|
||||
|
||||
func (cr *CommentRepo) ListComments(currentUserID uint, targetID uint, targetType string, page, size uint64, orderBy string, desc bool, depth int) ([]model.Comment, error) {
|
||||
func (cr *CommentRepo) ListComments(currentUserID, targetID, commentID uint, targetType string, page, size uint64, orderBy string, desc bool, depth int) ([]model.Comment, error) {
|
||||
if !slices.Contains(constant.OrderByEnumComment, orderBy) {
|
||||
return nil, errs.New(http.StatusBadRequest, "invalid order_by parameter", nil)
|
||||
}
|
||||
@ -189,13 +189,17 @@ func (cr *CommentRepo) ListComments(currentUserID uint, targetID uint, targetTyp
|
||||
|
||||
query := GetDB().Model(&model.Comment{}).Preload("User")
|
||||
|
||||
if commentID > 0 {
|
||||
query = query.Where("reply_id = ?", commentID)
|
||||
}
|
||||
|
||||
if currentUserID > 0 {
|
||||
query = query.Where("(is_private = ? OR (is_private = ? AND (user_id = ? OR user_id = ?)))", false, true, currentUserID, masterID)
|
||||
} else {
|
||||
query = query.Where("is_private = ?", false)
|
||||
}
|
||||
|
||||
if depth > 0 {
|
||||
if depth >= 0 {
|
||||
query = query.Where("target_id = ? AND target_type = ? AND depth = ?", targetID, targetType, depth)
|
||||
} else {
|
||||
query = query.Where("target_id = ? AND target_type = ?", targetID, targetType)
|
||||
@ -208,3 +212,11 @@ func (cr *CommentRepo) ListComments(currentUserID uint, targetID uint, targetTyp
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (cr *CommentRepo) CountReplyComments(commentID uint) (int64, error) {
|
||||
var count int64
|
||||
if err := GetDB().Model(&model.Comment{}).Where("reply_id = ?", commentID).Count(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package repo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"gorm.io/gorm"
|
||||
@ -11,24 +13,28 @@ type likeRepo struct{}
|
||||
|
||||
var Like = &likeRepo{}
|
||||
|
||||
func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) error {
|
||||
func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) (bool, error) {
|
||||
err := l.checkTargetType(targetType)
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
return GetDB().Transaction(func(tx *gorm.DB) error {
|
||||
// 判断是否已点赞
|
||||
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{}).Error; err != nil {
|
||||
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,
|
||||
@ -37,6 +43,7 @@ func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) error {
|
||||
if err := tx.Create(like).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
finalStatus = true
|
||||
}
|
||||
// 重新计算点赞数量
|
||||
var count int64
|
||||
@ -58,6 +65,7 @@ func (l *likeRepo) ToggleLike(userID, targetID uint, targetType string) error {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return finalStatus, err
|
||||
}
|
||||
|
||||
// IsLiked 检查是否点赞
|
||||
|
@ -1,11 +1,12 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"net/http"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type postRepo struct{}
|
||||
@ -73,13 +74,13 @@ func (p *postRepo) ListPosts(currentUserID uint, keywords []string, page, size u
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (p *postRepo) ToggleLikePost(postID uint, userID uint) error {
|
||||
func (p *postRepo) ToggleLikePost(postID uint, userID uint) (bool, error) {
|
||||
if postID == 0 || userID == 0 {
|
||||
return errs.New(http.StatusBadRequest, "invalid post ID or user ID", nil)
|
||||
return false, errs.New(http.StatusBadRequest, "invalid post ID or user ID", nil)
|
||||
}
|
||||
err := Like.ToggleLike(userID, postID, constant.TargetTypePost)
|
||||
liked, err := Like.ToggleLike(userID, postID, constant.TargetTypePost)
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
return nil
|
||||
return liked, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user