🎨 update submit comment response

This commit is contained in:
landaiqing
2024-11-04 17:32:00 +08:00
parent 89a9b21bff
commit a153e0345a
4 changed files with 64 additions and 17 deletions

View File

@@ -8,7 +8,7 @@ p = sub, obj, act
g = _, _ g = _, _
[policy_effect] [policy_effect]
e = some(where (p.eft == allow)) e = some(where (p.eft == allow)) && !some(where (p_eft == deny))
[matchers] [matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

View File

@@ -1,9 +1,12 @@
package comment_controller package comment_controller
import ( import (
"time"
ginI18n "github.com/gin-contrib/i18n" ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/mssola/useragent" "github.com/mssola/useragent"
"schisandra-cloud-album/common/enum" "schisandra-cloud-album/common/enum"
"schisandra-cloud-album/common/result" "schisandra-cloud-album/common/result"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
@@ -81,12 +84,23 @@ func (CommentController) CommentSubmit(c *gin.Context) {
OperatingSystem: operatingSystem, OperatingSystem: operatingSystem,
Agent: userAgent, 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 { if !response {
result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c) result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c)
return 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 return
} }
@@ -157,12 +171,25 @@ func (CommentController) ReplySubmit(c *gin.Context) {
OperatingSystem: operatingSystem, OperatingSystem: operatingSystem,
Agent: userAgent, 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 { if !response {
result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c) result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c)
return 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 return
} }
@@ -234,12 +261,26 @@ func (CommentController) ReplyReplySubmit(c *gin.Context) {
OperatingSystem: operatingSystem, OperatingSystem: operatingSystem,
Agent: userAgent, 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 { if !response {
result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c) result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c)
return 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 return
} }

View File

@@ -1,8 +1,9 @@
package model package model
import ( import (
"gorm.io/gorm"
"time" "time"
"gorm.io/gorm"
) )
const ScaCommentReplyTable = "sca_comment_reply" const ScaCommentReplyTable = "sca_comment_reply"
@@ -30,7 +31,6 @@ type ScaCommentReply struct {
Location string `gorm:"column:location;type:varchar(20);comment:评论地址" json:"location"` Location string `gorm:"column:location;type:varchar(20);comment:评论地址" json:"location"`
Browser string `gorm:"column:browser;type:varchar(20);comment:评论浏览器" json:"browser"` Browser string `gorm:"column:browser;type:varchar(20);comment:评论浏览器" json:"browser"`
OperatingSystem string `gorm:"column:operating_system;type:varchar(20);comment:评论操作系统" json:"operating_system"` 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"` Agent string `gorm:"column:agent;type:varchar(255);comment:客户端设备信息" json:"agent"`
} }

View File

@@ -5,6 +5,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"sort"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@@ -80,7 +81,7 @@ type LikeData struct {
} }
// SubmitCommentService 提交评论 // 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() tx := global.DB.Begin()
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@@ -91,14 +92,14 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep
err := commentReplyDao.CreateCommentReply(comment) err := commentReplyDao.CreateCommentReply(comment)
if err != nil { if err != nil {
global.LOG.Errorln(err) global.LOG.Errorln(err)
return false return 0, false
} }
if comment.ReplyId != 0 { if comment.ReplyId != 0 {
err = commentReplyDao.UpdateCommentReplyCount(comment.ReplyId) err = commentReplyDao.UpdateCommentReplyCount(comment.ReplyId)
if err != nil { if err != nil {
global.LOG.Errorln(err) global.LOG.Errorln(err)
tx.Rollback() tx.Rollback()
return false return 0, false
} }
} }
@@ -117,7 +118,7 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep
imagesData := <-imagesDataCh imagesData := <-imagesDataCh
if imagesData == nil { if imagesData == nil {
tx.Rollback() tx.Rollback()
return false return 0, false
} }
commentImages := CommentImages{ commentImages := CommentImages{
@@ -136,7 +137,7 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep
}() }()
} }
tx.Commit() tx.Commit()
return true return comment.Id, true
} }
// GetCommentReplyListService 获取评论回复列表 // GetCommentReplyListService 获取评论回复列表
@@ -330,9 +331,9 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string,
query, u := gplus.NewQuery[model.ScaCommentReply]() query, u := gplus.NewQuery[model.ScaCommentReply]()
page := gplus.NewPage[model.ScaCommentReply](pageNum, size) page := gplus.NewPage[model.ScaCommentReply](pageNum, size)
if isHot { if isHot {
query.OrderByDesc(&u.CommentOrder).OrderByDesc(&u.Likes).OrderByDesc(&u.ReplyCount) query.OrderByDesc(&u.Likes).OrderByDesc(&u.ReplyCount)
} else { } else {
query.OrderByDesc(&u.CommentOrder).OrderByDesc(&u.CreatedTime) query.OrderByDesc(&u.CreatedTime)
} }
query.Eq(&u.TopicId, topicId).Eq(&u.CommentType, enum.COMMENT) query.Eq(&u.TopicId, topicId).Eq(&u.CommentType, enum.COMMENT)
page, pageDB := gplus.SelectPage(page, query) page, pageDB := gplus.SelectPage(page, query)
@@ -504,7 +505,12 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string,
for commentContent := range commentChannel { for commentContent := range commentChannel {
commentsWithImages = append(commentsWithImages, commentContent) 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{ response := CommentResponse{
Comments: commentsWithImages, Comments: commentsWithImages,
Size: page.Size, Size: page.Size,