Skip to content

Commit

Permalink
- 后台查询文章列表返回字段新增 is_displayed 和 is_comment_allowed
Browse files Browse the repository at this point in the history
- 新增修改文章展示状态和评论开关的接口
  • Loading branch information
chenmingyong0423 committed Jan 17, 2024
1 parent 9cf9045 commit 3717258
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
8 changes: 8 additions & 0 deletions server/internal/pkg/web/request/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

package request

type PostDisplayReq struct {
IsDisplayed bool `json:"is_displayed"`
}

type PostCommentAllowedReq struct {
IsCommentAllowed bool `json:"is_comment_allowed"`
}

type PostReq struct {
Id string `json:"id"`
Author string `json:"author"`
Expand Down
18 changes: 10 additions & 8 deletions server/internal/pkg/web/vo/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
package vo

type AdminPostVO struct {
Id string `json:"id"`
CoverImg string `json:"cover_img"`
Title string `json:"title"`
Summary string `json:"summary"`
Categories []Category4Post `json:"categories"`
Tags []Tag4Post `json:"tags"`
CreateTime int64 `json:"create_time"`
UpdateTime int64 `json:"update_time"`
Id string `json:"id"`
CoverImg string `json:"cover_img"`
Title string `json:"title"`
Summary string `json:"summary"`
Categories []Category4Post `json:"categories"`
Tags []Tag4Post `json:"tags"`
IsDisplayed bool `json:"is_displayed"`
IsCommentAllowed bool `json:"is_comment_allowed"`
CreateTime int64 `json:"create_time"`
UpdateTime int64 `json:"update_time"`
}

type Category4Post struct {
Expand Down
28 changes: 20 additions & 8 deletions server/internal/post/handler/post_hanlder.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func (h *PostHandler) RegisterGinRoutes(engine *gin.Engine) {
adminGroup.PUT("", api.WrapWithBody(h.AdminUpdatePost))
adminGroup.POST("", api.WrapWithBody(h.AddPost))
adminGroup.DELETE("/:id", api.Wrap(h.DeletePost))
adminGroup.PUT("/:id/display", api.WrapWithBody(h.UpdatePostIsDisplayed))
adminGroup.PUT("/:id/comment-allowed", api.WrapWithBody(h.UpdatePostIsCommentAllowed))
}

func (h *PostHandler) GetLatestPosts(ctx *gin.Context) (listVO api.ListVO[*SummaryPostVO], err error) {
Expand Down Expand Up @@ -198,14 +200,16 @@ func (h *PostHandler) postsToAdminPost(posts []*domain.Post) []vo.AdminPostVO {
}
})
adminPostVOs[i] = vo.AdminPostVO{
Id: post.Id,
CoverImg: post.CoverImg,
Title: post.Title,
Summary: post.Summary,
Categories: categories,
Tags: tags,
CreateTime: post.CreateTime,
UpdateTime: post.UpdateTime,
Id: post.Id,
CoverImg: post.CoverImg,
Title: post.Title,
Summary: post.Summary,
Categories: categories,
Tags: tags,
IsDisplayed: post.IsDisplayed,
IsCommentAllowed: post.IsCommentAllowed,
CreateTime: post.CreateTime,
UpdateTime: post.UpdateTime,
}
}
return adminPostVOs
Expand Down Expand Up @@ -326,3 +330,11 @@ func (h *PostHandler) AdminUpdatePost(ctx *gin.Context, req request.PostReq) (an
Likes: nil,
})
}

func (h *PostHandler) UpdatePostIsDisplayed(ctx *gin.Context, req request.PostDisplayReq) (any, error) {
return nil, h.serv.UpdatePostIsDisplayed(ctx, ctx.Param("id"), req.IsDisplayed)
}

func (h *PostHandler) UpdatePostIsCommentAllowed(ctx *gin.Context, req request.PostCommentAllowedReq) (any, error) {
return nil, h.serv.UpdatePostIsCommentAllowed(ctx, ctx.Param("id"), req.IsCommentAllowed)
}
24 changes: 24 additions & 0 deletions server/internal/post/repository/dao/post_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type IPostDao interface {
FindById(ctx context.Context, id string) (*Post, error)
DecreaseByField(ctx context.Context, id string, filedName string, cnt int) error
SavePost(ctx context.Context, post *Post) error
UpdateIsDisplayedById(ctx context.Context, id string, isDisplayed bool) error
UpdateIsCommentAllowedById(ctx context.Context, id string, isCommentAllowed bool) error
}

