128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
|
|
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
|
||
|
|
}
|