summaryrefslogtreecommitdiff
path: root/internal/router/handlers/api-v1-like-get.go
diff options
from:
to:
context:
space:
mode:
authorGravatar Saya Andy <145215889+SayaAndy@users.noreply.github.com> 2025-10-28 23:15:23 +0700
committerGravatar GitHub <noreply@github.com> 2025-10-28 23:15:23 +0700
commitbda4faeeed4d9859cfe8afd7952a751683d74bd8 (patch)
tree464c18fed0c21b63b65425f48a3a790229461058 /internal/router/handlers/api-v1-like-get.go
parentdd69b45d3a41be2b5c38892c3b107770a0f859f2 (diff)
parent1862f232eb70105340a91e74a5630c233a411bca (diff)
downloadweb-0.11.1.tar.gz
web-0.11.1.zip
v0.11.1 (#18)v0.11.1
- refactor: make structurized implementation of routes + separate identity of router - feat: update go to 1.25 - feat: update all dependencies - debug: display routes when debug logs turned on
Diffstat (limited to 'internal/router/handlers/api-v1-like-get.go')
-rw-r--r--internal/router/handlers/api-v1-like-get.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/internal/router/handlers/api-v1-like-get.go b/internal/router/handlers/api-v1-like-get.go
new file mode 100644
index 0000000..e9c176e
--- /dev/null
+++ b/internal/router/handlers/api-v1-like-get.go
@@ -0,0 +1,63 @@
+package handlers
+
+import (
+ "fmt"
+ "log/slog"
+
+ "github.com/SayaAndy/saya-today-web/internal/router"
+ "github.com/gofiber/fiber/v2"
+)
+
+type GetLikeHandler struct {
+ router.BasicHandler
+}
+
+func init() {
+ router.Routes = append(router.Routes, &GetLikeHandler{})
+}
+
+func (r *GetLikeHandler) Filter() (method string, path string) {
+ return "GET", "/api/v1/like"
+}
+
+func (r *GetLikeHandler) IsTemplated() bool {
+ return false
+}
+
+func (r *GetLikeHandler) TemplatesToInject() []string {
+ return []string{"views/partials/blog-page-like-button.html"}
+}
+
+func (r *GetLikeHandler) ToCache() router.CacheSetting {
+ return router.Disabled
+}
+
+func (r *GetLikeHandler) ToValidateLang() router.LangSetting {
+ return router.InReferer
+}
+
+func (r *GetLikeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
+ path, pathParts, _, err := router.GetPathFromReferer(c)
+ if err != nil {
+ return fiber.StatusBadRequest, fmt.Errorf("error getting path from referer: %w", err)
+ }
+
+ if len(pathParts) != 3 && pathParts[1] != "blog" {
+ return fiber.StatusBadRequest, fmt.Errorf("invalid path format: expected '/:lang/blog/:page', got '%s'", path)
+ }
+
+ page := pathParts[2]
+ pageLink := lang + "/" + page + ".md"
+ if pages, _ := supplements.B2Client.Scan(pageLink); len(pages) == 0 {
+ return fiber.StatusNotFound, fmt.Errorf("server did not find '%s' article", pageLink)
+ }
+
+ ip := c.IP()
+ likeStatus := supplements.ClientCache.GetLikeStatus(ip, page)
+
+ slog.Debug("someone requested the like status!", slog.String("ip", ip), slog.String("page", page), slog.Bool("like_status", likeStatus))
+ templateMap["Liked"] = likeStatus
+ templateMap["LikedCount"] = supplements.ClientCache.GetLikeCount(page)
+
+ return fiber.StatusOK, nil
+}