From bb54eaf6623acdcfb2e9056eb803260dff2150a5 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 16 Jul 2025 15:28:11 +0530 Subject: enhance requests queries for posts; integrate backend with search --- controllers/posts.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'controllers/posts.go') diff --git a/controllers/posts.go b/controllers/posts.go index 03535c7..c14d793 100644 --- a/controllers/posts.go +++ b/controllers/posts.go @@ -11,17 +11,40 @@ import ( func PostsPageController(ctx *fiber.Ctx) error { ctx.Locals("Title", config.PT_POST_LIST) - preferences := ctx.Locals("Preferences") - prefs, ok := preferences.(config.SitePreferences) + preferences, ok := ctx.Locals("Preferences").(config.SitePreferences) if !ok { return fiber.NewError(fiber.StatusInternalServerError, "Invalid preferences type") } - posts, err := database.GetPosts(prefs.PostsPerPage) + request, ok := ctx.Locals("Request").(config.Request) + if !ok { + return fiber.NewError(fiber.StatusInternalServerError, "Invalid request type") + } + + queryTags := "" + queryRatings := map[string]bool{} + for _, param := range request.Query { + switch param.Key { + case "tags": + queryTags = param.Value + case "rating": + queryRatings[param.Value] = true + } + } + + if len(queryRatings) == 0 { + for _, rating := range []string{"safe", "questionable", "sensitive"} { + queryRatings[rating] = true + } + } + + posts, err := database.GetPosts(preferences.PostsPerPage) return shortcuts.Render(ctx, config.TEMPLATE_POST_LIST, fiber.Map{ - "Posts": posts, - "Error": err, + "Posts": posts, + "Error": err, + "QueryTags": queryTags, + "QueryRatings": queryRatings, }) } -- cgit v1.2.3