fix_api
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
package ForumComments
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"TrangleAgent/api/forum/v1"
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"TrangleAgent/internal/dao"
|
||||
"TrangleAgent/internal/model/entity"
|
||||
"TrangleAgent/internal/service"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// ForumComments 评论相关方法
|
||||
@@ -21,13 +25,12 @@ func init() {
|
||||
// ForumCommentsCreate 创建评论
|
||||
func (s *sForumComments) Create(ctx context.Context, req *v1.ForumCommentsCreateReq) (res *v1.ForumCommentsCreateRes, err error) {
|
||||
data := &entity.ForumComments{
|
||||
UserId: req.UserId,
|
||||
PostId: req.PostId,
|
||||
ParentId: req.ParentId,
|
||||
Content: req.Content,
|
||||
Status: req.Status,
|
||||
LikeCount: 0,
|
||||
ReplyCount: 0,
|
||||
UserId: req.UserId,
|
||||
PostId: req.PostId,
|
||||
ParentId: req.ParentId,
|
||||
Content: req.Content,
|
||||
Status: req.Status,
|
||||
LikeCount: 0,
|
||||
}
|
||||
|
||||
result, err := dao.ForumComments.Ctx(ctx).Data(data).OmitEmpty().Insert()
|
||||
@@ -42,20 +45,15 @@ func (s *sForumComments) Create(ctx context.Context, req *v1.ForumCommentsCreate
|
||||
|
||||
// 更新帖子的评论数
|
||||
if req.PostId > 0 {
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(req.PostId).OnDuplicate("comment_count", 1).Update()
|
||||
updateData := map[string]interface{}{
|
||||
dao.ForumPosts.Columns().CommentCount: gdb.Raw(fmt.Sprintf("%s + 1", dao.ForumPosts.Columns().CommentCount)),
|
||||
}
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(req.PostId).Data(updateData).Update()
|
||||
if err != nil {
|
||||
g.Log().Warning(ctx, "更新帖子评论数失败:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是回复评论,则更新父评论的回复数
|
||||
if req.ParentId > 0 {
|
||||
_, err = dao.ForumComments.Ctx(ctx).WherePri(req.ParentId).OnDuplicate("reply_count", 1).Update()
|
||||
if err != nil {
|
||||
g.Log().Warning(ctx, "更新父评论回复数失败:", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &v1.ForumCommentsCreateRes{
|
||||
Id: uint64(id),
|
||||
}, nil
|
||||
@@ -63,8 +61,11 @@ func (s *sForumComments) Create(ctx context.Context, req *v1.ForumCommentsCreate
|
||||
|
||||
// ForumCommentsUpdate 更新评论
|
||||
func (s *sForumComments) Update(ctx context.Context, req *v1.ForumCommentsUpdateReq) (res *v1.ForumCommentsUpdateRes, err error) {
|
||||
// 检查评论是否存在
|
||||
one, err := dao.ForumComments.Ctx(ctx).WherePri(req.Id).One()
|
||||
// 检查评论是否存在(排除已删除的)
|
||||
one, err := dao.ForumComments.Ctx(ctx).
|
||||
WherePri(req.Id).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论失败")
|
||||
}
|
||||
@@ -85,7 +86,10 @@ func (s *sForumComments) Update(ctx context.Context, req *v1.ForumCommentsUpdate
|
||||
|
||||
// ForumCommentsView 查看评论
|
||||
func (s *sForumComments) View(ctx context.Context, req *v1.ForumCommentsViewReq) (res *v1.ForumCommentsViewRes, err error) {
|
||||
record, err := dao.ForumComments.Ctx(ctx).WherePri(req.Id).One()
|
||||
record, err := dao.ForumComments.Ctx(ctx).
|
||||
WherePri(req.Id).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论失败")
|
||||
}
|
||||
@@ -100,16 +104,15 @@ func (s *sForumComments) View(ctx context.Context, req *v1.ForumCommentsViewReq)
|
||||
}
|
||||
|
||||
res = &v1.ForumCommentsViewRes{
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
PostId: entity.PostId,
|
||||
ParentId: entity.ParentId,
|
||||
Content: entity.Content,
|
||||
Status: entity.Status,
|
||||
LikeCount: entity.LikeCount,
|
||||
ReplyCount: entity.ReplyCount,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
PostId: entity.PostId,
|
||||
ParentId: entity.ParentId,
|
||||
Content: entity.Content,
|
||||
Status: entity.Status,
|
||||
LikeCount: entity.LikeCount,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -117,23 +120,25 @@ func (s *sForumComments) View(ctx context.Context, req *v1.ForumCommentsViewReq)
|
||||
|
||||
// ForumCommentsList 评论列表
|
||||
func (s *sForumComments) List(ctx context.Context, req *v1.ForumCommentsListReq) (res *v1.ForumCommentsListRes, err error) {
|
||||
query := dao.ForumComments.Ctx(ctx).OmitEmpty()
|
||||
query := dao.ForumComments.Ctx(ctx).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
OmitEmpty()
|
||||
|
||||
if req.UserId > 0 {
|
||||
query = query.Where("user_id", req.UserId)
|
||||
query = query.Where(dao.ForumComments.Columns().UserId, req.UserId)
|
||||
}
|
||||
if req.PostId > 0 {
|
||||
query = query.Where("post_id", req.PostId)
|
||||
query = query.Where(dao.ForumComments.Columns().PostId, req.PostId)
|
||||
}
|
||||
if req.ParentId > 0 {
|
||||
query = query.Where("parent_id", req.ParentId)
|
||||
query = query.Where(dao.ForumComments.Columns().ParentId, req.ParentId)
|
||||
}
|
||||
if req.Status != "" {
|
||||
query = query.Where("status", req.Status)
|
||||
query = query.Where(dao.ForumComments.Columns().Status, req.Status)
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
pageResult, err := query.Page(req.Page, req.PageSize).Order("created_at DESC").All()
|
||||
pageResult, err := query.Page(req.Page, req.PageSize).OrderDesc(dao.ForumComments.Columns().CreatedAt).All()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论列表失败")
|
||||
}
|
||||
@@ -147,16 +152,15 @@ func (s *sForumComments) List(ctx context.Context, req *v1.ForumCommentsListReq)
|
||||
}
|
||||
|
||||
item := &v1.ForumCommentsViewRes{
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
PostId: entity.PostId,
|
||||
ParentId: entity.ParentId,
|
||||
Content: entity.Content,
|
||||
Status: entity.Status,
|
||||
LikeCount: entity.LikeCount,
|
||||
ReplyCount: entity.ReplyCount,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
PostId: entity.PostId,
|
||||
ParentId: entity.ParentId,
|
||||
Content: entity.Content,
|
||||
Status: entity.Status,
|
||||
LikeCount: entity.LikeCount,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
@@ -177,8 +181,11 @@ func (s *sForumComments) List(ctx context.Context, req *v1.ForumCommentsListReq)
|
||||
|
||||
// ForumCommentsDelete 删除评论
|
||||
func (s *sForumComments) Delete(ctx context.Context, req *v1.ForumCommentsDeleteReq) (res *v1.ForumCommentsDeleteRes, err error) {
|
||||
// 检查评论是否存在
|
||||
one, err := dao.ForumComments.Ctx(ctx).WherePri(req.Id).One()
|
||||
// 检查评论是否存在(排除已删除的)
|
||||
one, err := dao.ForumComments.Ctx(ctx).
|
||||
WherePri(req.Id).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论失败")
|
||||
}
|
||||
@@ -192,27 +199,26 @@ func (s *sForumComments) Delete(ctx context.Context, req *v1.ForumCommentsDelete
|
||||
return nil, gerror.Wrap(err, "解析评论数据失败")
|
||||
}
|
||||
|
||||
// 软删除评论
|
||||
_, err = dao.ForumComments.Ctx(ctx).WherePri(req.Id).Delete()
|
||||
// 软删除评论:更新 deleted_at 和 status
|
||||
updateData := map[string]interface{}{
|
||||
dao.ForumComments.Columns().DeletedAt: gtime.Now(),
|
||||
dao.ForumComments.Columns().Status: "deleted",
|
||||
}
|
||||
_, err = dao.ForumComments.Ctx(ctx).WherePri(req.Id).Data(updateData).Update()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "删除评论失败")
|
||||
}
|
||||
|
||||
// 更新帖子的评论数
|
||||
// 更新帖子的评论数(减少)
|
||||
if comment.PostId > 0 {
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(comment.PostId).OnDuplicate("comment_count", 1).Update()
|
||||
updatePostData := map[string]interface{}{
|
||||
dao.ForumPosts.Columns().CommentCount: gdb.Raw(fmt.Sprintf("GREATEST(0, %s - 1)", dao.ForumPosts.Columns().CommentCount)),
|
||||
}
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(comment.PostId).Data(updatePostData).Update()
|
||||
if err != nil {
|
||||
g.Log().Warning(ctx, "更新帖子评论数失败:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是回复评论,则更新父评论的回复数
|
||||
if comment.ParentId > 0 {
|
||||
_, err = dao.ForumComments.Ctx(ctx).WherePri(comment.ParentId).OnDuplicate("reply_count", 1).Update()
|
||||
if err != nil {
|
||||
g.Log().Warning(ctx, "更新父评论回复数失败:", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &v1.ForumCommentsDeleteRes{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user