From 052d72c8c0630dd0020ab6328970b6d782afa6c9 Mon Sep 17 00:00:00 2001 From: Snowykami Date: Wed, 10 Sep 2025 09:59:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=B7=BB=E5=8A=A0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=8A=A8=E7=94=BB=E6=A0=B7=E5=BC=8F=E5=92=8C=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/comment.go | 252 ++++++++++---------- web/src/components/comment/comment-item.tsx | 7 + 2 files changed, 133 insertions(+), 126 deletions(-) diff --git a/internal/service/comment.go b/internal/service/comment.go index 7161f6e..aec6e22 100644 --- a/internal/service/comment.go +++ b/internal/service/comment.go @@ -1,177 +1,177 @@ package service import ( - "context" - "strconv" + "context" + "strconv" - "github.com/snowykami/neo-blog/pkg/constant" + "github.com/snowykami/neo-blog/pkg/constant" - "github.com/snowykami/neo-blog/internal/ctxutils" - "github.com/snowykami/neo-blog/internal/dto" - "github.com/snowykami/neo-blog/internal/model" - "github.com/snowykami/neo-blog/internal/repo" - "github.com/snowykami/neo-blog/pkg/errs" + "github.com/snowykami/neo-blog/internal/ctxutils" + "github.com/snowykami/neo-blog/internal/dto" + "github.com/snowykami/neo-blog/internal/model" + "github.com/snowykami/neo-blog/internal/repo" + "github.com/snowykami/neo-blog/pkg/errs" ) type CommentService struct{} func NewCommentService() *CommentService { - return &CommentService{} + return &CommentService{} } func (cs *CommentService) CreateComment(ctx context.Context, req *dto.CreateCommentReq) error { - currentUser, ok := ctxutils.GetCurrentUser(ctx) - if !ok { - return errs.ErrUnauthorized - } + currentUser, ok := ctxutils.GetCurrentUser(ctx) + if !ok { + return errs.ErrUnauthorized + } - if ok, err := cs.checkTargetExists(req.TargetID, req.TargetType); !ok { - if err != nil { - return errs.New(errs.ErrBadRequest.Code, "target not found", err) - } - return errs.ErrBadRequest - } + if ok, err := cs.checkTargetExists(req.TargetID, req.TargetType); !ok { + if err != nil { + return errs.New(errs.ErrBadRequest.Code, "target not found", err) + } + return errs.ErrBadRequest + } - comment := &model.Comment{ - Content: req.Content, - ReplyID: req.ReplyID, - TargetID: req.TargetID, - TargetType: req.TargetType, - UserID: currentUser.ID, - IsPrivate: req.IsPrivate, - } + comment := &model.Comment{ + Content: req.Content, + ReplyID: req.ReplyID, + TargetID: req.TargetID, + TargetType: req.TargetType, + UserID: currentUser.ID, + IsPrivate: req.IsPrivate, + } - err := repo.Comment.CreateComment(comment) + err := repo.Comment.CreateComment(comment) - if err != nil { - return err - } + if err != nil { + return err + } - return nil + return nil } func (cs *CommentService) UpdateComment(ctx context.Context, req *dto.UpdateCommentReq) error { - currentUser, ok := ctxutils.GetCurrentUser(ctx) - if !ok { - return errs.ErrUnauthorized - } + currentUser, ok := ctxutils.GetCurrentUser(ctx) + if !ok { + return errs.ErrUnauthorized + } - comment, err := repo.Comment.GetComment(strconv.Itoa(int(req.CommentID))) - if err != nil { - return err - } + comment, err := repo.Comment.GetComment(strconv.Itoa(int(req.CommentID))) + if err != nil { + return err + } - if currentUser.ID != comment.UserID { - return errs.ErrForbidden - } + if currentUser.ID != comment.UserID { + return errs.ErrForbidden + } - comment.Content = req.Content - comment.IsPrivate = req.IsPrivate + comment.Content = req.Content + comment.IsPrivate = req.IsPrivate - err = repo.Comment.UpdateComment(comment) + err = repo.Comment.UpdateComment(comment) - if err != nil { - return err - } + if err != nil { + return err + } - return nil + return nil } func (cs *CommentService) DeleteComment(ctx context.Context, commentID string) error { - currentUser, ok := ctxutils.GetCurrentUser(ctx) - if !ok { - return errs.ErrUnauthorized - } - if commentID == "" { - return errs.ErrBadRequest - } + currentUser, ok := ctxutils.GetCurrentUser(ctx) + if !ok { + return errs.ErrUnauthorized + } + if commentID == "" { + return errs.ErrBadRequest + } - comment, err := repo.Comment.GetComment(commentID) - if err != nil { - return errs.New(errs.ErrNotFound.Code, "comment not found", err) - } + comment, err := repo.Comment.GetComment(commentID) + if err != nil { + return errs.New(errs.ErrNotFound.Code, "comment not found", err) + } - if comment.UserID != currentUser.ID { - return errs.ErrForbidden - } + if comment.UserID != currentUser.ID { + return errs.ErrForbidden + } - if err := repo.Comment.DeleteComment(commentID); err != nil { - return err - } + if err := repo.Comment.DeleteComment(commentID); err != nil { + return err + } - return nil + return nil } func (cs *CommentService) GetComment(ctx context.Context, commentID string) (*dto.CommentDto, error) { - comment, err := repo.Comment.GetComment(commentID) + comment, err := repo.Comment.GetComment(commentID) - if err != nil { - return nil, errs.New(errs.ErrNotFound.Code, "comment not found", err) - } + if err != nil { + return nil, errs.New(errs.ErrNotFound.Code, "comment not found", err) + } - commentDto := dto.CommentDto{ - ID: comment.ID, - TargetID: comment.TargetID, - TargetType: comment.TargetType, - Content: comment.Content, - ReplyID: comment.ReplyID, - Depth: comment.Depth, - CreatedAt: comment.CreatedAt.String(), - UpdatedAt: comment.UpdatedAt.String(), - User: comment.User.ToDto(), - } + commentDto := dto.CommentDto{ + ID: comment.ID, + TargetID: comment.TargetID, + TargetType: comment.TargetType, + Content: comment.Content, + ReplyID: comment.ReplyID, + Depth: comment.Depth, + CreatedAt: comment.CreatedAt.String(), + UpdatedAt: comment.UpdatedAt.String(), + User: comment.User.ToDto(), + } - return &commentDto, err + return &commentDto, err } func (cs *CommentService) GetCommentList(ctx context.Context, req *dto.GetCommentListReq) ([]dto.CommentDto, error) { - currentUserID := uint(0) - if currentUser, ok := ctxutils.GetCurrentUser(ctx); ok { - currentUserID = currentUser.ID - } + currentUserID := uint(0) + if currentUser, ok := ctxutils.GetCurrentUser(ctx); ok { + currentUserID = currentUser.ID + } - comments, err := repo.Comment.ListComments(currentUserID, req.TargetID, req.CommentID, req.TargetType, req.Page, req.Size, req.OrderBy, req.Desc, req.Depth) - if err != nil { - return nil, errs.New(errs.ErrInternalServer.Code, "failed to list comments", err) - } + comments, err := repo.Comment.ListComments(currentUserID, req.TargetID, req.CommentID, req.TargetType, req.Page, req.Size, req.OrderBy, req.Desc, req.Depth) + if err != nil { + return nil, errs.New(errs.ErrInternalServer.Code, "failed to list comments", err) + } - commentDtos := make([]dto.CommentDto, 0) + commentDtos := make([]dto.CommentDto, 0) - for _, comment := range comments { - replyCount, _ := repo.Comment.CountReplyComments(currentUserID, comment.ID) - isLiked := false - if currentUserID != 0 { - isLiked, _ = repo.Like.IsLiked(currentUserID, comment.ID, constant.TargetTypeComment) - } + for _, comment := range comments { + replyCount, _ := repo.Comment.CountReplyComments(currentUserID, comment.ID) + isLiked := false + if currentUserID != 0 { + isLiked, _ = repo.Like.IsLiked(currentUserID, comment.ID, constant.TargetTypeComment) + } - commentDto := dto.CommentDto{ - ID: comment.ID, - Content: comment.Content, - TargetID: comment.TargetID, - TargetType: comment.TargetType, - ReplyID: comment.ReplyID, - CreatedAt: comment.CreatedAt.String(), - UpdatedAt: comment.UpdatedAt.String(), - Depth: comment.Depth, - User: comment.User.ToDto(), - ReplyCount: replyCount, - LikeCount: comment.LikeCount, - IsLiked: isLiked, - IsPrivate: comment.IsPrivate, - } - commentDtos = append(commentDtos, commentDto) - } - return commentDtos, nil + commentDto := dto.CommentDto{ + ID: comment.ID, + Content: comment.Content, + TargetID: comment.TargetID, + TargetType: comment.TargetType, + ReplyID: comment.ReplyID, + CreatedAt: comment.CreatedAt.String(), + UpdatedAt: comment.UpdatedAt.String(), + Depth: comment.Depth, + User: comment.User.ToDto(), + ReplyCount: replyCount, + LikeCount: comment.LikeCount, + IsLiked: isLiked, + IsPrivate: comment.IsPrivate, + } + commentDtos = append(commentDtos, commentDto) + } + return commentDtos, nil } func (cs *CommentService) checkTargetExists(targetID uint, targetType string) (bool, error) { - switch targetType { - case constant.TargetTypePost: - if _, err := repo.Post.GetPostByID(strconv.Itoa(int(targetID))); err != nil { - return false, errs.New(errs.ErrNotFound.Code, "post not found", err) - } - default: - return false, errs.New(errs.ErrBadRequest.Code, "invalid target type", nil) - } - return true, nil + switch targetType { + case constant.TargetTypePost: + if _, err := repo.Post.GetPostByID(strconv.Itoa(int(targetID))); err != nil { + return false, errs.New(errs.ErrNotFound.Code, "post not found", err) + } + default: + return false, errs.New(errs.ErrBadRequest.Code, "invalid target type", nil) + } + return true, nil } diff --git a/web/src/components/comment/comment-item.tsx b/web/src/components/comment/comment-item.tsx index 7c213fa..cad65b3 100644 --- a/web/src/components/comment/comment-item.tsx +++ b/web/src/components/comment/comment-item.tsx @@ -36,6 +36,7 @@ export function CommentItem( const [likeCount, setLikeCount] = useState(comment.likeCount); const [liked, setLiked] = useState(comment.isLiked); + const [canClickLike, setCanClickLike] = useState(true); const [isPrivate, setIsPrivate] = useState(comment.isPrivate); const [replyCount, setReplyCount] = useState(comment.replyCount); const [showReplies, setShowReplies] = useState(false); @@ -45,6 +46,10 @@ export function CommentItem( const [showEditInput, setShowEditInput] = useState(false); const handleToggleLike = () => { + if (!canClickLike) { + return; + } + setCanClickLike(false); if (!user) { toast.error(t("login_required"), { action:
@@ -58,6 +63,7 @@ export function CommentItem( }); return; } + setLiked(!liked) // 提前转换状态,让用户觉得响应很快 toggleLike( { targetType: TargetType.Comment, targetId: comment.id } ).then(res => { @@ -67,6 +73,7 @@ export function CommentItem( }).catch(error => { toast.error(t("like_failed") + ": " + error.message); }); + setCanClickLike(true); } const reloadReplies = () => {