Pinned post

This commit is contained in:
LittleSheep 2024-07-25 22:45:31 +08:00
parent c8d55d0b2c
commit 761d73100b
4 changed files with 29 additions and 3 deletions

View File

@ -17,6 +17,9 @@ type Account struct {
Reactions []Reaction `json:"reactions"`
ExternalID uint `json:"external_id"`
PinnedPost *Post `json:"pinned_post"`
PinnedPostID *uint `json:"pinned_post_id"`
TotalUpvote int `json:"total_upvote"`
TotalDownvote int `json:"total_downvote"`
}

View File

@ -8,7 +8,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
api := app.Group(baseURL).Name("API")
{
api.Get("/users/me", getUserinfo)
api.Get("/users/:accountId", getOthersInfo)
api.Get("/users/:account", getOthersInfo)
recommendations := api.Group("/recommendations").Name("Recommendations API")
{
@ -33,6 +33,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
posts.Get("/drafts", listDraftPost)
posts.Get("/:postId", getPost)
posts.Post("/:postId/react", reactPost)
posts.Post("/:postId/pin", pinPost)
posts.Delete("/:postId", deletePost)
posts.Get("/:postId/replies", listPostReplies)

View File

@ -161,3 +161,23 @@ func reactPost(c *fiber.Ctx) error {
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
}
}
func pinPost(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var res models.Post
if err := database.C.Where("id = ? AND author_id = ?", c.Params("postId"), user.ID).First(&res).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post in your posts to pin: %v", err))
}
user.PinnedPostID = &res.ID
if err := database.C.Save(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("failed to save changes: %v", err))
}
return c.SendStatus(fiber.StatusOK)
}

View File

@ -16,6 +16,7 @@ func getUserinfo(c *fiber.Ctx) error {
var data models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
Preload("PinnedPost").
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
@ -24,11 +25,12 @@ func getUserinfo(c *fiber.Ctx) error {
}
func getOthersInfo(c *fiber.Ctx) error {
accountId := c.Params("accountId")
account := c.Params("account")
var data models.Account
if err := database.C.
Where(&models.Account{Name: accountId}).
Where(&models.Account{Name: account}).
Preload("PinnedPost").
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}