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/home.go | 9 ++++++++- controllers/posts.go | 33 ++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 6 deletions(-) (limited to 'controllers') diff --git a/controllers/home.go b/controllers/home.go index 9fd0d33..dfa5d13 100644 --- a/controllers/home.go +++ b/controllers/home.go @@ -9,5 +9,12 @@ import ( func HomePageController(ctx *fiber.Ctx) error { ctx.Locals("Title", config.PT_HOME) - return shortcuts.Render(ctx, config.TEMPLATE_HOME, nil) + queryRatings := map[string]bool{ + "safe": true, + "questionable": true, + "sensitive": true, + } + return shortcuts.Render(ctx, config.TEMPLATE_HOME, fiber.Map{ + "QueryRatings": queryRatings, + }) } 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