fix
This commit is contained in:
@@ -31,6 +31,7 @@ func RegisterControllers(group *ghttp.RouterGroup) {
|
||||
containment.NewV1(),
|
||||
room.NewV1(),
|
||||
forum.NewV1(),
|
||||
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
35
Backend/internal/controller/forum/boards.go
Normal file
35
Backend/internal/controller/forum/boards.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package forum
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"TrangleAgent/internal/service"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// ForumBoardsCreate 创建版块
|
||||
func (c *ControllerV1) ForumBoardsCreate(ctx context.Context, req *v1.ForumBoardsCreateReq) (res *v1.ForumBoardsCreateRes, err error) {
|
||||
return service.ForumBoards().Create(ctx, req)
|
||||
}
|
||||
|
||||
// ForumBoardsUpdate 更新版块
|
||||
func (c *ControllerV1) ForumBoardsUpdate(ctx context.Context, req *v1.ForumBoardsUpdateReq) (res *v1.ForumBoardsUpdateRes, err error) {
|
||||
return service.ForumBoards().Update(ctx, req)
|
||||
}
|
||||
|
||||
// ForumBoardsDelete 删除版块
|
||||
func (c *ControllerV1) ForumBoardsDelete(ctx context.Context, req *v1.ForumBoardsDeleteReq) (res *v1.ForumBoardsDeleteRes, err error) {
|
||||
return service.ForumBoards().Delete(ctx, req)
|
||||
}
|
||||
|
||||
// ForumBoardsView 查看版块
|
||||
func (c *ControllerV1) ForumBoardsView(ctx context.Context, req *v1.ForumBoardsViewReq) (res *v1.ForumBoardsViewRes, err error) {
|
||||
return service.ForumBoards().View(ctx, req)
|
||||
}
|
||||
|
||||
// ForumBoardsList 获取版块列表
|
||||
func (c *ControllerV1) ForumBoardsList(ctx context.Context, req *v1.ForumBoardsListReq) (res *v1.ForumBoardsListRes, err error) {
|
||||
return service.ForumBoards().List(ctx, req)
|
||||
}
|
||||
11
Backend/internal/controller/forum/forum.go
Normal file
11
Backend/internal/controller/forum/forum.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package forum
|
||||
|
||||
import (
|
||||
"TrangleAgent/api/forum"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewV1() forum.IForumV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package forum
|
||||
|
||||
import (
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"TrangleAgent/internal/service"
|
||||
"context"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewV1() *ControllerV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
|
||||
// ForumPostsCreate 创建帖子
|
||||
func (c *ControllerV1) Create(ctx context.Context, req *v1.ForumPostsCreateReq) (res *v1.ForumPostsCreateRes, err error) {
|
||||
return service.ForumPosts().Create(ctx, req)
|
||||
}
|
||||
|
||||
// ForumPostsUpdate 更新帖子
|
||||
func (c *ControllerV1) Update(ctx context.Context, req *v1.ForumPostsUpdateReq) (res *v1.ForumPostsUpdateRes, err error) {
|
||||
return service.ForumPosts().Update(ctx, req)
|
||||
}
|
||||
|
||||
// ForumPostsDelete 删除帖子
|
||||
func (c *ControllerV1) Delete(ctx context.Context, req *v1.ForumPostsDeleteReq) (res *v1.ForumPostsDeleteRes, err error) {
|
||||
return service.ForumPosts().Delete(ctx, req)
|
||||
}
|
||||
|
||||
// ForumPostsView 查看帖子详情
|
||||
func (c *ControllerV1) View(ctx context.Context, req *v1.ForumPostsViewReq) (res *v1.ForumPostsViewRes, err error) {
|
||||
return service.ForumPosts().View(ctx, req)
|
||||
}
|
||||
|
||||
// ForumPostsList 获取帖子列表
|
||||
func (c *ControllerV1) List(ctx context.Context, req *v1.ForumPostsListReq) (res *v1.ForumPostsListRes, err error) {
|
||||
return service.ForumPosts().List(ctx, req)
|
||||
}
|
||||
28
Backend/internal/controller/forum/posts.go
Normal file
28
Backend/internal/controller/forum/posts.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package forum
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"TrangleAgent/internal/service"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) ForumPostsCreate(ctx context.Context, req *v1.ForumPostsCreateReq) (res *v1.ForumPostsCreateRes, err error) {
|
||||
return service.ForumPosts().Create(ctx, req)
|
||||
}
|
||||
|
||||
func (c *ControllerV1) ForumPostsUpdate(ctx context.Context, req *v1.ForumPostsUpdateReq) (res *v1.ForumPostsUpdateRes, err error) {
|
||||
return service.ForumPosts().Update(ctx, req)
|
||||
}
|
||||
|
||||
func (c *ControllerV1) ForumPostsDelete(ctx context.Context, req *v1.ForumPostsDeleteReq) (res *v1.ForumPostsDeleteRes, err error) {
|
||||
return service.ForumPosts().Delete(ctx, req)
|
||||
}
|
||||
|
||||
func (c *ControllerV1) ForumPostsView(ctx context.Context, req *v1.ForumPostsViewReq) (res *v1.ForumPostsViewRes, err error) {
|
||||
return service.ForumPosts().View(ctx, req)
|
||||
}
|
||||
|
||||
func (c *ControllerV1) ForumPostsList(ctx context.Context, req *v1.ForumPostsListReq) (res *v1.ForumPostsListRes, err error) {
|
||||
return service.ForumPosts().List(ctx, req)
|
||||
}
|
||||
31
Backend/internal/logic/Forum/boards.go
Normal file
31
Backend/internal/logic/Forum/boards.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package Forum
|
||||
|
||||
import (
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"context"
|
||||
)
|
||||
|
||||
type sForumBoards struct{}
|
||||
|
||||
func NewForumBoards() *sForumBoards {
|
||||
return &sForumBoards{}
|
||||
}
|
||||
|
||||
func (s *sForumBoards) Create(ctx context.Context, req *v1.ForumBoardsCreateReq) (res *v1.ForumBoardsCreateRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *sForumBoards) Update(ctx context.Context, req *v1.ForumBoardsUpdateReq) (res *v1.ForumBoardsUpdateRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *sForumBoards) Delete(ctx context.Context, req *v1.ForumBoardsDeleteReq) (res *v1.ForumBoardsDeleteRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *sForumBoards) View(ctx context.Context, req *v1.ForumBoardsViewReq) (res *v1.ForumBoardsViewRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *sForumBoards) List(ctx context.Context, req *v1.ForumBoardsListReq) (res *v1.ForumBoardsListRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
31
Backend/internal/logic/Forum/comments.go
Normal file
31
Backend/internal/logic/Forum/comments.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package Forum
|
||||
|
||||
import (
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"context"
|
||||
)
|
||||
|
||||
type sForumComments struct{}
|
||||
|
||||
func NewForumComments() *sForumComments {
|
||||
return &sForumComments{}
|
||||
}
|
||||
|
||||
func (s *sForumComments) Create(ctx context.Context, req *v1.ForumCommentsCreateReq) (res *v1.ForumCommentsCreateRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *sForumComments) Update(ctx context.Context, req *v1.ForumCommentsUpdateReq) (res *v1.ForumCommentsUpdateRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *sForumComments) Delete(ctx context.Context, req *v1.ForumCommentsDeleteReq) (res *v1.ForumCommentsDeleteRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *sForumComments) View(ctx context.Context, req *v1.ForumCommentsViewReq) (res *v1.ForumCommentsViewRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (s *sForumComments) List(ctx context.Context, req *v1.ForumCommentsListReq) (res *v1.ForumCommentsListRes, err error) {
|
||||
return nil, nil
|
||||
}
|
||||
127
Backend/internal/logic/Forum/post.go
Normal file
127
Backend/internal/logic/Forum/post.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package Forum
|
||||
|
||||
import (
|
||||
v1 "TrangleAgent/api/forum/v1"
|
||||
"TrangleAgent/internal/dao"
|
||||
"TrangleAgent/internal/model"
|
||||
"TrangleAgent/internal/service"
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// sForumPosts 帖子逻辑
|
||||
type sForumPosts struct{}
|
||||
|
||||
func NewForumPosts() *sForumPosts {
|
||||
return &sForumPosts{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterForumPosts(NewForumPosts())
|
||||
}
|
||||
|
||||
func (s *sForumPosts) Create(ctx context.Context, req *v1.ForumPostsCreateReq) (res *v1.ForumPostsCreateRes, err error) {
|
||||
|
||||
//校验参数
|
||||
if len(req.Title) < 5 || len(req.Title) > 100 {
|
||||
return nil, gerror.New("标题长度要在5-100之间")
|
||||
}
|
||||
if len(req.Content) < 5 || len(req.Content) > 10000 {
|
||||
return nil, gerror.New("内容长度要在5-10000之间")
|
||||
}
|
||||
if req.BoardId == 0 {
|
||||
return nil, gerror.New("板块ID不能为空")
|
||||
}
|
||||
|
||||
//创建帖子
|
||||
insert := model.ForumPost{
|
||||
BoardId: req.BoardId,
|
||||
UserId: req.UserId,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
CoverImage: req.CoverImage,
|
||||
Status: req.Status,
|
||||
CreatedAt: gtime.Now(),
|
||||
UpdatedAt: gtime.Now(),
|
||||
}
|
||||
result, err := dao.ForumPosts.Ctx(ctx).Data(insert).Insert()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "创建帖子失败")
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "获取帖子ID失败")
|
||||
}
|
||||
|
||||
return &v1.ForumPostsCreateRes{Id: uint64(id)}, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *sForumPosts) Update(ctx context.Context, req *v1.ForumPostsUpdateReq) (res *v1.ForumPostsUpdateRes, err error) {
|
||||
//校验参数
|
||||
if len(req.Title) < 5 || len(req.Title) > 100 {
|
||||
return nil, gerror.New("标题长度要在5-100之间")
|
||||
}
|
||||
if len(req.Content) < 5 || len(req.Content) > 10000 {
|
||||
return nil, gerror.New("内容长度要在5-10000之间")
|
||||
}
|
||||
//只能修改 帖子标题 帖子正文 帖子封面图URL
|
||||
fields := []string{"title", "content", "cover_image"}
|
||||
//更新帖子
|
||||
_, err = dao.ForumPosts.Ctx(ctx).Data(model.ForumPost{
|
||||
Id: req.Id,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
CoverImage: req.CoverImage,
|
||||
}).Fields(fields).OmitEmpty().Update()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "更新帖子失败")
|
||||
}
|
||||
|
||||
return &v1.ForumPostsUpdateRes{Id: req.Id}, nil
|
||||
}
|
||||
|
||||
func (s *sForumPosts) Delete(ctx context.Context, req *v1.ForumPostsDeleteReq) (res *v1.ForumPostsDeleteRes, err error) {
|
||||
//根据id删除帖子
|
||||
_, err = dao.ForumPosts.Ctx(ctx).Where(dao.ForumPosts.Columns().Id, req.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "删除帖子失败")
|
||||
}
|
||||
return &v1.ForumPostsDeleteRes{}, nil
|
||||
}
|
||||
|
||||
func (s *sForumPosts) View(ctx context.Context, req *v1.ForumPostsViewReq) (res *v1.ForumPostsViewRes, err error) {
|
||||
// 根据id查询帖子
|
||||
var post model.ForumPostViewParams
|
||||
err = dao.ForumPosts.Ctx(ctx).Where(dao.ForumPosts.Columns().Id, req.Id).Scan(&post)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子失败")
|
||||
}
|
||||
return &v1.ForumPostsViewRes{
|
||||
ForumPostViewParams: post,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sForumPosts) List(ctx context.Context, req *v1.ForumPostsListReq) (res *v1.ForumPostsListRes, err error) {
|
||||
// 查询帖子列表
|
||||
|
||||
mod := dao.ForumPosts.Ctx(ctx)
|
||||
|
||||
if req.Title != "" {
|
||||
mod = mod.WhereLike(dao.ForumPosts.Columns().Title, "%"+req.Title+"%")
|
||||
}
|
||||
if req.BoardId != 0 {
|
||||
mod = mod.Where(dao.ForumPosts.Columns().BoardId, req.BoardId)
|
||||
}
|
||||
var list []*model.ForumPostViewParams
|
||||
err = mod.Scan(&list)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子列表失败")
|
||||
}
|
||||
return &v1.ForumPostsListRes{
|
||||
List: list,
|
||||
}, nil
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
package ForumComments
|
||||
|
||||
import (
|
||||
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 评论相关方法
|
||||
type sForumComments struct{}
|
||||
|
||||
func init() {
|
||||
service.RegisterForumComments(&sForumComments{})
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
|
||||
result, err := dao.ForumComments.Ctx(ctx).Data(data).OmitEmpty().Insert()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "创建评论失败")
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "获取评论ID失败")
|
||||
}
|
||||
|
||||
// 更新帖子的评论数
|
||||
if req.PostId > 0 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return &v1.ForumCommentsCreateRes{
|
||||
Id: uint64(id),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 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).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论失败")
|
||||
}
|
||||
if one.IsEmpty() {
|
||||
return nil, errors.New("评论不存在")
|
||||
}
|
||||
|
||||
// 更新评论
|
||||
_, err = dao.ForumComments.Ctx(ctx).WherePri(req.Id).Data(req).OmitEmpty().Update()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "更新评论失败")
|
||||
}
|
||||
|
||||
return &v1.ForumCommentsUpdateRes{
|
||||
Id: req.Id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 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).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论失败")
|
||||
}
|
||||
if record.IsEmpty() {
|
||||
return nil, errors.New("评论不存在")
|
||||
}
|
||||
|
||||
var entity *entity.ForumComments
|
||||
err = record.Struct(&entity)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "解析评论数据失败")
|
||||
}
|
||||
|
||||
res = &v1.ForumCommentsViewRes{
|
||||
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
|
||||
}
|
||||
|
||||
// ForumCommentsList 评论列表
|
||||
func (s *sForumComments) List(ctx context.Context, req *v1.ForumCommentsListReq) (res *v1.ForumCommentsListRes, err error) {
|
||||
query := dao.ForumComments.Ctx(ctx).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
OmitEmpty()
|
||||
|
||||
if req.UserId > 0 {
|
||||
query = query.Where(dao.ForumComments.Columns().UserId, req.UserId)
|
||||
}
|
||||
if req.PostId > 0 {
|
||||
query = query.Where(dao.ForumComments.Columns().PostId, req.PostId)
|
||||
}
|
||||
if req.ParentId > 0 {
|
||||
query = query.Where(dao.ForumComments.Columns().ParentId, req.ParentId)
|
||||
}
|
||||
if req.Status != "" {
|
||||
query = query.Where(dao.ForumComments.Columns().Status, req.Status)
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
pageResult, err := query.Page(req.Page, req.PageSize).OrderDesc(dao.ForumComments.Columns().CreatedAt).All()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论列表失败")
|
||||
}
|
||||
|
||||
var list []*v1.ForumCommentsViewRes
|
||||
for _, record := range pageResult {
|
||||
var entity *entity.ForumComments
|
||||
err = record.Struct(&entity)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "解析评论数据失败")
|
||||
}
|
||||
|
||||
item := &v1.ForumCommentsViewRes{
|
||||
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)
|
||||
}
|
||||
|
||||
total, err := query.Count()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "统计评论总数失败")
|
||||
}
|
||||
|
||||
res = &v1.ForumCommentsListRes{}
|
||||
res.List = list
|
||||
res.Page = req.Page
|
||||
res.PageSize = req.PageSize
|
||||
res.Total = total
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// 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).
|
||||
WhereNull(dao.ForumComments.Columns().DeletedAt).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询评论失败")
|
||||
}
|
||||
if one.IsEmpty() {
|
||||
return nil, errors.New("评论不存在")
|
||||
}
|
||||
|
||||
var comment *entity.ForumComments
|
||||
err = one.Struct(&comment)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "解析评论数据失败")
|
||||
}
|
||||
|
||||
// 软删除评论:更新 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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return &v1.ForumCommentsDeleteRes{}, nil
|
||||
}
|
||||
@@ -1,186 +0,0 @@
|
||||
package ForumComments
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"TrangleAgent/api/forum/v1"
|
||||
"TrangleAgent/internal/dao"
|
||||
"TrangleAgent/internal/model/entity"
|
||||
"TrangleAgent/internal/service"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type sForumPosts struct{}
|
||||
|
||||
func init() {
|
||||
service.RegisterForumPosts(&sForumPosts{})
|
||||
}
|
||||
|
||||
// ForumPostsCreate 创建帖子
|
||||
func (s *sForumPosts) Create(ctx context.Context, req *v1.ForumPostsCreateReq) (res *v1.ForumPostsCreateRes, err error) {
|
||||
data := &entity.ForumPosts{
|
||||
UserId: req.UserId,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
CoverImage: req.CoverImage,
|
||||
Status: req.Status,
|
||||
ViewCount: 0,
|
||||
LikeCount: 0,
|
||||
CommentCount: 0,
|
||||
}
|
||||
|
||||
result, err := dao.ForumPosts.Ctx(ctx).Data(data).OmitEmpty().Insert()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "创建帖子失败")
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "获取帖子ID失败")
|
||||
}
|
||||
|
||||
return &v1.ForumPostsCreateRes{
|
||||
Id: uint64(id),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ForumPostsUpdate 更新帖子
|
||||
func (s *sForumPosts) Update(ctx context.Context, req *v1.ForumPostsUpdateReq) (res *v1.ForumPostsUpdateRes, err error) {
|
||||
// 检查帖子是否存在
|
||||
one, err := dao.ForumPosts.Ctx(ctx).WherePri(req.Id).One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子失败")
|
||||
}
|
||||
if one.IsEmpty() {
|
||||
return nil, errors.New("帖子不存在")
|
||||
}
|
||||
|
||||
// 更新帖子
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(req.Id).Data(req).OmitEmpty().Update()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "更新帖子失败")
|
||||
}
|
||||
|
||||
return &v1.ForumPostsUpdateRes{
|
||||
Id: req.Id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ForumPostsView 查看帖子
|
||||
func (s *sForumPosts) View(ctx context.Context, req *v1.ForumPostsViewReq) (res *v1.ForumPostsViewRes, err error) {
|
||||
record, err := dao.ForumPosts.Ctx(ctx).WherePri(req.Id).One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子失败")
|
||||
}
|
||||
if record.IsEmpty() {
|
||||
return nil, errors.New("帖子不存在")
|
||||
}
|
||||
|
||||
// 更新浏览量
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(req.Id).OnDuplicate("view_count", "view_count+1").Update()
|
||||
if err != nil {
|
||||
g.Log().Warning(ctx, "更新浏览量失败:", err)
|
||||
}
|
||||
|
||||
var entity *entity.ForumPosts
|
||||
err = record.Struct(&entity)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "解析帖子数据失败")
|
||||
}
|
||||
|
||||
res = &v1.ForumPostsViewRes{
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
Title: entity.Title,
|
||||
Content: entity.Content,
|
||||
CoverImage: entity.CoverImage,
|
||||
Status: entity.Status,
|
||||
ViewCount: entity.ViewCount,
|
||||
LikeCount: entity.LikeCount,
|
||||
CommentCount: entity.CommentCount,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ForumPostsList 帖子列表
|
||||
func (s *sForumPosts) List(ctx context.Context, req *v1.ForumPostsListReq) (res *v1.ForumPostsListRes, err error) {
|
||||
query := dao.ForumPosts.Ctx(ctx).OmitEmpty()
|
||||
|
||||
if req.UserId > 0 {
|
||||
query = query.Where("user_id", req.UserId)
|
||||
}
|
||||
if req.Title != "" {
|
||||
query = query.WhereLike("title", "%"+req.Title+"%")
|
||||
}
|
||||
if req.Status != "" {
|
||||
query = query.Where("status", req.Status)
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
pageResult, err := query.Page(req.Page, req.PageSize).Order("created_at DESC").All()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子列表失败")
|
||||
}
|
||||
|
||||
var list []*v1.ForumPostsViewRes
|
||||
for _, record := range pageResult {
|
||||
var entity *entity.ForumPosts
|
||||
err = record.Struct(&entity)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "解析帖子数据失败")
|
||||
}
|
||||
|
||||
item := &v1.ForumPostsViewRes{
|
||||
Id: entity.Id,
|
||||
UserId: entity.UserId,
|
||||
Title: entity.Title,
|
||||
Content: entity.Content,
|
||||
CoverImage: entity.CoverImage,
|
||||
Status: entity.Status,
|
||||
ViewCount: entity.ViewCount,
|
||||
LikeCount: entity.LikeCount,
|
||||
CommentCount: entity.CommentCount,
|
||||
CreatedAt: entity.CreatedAt,
|
||||
UpdatedAt: entity.UpdatedAt,
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
total, err := query.Count()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "统计帖子总数失败")
|
||||
}
|
||||
|
||||
res = &v1.ForumPostsListRes{}
|
||||
res.List = list
|
||||
res.PageResult.Page = req.Page
|
||||
res.PageResult.PageSize = req.PageSize
|
||||
res.PageResult.Total = total
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ForumPostsDelete 删除帖子
|
||||
func (s *sForumPosts) Delete(ctx context.Context, req *v1.ForumPostsDeleteReq) (res *v1.ForumPostsDeleteRes, err error) {
|
||||
// 检查帖子是否存在
|
||||
one, err := dao.ForumPosts.Ctx(ctx).WherePri(req.Id).One()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子失败")
|
||||
}
|
||||
if one.IsEmpty() {
|
||||
return nil, errors.New("帖子不存在")
|
||||
}
|
||||
|
||||
// 软删除帖子
|
||||
_, err = dao.ForumPosts.Ctx(ctx).WherePri(req.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "删除帖子失败")
|
||||
}
|
||||
|
||||
return &v1.ForumPostsDeleteRes{}, nil
|
||||
}
|
||||
2
Backend/internal/logic/chat/dispatchRoom.go
Normal file
2
Backend/internal/logic/chat/dispatchRoom.go
Normal file
@@ -0,0 +1,2 @@
|
||||
package chat
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
_ "TrangleAgent/internal/logic/ForumComments"
|
||||
_ "TrangleAgent/internal/logic/Forum"
|
||||
_ "TrangleAgent/internal/logic/containment"
|
||||
_ "TrangleAgent/internal/logic/department"
|
||||
_ "TrangleAgent/internal/logic/login"
|
||||
|
||||
109
Backend/internal/model/forum.go
Normal file
109
Backend/internal/model/forum.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// ForumBoard 论坛版块
|
||||
type ForumBoard struct {
|
||||
Id uint64 `json:"id" orm:"id" description:"版块ID"`
|
||||
SectionId uint64 `json:"sectionId" orm:"section_id" description:"所属频道ID"`
|
||||
Name string `json:"name" orm:"name" description:"版块名称"`
|
||||
Description string `json:"description" orm:"description" description:"版块简介"`
|
||||
CoverImage string `json:"coverImage" orm:"cover_image" description:"版块封面图URL"`
|
||||
Status string `json:"status" orm:"status" description:"版块状态"`
|
||||
DisplayOrder int `json:"displayOrder" orm:"display_order" description:"排序值"`
|
||||
PostCount uint `json:"postCount" orm:"post_count" description:"帖子总数"`
|
||||
TodayPostCount uint `json:"todayPostCount" orm:"today_post_count" description:"今日发帖数"`
|
||||
LastPostId uint64 `json:"lastPostId" orm:"last_post_id" description:"最后一篇帖子ID"`
|
||||
LastPostAt *gtime.Time `json:"lastPostAt" orm:"last_post_at" description:"最后发帖时间"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
// ForumBoardViewParams 版块查看参数
|
||||
type ForumBoardViewParams struct {
|
||||
Id uint64 `json:"id" description:"版块ID"`
|
||||
SectionId uint64 `json:"sectionId" description:"所属频道ID"`
|
||||
Name string `json:"name" description:"版块名称"`
|
||||
Description string `json:"description" description:"版块简介"`
|
||||
CoverImage string `json:"coverImage" description:"版块封面图URL"`
|
||||
Status string `json:"status" description:"版块状态"`
|
||||
DisplayOrder int `json:"displayOrder" description:"排序值"`
|
||||
PostCount uint `json:"postCount" description:"帖子总数"`
|
||||
TodayPostCount uint `json:"todayPostCount" description:"今日发帖数"`
|
||||
LastPostId uint64 `json:"lastPostId" description:"最后一篇帖子ID"`
|
||||
LastPostAt *gtime.Time `json:"lastPostAt" description:"最后发帖时间"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" description:"创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" description:"更新时间"`
|
||||
}
|
||||
|
||||
// ForumPost 论坛帖子
|
||||
type ForumPost struct {
|
||||
Id uint64 `json:"id" orm:"id" description:"帖子ID"`
|
||||
BoardId uint64 `json:"boardId" orm:"board_id" description:"所属版块ID"`
|
||||
UserId uint64 `json:"userId" orm:"user_id" description:"发帖用户ID"`
|
||||
Title string `json:"title" orm:"title" description:"帖子标题"`
|
||||
Content string `json:"content" orm:"content" description:"帖子正文"`
|
||||
CoverImage string `json:"coverImage" orm:"cover_image" description:"帖子封面图URL"`
|
||||
Status string `json:"status" orm:"status" description:"帖子状态"`
|
||||
IsPinned int `json:"isPinned" orm:"is_pinned" description:"是否置顶"`
|
||||
IsEssence int `json:"isEssence" orm:"is_essence" description:"是否精华"`
|
||||
ViewCount uint `json:"viewCount" orm:"view_count" description:"浏览量"`
|
||||
LikeCount uint `json:"likeCount" orm:"like_count" description:"点赞数"`
|
||||
CommentCount uint `json:"commentCount" orm:"comment_count" description:"评论数"`
|
||||
CollectCount uint `json:"collectCount" orm:"collect_count" description:"收藏数"`
|
||||
LastCommentId uint64 `json:"lastCommentId" orm:"last_comment_id" description:"最后一条评论ID"`
|
||||
LastCommentAt *gtime.Time `json:"lastCommentAt" orm:"last_comment_at" description:"最后评论时间"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"发帖时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
// ForumPostViewParams 帖子查看参数
|
||||
type ForumPostViewParams struct {
|
||||
Id uint64 `json:"id" description:"帖子ID"`
|
||||
BoardId uint64 `json:"boardId" description:"所属版块ID"`
|
||||
UserId uint64 `json:"userId" description:"发帖用户ID"`
|
||||
Title string `json:"title" description:"帖子标题"`
|
||||
Content string `json:"content" description:"帖子正文"`
|
||||
CoverImage string `json:"coverImage" description:"帖子封面图URL"`
|
||||
Status string `json:"status" description:"帖子状态"`
|
||||
IsPinned int `json:"isPinned" description:"是否置顶"`
|
||||
IsEssence int `json:"isEssence" description:"是否精华"`
|
||||
ViewCount uint `json:"viewCount" description:"浏览量"`
|
||||
LikeCount uint `json:"likeCount" description:"点赞数"`
|
||||
CommentCount uint `json:"commentCount" description:"评论数"`
|
||||
CollectCount uint `json:"collectCount" description:"收藏数"`
|
||||
LastCommentId uint64 `json:"lastCommentId" description:"最后一条评论ID"`
|
||||
LastCommentAt *gtime.Time `json:"lastCommentAt" description:"最后评论时间"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" description:"发帖时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" description:"更新时间"`
|
||||
}
|
||||
|
||||
// ForumComment 论坛评论
|
||||
type ForumComment struct {
|
||||
Id uint64 `json:"id" orm:"id" description:"评论ID"`
|
||||
PostId uint64 `json:"postId" orm:"post_id" description:"所属帖子ID"`
|
||||
UserId uint64 `json:"userId" orm:"user_id" description:"评论发布者ID"`
|
||||
ParentId uint64 `json:"parentId" orm:"parent_id" description:"父评论ID"`
|
||||
ReplyToUserId uint64 `json:"replyToUserId" orm:"reply_to_user_id" description:"回复的用户ID"`
|
||||
Content string `json:"content" orm:"content" description:"评论内容"`
|
||||
Status string `json:"status" orm:"status" description:"评论状态"`
|
||||
LikeCount uint `json:"likeCount" orm:"like_count" description:"点赞数"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" orm:"created_at" description:"创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" orm:"updated_at" description:"更新时间"`
|
||||
}
|
||||
|
||||
// ForumCommentViewParams 评论查看参数
|
||||
type ForumCommentViewParams struct {
|
||||
Id uint64 `json:"id" description:"评论ID"`
|
||||
UserId uint64 `json:"userId" description:"评论发布者ID"`
|
||||
PostId uint64 `json:"postId" description:"所属帖子ID"`
|
||||
ParentId uint64 `json:"parentId" description:"父评论ID"`
|
||||
ReplyToUserId uint64 `json:"replyToUserId" description:"回复的用户ID"`
|
||||
Content string `json:"content" description:"评论内容"`
|
||||
Status string `json:"status" description:"评论状态"`
|
||||
LikeCount uint `json:"likeCount" description:"点赞数"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" description:"评论创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" description:"评论更新时间"`
|
||||
}
|
||||
@@ -11,37 +11,46 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
IForumBoards interface {
|
||||
Create(ctx context.Context, req *v1.ForumBoardsCreateReq) (res *v1.ForumBoardsCreateRes, err error)
|
||||
Update(ctx context.Context, req *v1.ForumBoardsUpdateReq) (res *v1.ForumBoardsUpdateRes, err error)
|
||||
Delete(ctx context.Context, req *v1.ForumBoardsDeleteReq) (res *v1.ForumBoardsDeleteRes, err error)
|
||||
View(ctx context.Context, req *v1.ForumBoardsViewReq) (res *v1.ForumBoardsViewRes, err error)
|
||||
List(ctx context.Context, req *v1.ForumBoardsListReq) (res *v1.ForumBoardsListRes, err error)
|
||||
}
|
||||
IForumComments interface {
|
||||
// ForumCommentsCreate 创建评论
|
||||
Create(ctx context.Context, req *v1.ForumCommentsCreateReq) (res *v1.ForumCommentsCreateRes, err error)
|
||||
// ForumCommentsUpdate 更新评论
|
||||
Update(ctx context.Context, req *v1.ForumCommentsUpdateReq) (res *v1.ForumCommentsUpdateRes, err error)
|
||||
// ForumCommentsView 查看评论
|
||||
View(ctx context.Context, req *v1.ForumCommentsViewReq) (res *v1.ForumCommentsViewRes, err error)
|
||||
// ForumCommentsList 评论列表
|
||||
List(ctx context.Context, req *v1.ForumCommentsListReq) (res *v1.ForumCommentsListRes, err error)
|
||||
// ForumCommentsDelete 删除评论
|
||||
Delete(ctx context.Context, req *v1.ForumCommentsDeleteReq) (res *v1.ForumCommentsDeleteRes, err error)
|
||||
View(ctx context.Context, req *v1.ForumCommentsViewReq) (res *v1.ForumCommentsViewRes, err error)
|
||||
List(ctx context.Context, req *v1.ForumCommentsListReq) (res *v1.ForumCommentsListRes, err error)
|
||||
}
|
||||
IForumPosts interface {
|
||||
// ForumPostsCreate 创建帖子
|
||||
Create(ctx context.Context, req *v1.ForumPostsCreateReq) (res *v1.ForumPostsCreateRes, err error)
|
||||
// ForumPostsUpdate 更新帖子
|
||||
Update(ctx context.Context, req *v1.ForumPostsUpdateReq) (res *v1.ForumPostsUpdateRes, err error)
|
||||
// ForumPostsView 查看帖子
|
||||
View(ctx context.Context, req *v1.ForumPostsViewReq) (res *v1.ForumPostsViewRes, err error)
|
||||
// ForumPostsList 帖子列表
|
||||
List(ctx context.Context, req *v1.ForumPostsListReq) (res *v1.ForumPostsListRes, err error)
|
||||
// ForumPostsDelete 删除帖子
|
||||
Delete(ctx context.Context, req *v1.ForumPostsDeleteReq) (res *v1.ForumPostsDeleteRes, err error)
|
||||
View(ctx context.Context, req *v1.ForumPostsViewReq) (res *v1.ForumPostsViewRes, err error)
|
||||
List(ctx context.Context, req *v1.ForumPostsListReq) (res *v1.ForumPostsListRes, err error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localForumBoards IForumBoards
|
||||
localForumComments IForumComments
|
||||
localForumPosts IForumPosts
|
||||
)
|
||||
|
||||
func ForumBoards() IForumBoards {
|
||||
if localForumBoards == nil {
|
||||
panic("implement not found for interface IForumBoards, forgot register?")
|
||||
}
|
||||
return localForumBoards
|
||||
}
|
||||
|
||||
func RegisterForumBoards(i IForumBoards) {
|
||||
localForumBoards = i
|
||||
}
|
||||
|
||||
func ForumComments() IForumComments {
|
||||
if localForumComments == nil {
|
||||
panic("implement not found for interface IForumComments, forgot register?")
|
||||
Reference in New Issue
Block a user