var _ IPostDao = (*PostDao)(nil)
Expand All @@ -90,6 +92,28 @@ type PostDao struct {
coll *mongox.Collection[Post]
}

func (d *PostDao) UpdateIsCommentAllowedById(ctx context.Context, id string, isCommentAllowed bool) error {
result, err := d.coll.Updater().Filter(query.Id(id)).Updates(update.BsonBuilder().Set("is_comment_allowed", isCommentAllowed).Set("update_time", time.Now().Unix()).Build()).UpdateOne(ctx)
if err != nil {
return errors.Wrapf(err, "fails to update the is_comment_allowed of post, id=%s, isCommentAllowed=%v", id, isCommentAllowed)
}
if result.ModifiedCount == 0 {
return fmt.Errorf("fails to update the is_comment_allowed of post, id=%s, isCommentAllowed=%v", id, isCommentAllowed)
}
return nil
}

func (d *PostDao) UpdateIsDisplayedById(ctx context.Context, id string, isDisplayed bool) error {
result, err := d.coll.Updater().Filter(query.Id(id)).Updates(update.BsonBuilder().Set("is_displayed", isDisplayed).Set("update_time", time.Now().Unix()).Build()).UpdateOne(ctx)
if err != nil {
return errors.Wrapf(err, "fails to update the is_displayed of post, id=%s, isDisplayed=%v", id, isDisplayed)
}
if result.ModifiedCount == 0 {
return fmt.Errorf("fails to update the is_displayed of post, id=%s, isDisplayed=%v", id, isDisplayed)
}
return nil
}

func (d *PostDao) SavePost(ctx context.Context, post *Post) error {
result, err := d.coll.Updater().Filter(query.Id(post.Id)).UpdatesWithOperator("$set", post).UpdateOne(ctx, options.Update().SetUpsert(true))
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions server/internal/post/repository/post_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type IPostRepository interface {
FindPostById(ctx context.Context, id string) (*domain.Post, error)
DecreaseCommentCount(ctx context.Context, postId string, cnt int) error
SavePost(ctx context.Context, post *domain.Post) error
UpdatePostIsDisplayedById(ctx context.Context, id string, isDisplayed bool) error
UpdatePostIsCommentAllowedById(ctx context.Context, id string, isCommentAllowed bool) error
}

var _ IPostRepository = (*PostRepository)(nil)
Expand All @@ -62,6 +64,14 @@ type PostRepository struct {
dao dao.IPostDao
}

func (r *PostRepository) UpdatePostIsCommentAllowedById(ctx context.Context, id string, isCommentAllowed bool) error {
return r.dao.UpdateIsCommentAllowedById(ctx, id, isCommentAllowed)
}

func (r *PostRepository) UpdatePostIsDisplayedById(ctx context.Context, id string, isDisplayed bool) error {
return r.dao.UpdateIsDisplayedById(ctx, id, isDisplayed)
}

func (r *PostRepository) SavePost(ctx context.Context, post *domain.Post) error {
unix := time.Now().Unix()
categories := r.toDaoCategory4Post(post.Categories)
Expand Down
10 changes: 10 additions & 0 deletions server/internal/post/service/post_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type IPostService interface {
DecreaseCommentCount(ctx context.Context, postId string, cnt int) error
AdminGetPostById(ctx context.Context, id string) (*domain.Post, error)
AdminUpdatePostById(ctx context.Context, savedPost *domain.Post) error
UpdatePostIsDisplayed(ctx context.Context, id string, isDisplayed bool) error
UpdatePostIsCommentAllowed(ctx context.Context, id string, isCommentAllowed bool) error
}

var _ IPostService = (*PostService)(nil)
Expand All @@ -77,6 +79,14 @@ type PostService struct {
ipMap sync.Map
}

func (s *PostService) UpdatePostIsCommentAllowed(ctx context.Context, id string, isCommentAllowed bool) error {
return s.repo.UpdatePostIsCommentAllowedById(ctx, id, isCommentAllowed)
}

func (s *PostService) UpdatePostIsDisplayed(ctx context.Context, id string, isDisplayed bool) error {
return s.repo.UpdatePostIsDisplayedById(ctx, id, isDisplayed)
}

func (s *PostService) AdminUpdatePostById(ctx context.Context, savedPost *domain.Post) error {
post, err := s.repo.FindPostById(ctx, savedPost.Id)
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
Expand Down

0 comments on commit 3717258

Please sign in to comment.