From a153e0345ab68744975dd698402c81d3fd1eb9de Mon Sep 17 00:00:00 2001 From: landaiqing <3517283258@qq.com> Date: Mon, 4 Nov 2024 17:32:00 +0800 Subject: [PATCH] :art: update submit comment response --- config/rbac_model.conf | 2 +- .../comment_controller/comment_controller.go | 53 ++++++++++++++++--- model/sca_comment_reply.go | 4 +- service/impl/comment_reply_service_impl.go | 22 +++++--- 4 files changed, 64 insertions(+), 17 deletions(-) diff --git a/config/rbac_model.conf b/config/rbac_model.conf index 71159e3..fa9d928 100644 --- a/config/rbac_model.conf +++ b/config/rbac_model.conf @@ -8,7 +8,7 @@ p = sub, obj, act g = _, _ [policy_effect] -e = some(where (p.eft == allow)) +e = some(where (p.eft == allow)) && !some(where (p_eft == deny)) [matchers] m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act \ No newline at end of file diff --git a/controller/comment_controller/comment_controller.go b/controller/comment_controller/comment_controller.go index 7d63f99..ccf7f6c 100644 --- a/controller/comment_controller/comment_controller.go +++ b/controller/comment_controller/comment_controller.go @@ -1,9 +1,12 @@ package comment_controller import ( + "time" + ginI18n "github.com/gin-contrib/i18n" "github.com/gin-gonic/gin" "github.com/mssola/useragent" + "schisandra-cloud-album/common/enum" "schisandra-cloud-album/common/result" "schisandra-cloud-album/global" @@ -81,12 +84,23 @@ func (CommentController) CommentSubmit(c *gin.Context) { OperatingSystem: operatingSystem, Agent: userAgent, } - response := commentReplyService.SubmitCommentService(&commentReply, commentRequest.TopicId, commentRequest.UserID, commentRequest.Images) + commentId, response := commentReplyService.SubmitCommentService(&commentReply, commentRequest.TopicId, commentRequest.UserID, commentRequest.Images) if !response { result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c) return } - result.OkWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitSuccess"), c) + responseData := model.ScaCommentReply{ + Id: commentId, + Content: commentContent, + UserId: commentRequest.UserID, + TopicId: commentRequest.TopicId, + Author: isAuthor, + Location: location, + Browser: browser, + OperatingSystem: operatingSystem, + CreatedTime: time.Now(), + } + result.OkWithData(responseData, c) return } @@ -157,12 +171,25 @@ func (CommentController) ReplySubmit(c *gin.Context) { OperatingSystem: operatingSystem, Agent: userAgent, } - response := commentReplyService.SubmitCommentService(&commentReply, replyCommentRequest.TopicId, replyCommentRequest.UserID, replyCommentRequest.Images) + commentReplyId, response := commentReplyService.SubmitCommentService(&commentReply, replyCommentRequest.TopicId, replyCommentRequest.UserID, replyCommentRequest.Images) if !response { result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c) return } - result.OkWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitSuccess"), c) + responseData := model.ScaCommentReply{ + Id: commentReplyId, + Content: commentContent, + UserId: replyCommentRequest.UserID, + TopicId: replyCommentRequest.TopicId, + ReplyId: replyCommentRequest.ReplyId, + ReplyUser: replyCommentRequest.ReplyUser, + Author: isAuthor, + Location: location, + Browser: browser, + OperatingSystem: operatingSystem, + CreatedTime: time.Now(), + } + result.OkWithData(responseData, c) return } @@ -234,12 +261,26 @@ func (CommentController) ReplyReplySubmit(c *gin.Context) { OperatingSystem: operatingSystem, Agent: userAgent, } - response := commentReplyService.SubmitCommentService(&commentReply, replyReplyRequest.TopicId, replyReplyRequest.UserID, replyReplyRequest.Images) + commentReplyReplyId, response := commentReplyService.SubmitCommentService(&commentReply, replyReplyRequest.TopicId, replyReplyRequest.UserID, replyReplyRequest.Images) if !response { result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c) return } - result.OkWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitSuccess"), c) + responseData := model.ScaCommentReply{ + Id: commentReplyReplyId, + Content: commentContent, + UserId: replyReplyRequest.UserID, + TopicId: replyReplyRequest.TopicId, + ReplyTo: replyReplyRequest.ReplyTo, + ReplyId: replyReplyRequest.ReplyId, + ReplyUser: replyReplyRequest.ReplyUser, + Author: isAuthor, + Location: location, + Browser: browser, + OperatingSystem: operatingSystem, + CreatedTime: time.Now(), + } + result.OkWithData(responseData, c) return } diff --git a/model/sca_comment_reply.go b/model/sca_comment_reply.go index 3beca6a..1fecb0f 100644 --- a/model/sca_comment_reply.go +++ b/model/sca_comment_reply.go @@ -1,8 +1,9 @@ package model import ( - "gorm.io/gorm" "time" + + "gorm.io/gorm" ) const ScaCommentReplyTable = "sca_comment_reply" @@ -30,7 +31,6 @@ type ScaCommentReply struct { Location string `gorm:"column:location;type:varchar(20);comment:评论地址" json:"location"` Browser string `gorm:"column:browser;type:varchar(20);comment:评论浏览器" json:"browser"` OperatingSystem string `gorm:"column:operating_system;type:varchar(20);comment:评论操作系统" json:"operating_system"` - CommentOrder int64 `gorm:"column:comment_order;type:bigint(20);default:0;comment:评论排序" json:"-"` Agent string `gorm:"column:agent;type:varchar(255);comment:客户端设备信息" json:"agent"` } diff --git a/service/impl/comment_reply_service_impl.go b/service/impl/comment_reply_service_impl.go index aacde8f..f78af8f 100644 --- a/service/impl/comment_reply_service_impl.go +++ b/service/impl/comment_reply_service_impl.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "sort" "strconv" "sync" "time" @@ -80,7 +81,7 @@ type LikeData struct { } // SubmitCommentService 提交评论 -func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentReply, topicId string, uid string, images []string) bool { +func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentReply, topicId string, uid string, images []string) (int64, bool) { tx := global.DB.Begin() defer func() { if r := recover(); r != nil { @@ -91,14 +92,14 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep err := commentReplyDao.CreateCommentReply(comment) if err != nil { global.LOG.Errorln(err) - return false + return 0, false } if comment.ReplyId != 0 { err = commentReplyDao.UpdateCommentReplyCount(comment.ReplyId) if err != nil { global.LOG.Errorln(err) tx.Rollback() - return false + return 0, false } } @@ -117,7 +118,7 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep imagesData := <-imagesDataCh if imagesData == nil { tx.Rollback() - return false + return 0, false } commentImages := CommentImages{ @@ -136,7 +137,7 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep }() } tx.Commit() - return true + return comment.Id, true } // GetCommentReplyListService 获取评论回复列表 @@ -330,9 +331,9 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string, query, u := gplus.NewQuery[model.ScaCommentReply]() page := gplus.NewPage[model.ScaCommentReply](pageNum, size) if isHot { - query.OrderByDesc(&u.CommentOrder).OrderByDesc(&u.Likes).OrderByDesc(&u.ReplyCount) + query.OrderByDesc(&u.Likes).OrderByDesc(&u.ReplyCount) } else { - query.OrderByDesc(&u.CommentOrder).OrderByDesc(&u.CreatedTime) + query.OrderByDesc(&u.CreatedTime) } query.Eq(&u.TopicId, topicId).Eq(&u.CommentType, enum.COMMENT) page, pageDB := gplus.SelectPage(page, query) @@ -504,7 +505,12 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string, for commentContent := range commentChannel { commentsWithImages = append(commentsWithImages, commentContent) } - + // 根据CreatedTime进行排序,如果isHot为false + if !isHot { + sort.Slice(commentsWithImages, func(i, j int) bool { + return commentsWithImages[i].CreatedTime.After(commentsWithImages[j].CreatedTime) + }) + } response := CommentResponse{ Comments: commentsWithImages, Size: page.Size,