diff options
| author | Bobby <[email protected]> | 2025-07-18 17:07:23 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-07-18 17:07:23 +0530 |
| commit | aa0405ee98c45a9bb25dd9959d899bbd56bc1b02 (patch) | |
| tree | c6b75124708f3a3ab5fecbdb454eb5f530dd2ffa /controllers | |
| parent | 821773b12c07a4bc23628e7d98ac4b34da1eb9e1 (diff) | |
| download | imageboard-aa0405ee98c45a9bb25dd9959d899bbd56bc1b02.tar.xz imageboard-aa0405ee98c45a9bb25dd9959d899bbd56bc1b02.zip | |
favourite system and ∂etails on single page
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/posts.go | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/controllers/posts.go b/controllers/posts.go index 11525b8..41bbc86 100644 --- a/controllers/posts.go +++ b/controllers/posts.go @@ -299,9 +299,51 @@ func PostsSinglePostPageController(ctx *fiber.Ctx) error { return renderSinglePostError(ctx, "Failed to retrieve post. "+err.Error(), fiber.StatusInternalServerError) } + currentUser := auth.GetCurrentUser(ctx) + isUserFavourited := false + if currentUser != nil { + isUserFavourited = post.IsUserFavourited(database.DB, currentUser) + } + ctx.Locals("Title", config.PT_POST_SINGLE+" #"+format.Int64ToString(int64(post.ID))) return shortcuts.Render(ctx, config.TEMPLATE_POST_SINGLE, fiber.Map{ - "Post": post, - "CDNURL": format.GetCDNURL(), + "Post": post, + "CDNURL": format.GetCDNURL(), + "IsUserFavourited": isUserFavourited, }) } + +func PostsSinglePostFavouriteController(ctx *fiber.Ctx) error { + if !auth.IsAuthenticated(ctx) { + return ctx.Redirect(auth.GetLoginURLWithNextField(ctx), fiber.StatusFound) + } + + postID := ctx.Params("id") + if postID == "" { + return renderSinglePostError(ctx, "Post ID is required", fiber.StatusBadRequest) + } + + uintPostID, err := format.StringToUint(postID) + if err != nil { + return renderSinglePostError(ctx, "Invalid Post ID", fiber.StatusBadRequest) + } + + post, err := database.GetPostByID(uintPostID) + if err != nil { + if err.Error() == "record not found" { + return renderSinglePostError(ctx, "Post not found", fiber.StatusNotFound) + } + return renderSinglePostError(ctx, "Failed to retrieve post. "+err.Error(), fiber.StatusInternalServerError) + } + + currentUser := auth.GetCurrentUser(ctx) + if currentUser == nil { + return renderSinglePostError(ctx, "User not found", fiber.StatusUnauthorized) + } + + if err := post.ToggleFavourite(database.DB, currentUser); err != nil { + return renderSinglePostError(ctx, "Failed to toggle favourite. "+err.Error(), fiber.StatusInternalServerError) + } + + return ctx.Redirect("/posts/" + postID) +} |
