✨ Pinned post
This commit is contained in:
		@@ -17,6 +17,9 @@ type Account struct {
 | 
				
			|||||||
	Reactions    []Reaction `json:"reactions"`
 | 
						Reactions    []Reaction `json:"reactions"`
 | 
				
			||||||
	ExternalID   uint       `json:"external_id"`
 | 
						ExternalID   uint       `json:"external_id"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						PinnedPost   *Post `json:"pinned_post"`
 | 
				
			||||||
 | 
						PinnedPostID *uint `json:"pinned_post_id"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	TotalUpvote   int `json:"total_upvote"`
 | 
						TotalUpvote   int `json:"total_upvote"`
 | 
				
			||||||
	TotalDownvote int `json:"total_downvote"`
 | 
						TotalDownvote int `json:"total_downvote"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
 | 
				
			|||||||
	api := app.Group(baseURL).Name("API")
 | 
						api := app.Group(baseURL).Name("API")
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		api.Get("/users/me", getUserinfo)
 | 
							api.Get("/users/me", getUserinfo)
 | 
				
			||||||
		api.Get("/users/:accountId", getOthersInfo)
 | 
							api.Get("/users/:account", getOthersInfo)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		recommendations := api.Group("/recommendations").Name("Recommendations API")
 | 
							recommendations := api.Group("/recommendations").Name("Recommendations API")
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@@ -33,6 +33,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
 | 
				
			|||||||
			posts.Get("/drafts", listDraftPost)
 | 
								posts.Get("/drafts", listDraftPost)
 | 
				
			||||||
			posts.Get("/:postId", getPost)
 | 
								posts.Get("/:postId", getPost)
 | 
				
			||||||
			posts.Post("/:postId/react", reactPost)
 | 
								posts.Post("/:postId/react", reactPost)
 | 
				
			||||||
 | 
								posts.Post("/:postId/pin", pinPost)
 | 
				
			||||||
			posts.Delete("/:postId", deletePost)
 | 
								posts.Delete("/:postId", deletePost)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			posts.Get("/:postId/replies", listPostReplies)
 | 
								posts.Get("/:postId/replies", listPostReplies)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -161,3 +161,23 @@ func reactPost(c *fiber.Ctx) error {
 | 
				
			|||||||
		return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
 | 
							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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,7 @@ func getUserinfo(c *fiber.Ctx) error {
 | 
				
			|||||||
	var data models.Account
 | 
						var data models.Account
 | 
				
			||||||
	if err := database.C.
 | 
						if err := database.C.
 | 
				
			||||||
		Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
 | 
							Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
 | 
				
			||||||
 | 
							Preload("PinnedPost").
 | 
				
			||||||
		First(&data).Error; err != nil {
 | 
							First(&data).Error; err != nil {
 | 
				
			||||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
							return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -24,11 +25,12 @@ func getUserinfo(c *fiber.Ctx) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getOthersInfo(c *fiber.Ctx) error {
 | 
					func getOthersInfo(c *fiber.Ctx) error {
 | 
				
			||||||
	accountId := c.Params("accountId")
 | 
						account := c.Params("account")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var data models.Account
 | 
						var data models.Account
 | 
				
			||||||
	if err := database.C.
 | 
						if err := database.C.
 | 
				
			||||||
		Where(&models.Account{Name: accountId}).
 | 
							Where(&models.Account{Name: account}).
 | 
				
			||||||
 | 
							Preload("PinnedPost").
 | 
				
			||||||
		First(&data).Error; err != nil {
 | 
							First(&data).Error; err != nil {
 | 
				
			||||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
							return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user