recommender/internal/handler/http/controller/application_v1/posts.go
2024-11-10 03:49:53 +08:00

208 lines
5.8 KiB
Go

package application_v1
import (
"github.com/gin-gonic/gin"
"github.com/iVampireSP/pkg/page"
"leafdev.top/Ecosystem/recommender/internal/entity"
"leafdev.top/Ecosystem/recommender/internal/handler/http/request"
"leafdev.top/Ecosystem/recommender/internal/handler/http/response"
"leafdev.top/Ecosystem/recommender/internal/service/application"
"leafdev.top/Ecosystem/recommender/internal/service/auth"
"leafdev.top/Ecosystem/recommender/internal/service/category"
"leafdev.top/Ecosystem/recommender/internal/service/post"
"leafdev.top/Ecosystem/recommender/pkg/consts"
"net/http"
)
type PostController struct {
authService *auth.Service
applicationService *application.Service
postService *post.Service
categoryService *category.Service
}
func NewPostController(
authService *auth.Service,
applicationService *application.Service,
postService *post.Service,
categoryService *category.Service,
) *PostController {
return &PostController{
authService,
applicationService,
postService,
categoryService,
}
}
// List godoc
// @Summary 列出 Posts
// @Description 列出 Posts
// @Tags application_api
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} response.ResponseBody{data=page.PagedResult[entity.Post]}
// @Failure 400 {object} response.ResponseBody
// @Router /applications/v1/posts [get]
func (pc *PostController) List(c *gin.Context) {
app, err := pc.authService.GetApplication(c)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
var pagedResult = page.NewPagedResult[*entity.Post]()
err = pc.postService.ListPosts(c, pagedResult, nil, app)
response.Ctx(c).Data(pagedResult.Output()).Send()
return
}
// Save godoc
// @Summary 新建资源
// @Description 新建资源
// @Tags application_api
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param PostSaveRequest body request.PostSaveRequest true "body"
// @Success 200 {object} response.ResponseBody{data=entity.Post}
// @Failure 400 {object} response.ResponseBody
// @Router /applications/v1/posts [post]
func (pc *PostController) Save(c *gin.Context) {
app, err := pc.authService.GetApplication(c)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
var postSaveRequest = &request.PostSaveRequest{}
if err := c.ShouldBindJSON(postSaveRequest); err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
categoryEntity, err := pc.categoryService.GetCategoryById(c, postSaveRequest.CategoryId)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
if categoryEntity.ApplicationId != app.Id {
response.Ctx(c).Status(http.StatusNotFound).Send()
return
}
exists, err := pc.postService.TargetIdExists(c, app, postSaveRequest.TargetId)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
if exists {
response.Ctx(c).Status(http.StatusConflict).Error(consts.ErrPostTargetIdExists).Send()
return
}
var postEntity = &entity.Post{
Title: postSaveRequest.Title,
Content: postSaveRequest.Content,
TargetId: postSaveRequest.TargetId,
ApplicationId: app.Id,
CategoryId: &categoryEntity.Id,
Processed: false,
}
err = pc.postService.CreatePost(c, app, postEntity)
response.Ctx(c).Error(err).Data(postEntity).Send()
return
}
// Delete godoc
// @Summary 删除 Post
// @Description 删除 Post
// @Tags application_api
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param postIdRequest path request.PostIdRequest true "post_id"
// @Success 200 {object} response.ResponseBody{data=entity.Post}
// @Failure 400 {object} response.ResponseBody
// @Router /applications/v1/post/{post_id} [delete]
func (pc *PostController) Delete(c *gin.Context) {
app, err := pc.authService.GetApplication(c)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
var postIdRequest = &request.PostIdRequest{}
if err := c.ShouldBindUri(postIdRequest); err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
postEntity, err := pc.postService.GetPostById(c, postIdRequest.PostId)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
if postEntity.ApplicationId != app.Id {
response.Ctx(c).Status(http.StatusForbidden).Send()
return
}
err = pc.postService.DeletePost(c, postEntity)
if err != nil {
response.Ctx(c).Error(err).Send()
}
response.Ctx(c).Status(http.StatusNoContent).Send()
return
}
// Get godoc
// @Summary 获取 Post
// @Description 获取 Post
// @Tags application_api
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param postIdRequest path request.PostIdRequest true "post_id"
// @Success 200 {object} response.ResponseBody{data=entity.Post}
// @Failure 400 {object} response.ResponseBody
// @Router /applications/v1/post/{post_id} [get]
func (pc *PostController) Get(c *gin.Context) {
app, err := pc.authService.GetApplication(c)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
var postIdRequest = &request.PostIdRequest{}
if err := c.ShouldBindUri(postIdRequest); err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
postEntity, err := pc.postService.GetPostById(c, postIdRequest.PostId)
if err != nil {
response.Ctx(c).Error(err).Status(http.StatusBadRequest).Send()
return
}
if postEntity.ApplicationId != app.Id {
response.Ctx(c).Status(http.StatusForbidden).Send()
return
}
response.Ctx(c).Data(postEntity).Send()
return
}