diff --git a/pkg/internal/models/accounts.go b/pkg/internal/models/accounts.go index be54d5a..489b198 100644 --- a/pkg/internal/models/accounts.go +++ b/pkg/internal/models/accounts.go @@ -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"` } diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 842a9d5..9e41d80 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -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) diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 35a092a..14befad 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -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) +} diff --git a/pkg/internal/server/api/users_api.go b/pkg/internal/server/api/users_api.go index e0879d8..23d92d7 100644 --- a/pkg/internal/server/api/users_api.go +++ b/pkg/internal/server/api/users_api.go @@ -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()) }