完善论坛模块 评论css展示
This commit is contained in:
@@ -54,6 +54,7 @@ type ForumPostsViewReq struct {
|
||||
|
||||
// ForumPostsViewRes 查看帖子响应
|
||||
type ForumPostsViewRes struct {
|
||||
UserName string `json:"name" description:"发帖用户昵称"`
|
||||
model.ForumPostViewParams
|
||||
}
|
||||
|
||||
|
||||
@@ -30,9 +30,6 @@ func (s *sForumPosts) Create(ctx context.Context, req *v1.ForumPostsCreateReq) (
|
||||
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不能为空")
|
||||
}
|
||||
@@ -107,7 +104,17 @@ func (s *sForumPosts) View(ctx context.Context, req *v1.ForumPostsViewReq) (res
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子失败")
|
||||
}
|
||||
// 查询用户名称
|
||||
type User struct {
|
||||
Name string `json:"name" orm:"nickname" description:"用户昵称"`
|
||||
}
|
||||
var user User
|
||||
err = dao.Users.Ctx(ctx).Where(dao.Users.Columns().Id, post.UserId).Scan(&user)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询用户昵称失败")
|
||||
}
|
||||
return &v1.ForumPostsViewRes{
|
||||
UserName: user.Name,
|
||||
ForumPostViewParams: post,
|
||||
}, nil
|
||||
}
|
||||
@@ -159,6 +166,46 @@ func (s *sForumPosts) List(ctx context.Context, req *v1.ForumPostsListReq) (res
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询帖子列表失败")
|
||||
}
|
||||
// 查询用户名称
|
||||
if len(list) > 0 {
|
||||
type User struct {
|
||||
Id uint64 `json:"id" orm:"id"`
|
||||
Name string `json:"name" orm:"nickname"`
|
||||
}
|
||||
var users []User
|
||||
|
||||
// 构造去重后的用户 ID 切片
|
||||
idSet := make(map[uint64]struct{}, len(list))
|
||||
for _, item := range list {
|
||||
idSet[item.UserId] = struct{}{}
|
||||
}
|
||||
|
||||
userIds := make([]uint64, 0, len(idSet))
|
||||
for id := range idSet {
|
||||
userIds = append(userIds, id)
|
||||
}
|
||||
|
||||
err = dao.Users.Ctx(ctx).
|
||||
Fields(dao.Users.Columns().Id, dao.Users.Columns().Nickname).
|
||||
Where(dao.Users.Columns().Id, userIds).
|
||||
Scan(&users)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "查询用户名称失败")
|
||||
}
|
||||
|
||||
// 建立 userId -> name 映射
|
||||
userMap := make(map[uint64]string, len(users))
|
||||
for _, user := range users {
|
||||
userMap[user.Id] = user.Name
|
||||
}
|
||||
|
||||
// 关联用户名称
|
||||
for _, item := range list {
|
||||
if name, ok := userMap[item.UserId]; ok {
|
||||
item.Name = name
|
||||
}
|
||||
}
|
||||
}
|
||||
return &v1.ForumPostsListRes{
|
||||
PageResult: response.PageResult{
|
||||
Total: total,
|
||||
|
||||
@@ -58,6 +58,7 @@ type ForumPostViewParams struct {
|
||||
Id uint64 `json:"id" description:"帖子ID"`
|
||||
BoardId uint64 `json:"boardId" description:"所属版块ID"`
|
||||
UserId uint64 `json:"userId" description:"发帖用户ID"`
|
||||
Name string `json:"name" description:"发帖用户名称"`
|
||||
Title string `json:"title" description:"帖子标题"`
|
||||
Content string `json:"content" description:"帖子正文"`
|
||||
CoverImage string `json:"coverImage" description:"帖子封面图URL"`
|
||||
|
||||
Reference in New Issue
Block a user