Post own page

This commit is contained in:
2024-02-06 01:10:22 +08:00
parent d3adb20b0e
commit bbdc8e6aa6
11 changed files with 206 additions and 24 deletions

View File

@ -48,6 +48,42 @@ func listPost(c *fiber.Ctx) error {
})
}
func getPost(c *fiber.Ctx) error {
id, _ := c.ParamsInt("postId", 0)
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
var post models.Post
if err := services.PreloadRelatedPost(database.C.Where(&models.Post{
BaseModel: models.BaseModel{ID: uint(id)},
})).First(&post).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
tx := database.C.
Where(&models.Post{ReplyID: &post.ID}).
Where("published_at <= ? OR published_at IS NULL", time.Now()).
Order("created_at desc")
var count int64
if err := tx.
Model(&models.Post{}).
Count(&count).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
posts, err := services.ListPost(tx, take, offset)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(fiber.Map{
"data": post,
"count": count,
"related": posts,
})
}
func createPost(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)

View File

@ -69,6 +69,7 @@ func NewServer() {
api.Post("/attachments", auth, uploadAttachment)
api.Get("/posts", listPost)
api.Get("/posts/:postId", getPost)
api.Post("/posts", auth, createPost)
api.Post("/posts/:postId/react/:reactType", auth, reactPost)
api.Put("/posts/:postId", auth, editPost)