the meili search endpoint

This commit is contained in:
Daniel Goc
2026-03-24 11:15:09 +01:00
parent bfc488bad8
commit 04538d4373
6 changed files with 188 additions and 44 deletions

View File

@@ -1,6 +1,8 @@
package restricted
import (
"strconv"
"git.ma-al.com/goc_daniel/b2b/app/service/meiliService"
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
@@ -23,8 +25,12 @@ func NewMeiliSearchHandler() *MeiliSearchHandler {
func MeiliSearchHandlerRoutes(r fiber.Router) fiber.Router {
handler := NewMeiliSearchHandler()
r.Get("/test", handler.Test)
// for superadmin only
r.Get("/create-index", handler.CreateIndex)
r.Get("/test", handler.Test)
// for all users
r.Get("/search", handler.Search)
return r
}
@@ -61,3 +67,49 @@ func (h *MeiliSearchHandler) Test(c fiber.Ctx) error {
return c.JSON(response.Make(&test, 0, i18n.T_(c, response.Message_OK)))
}
func (h *MeiliSearchHandler) Search(c fiber.Ctx) error {
id_lang, ok := c.Locals("langID").(uint)
if !ok {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
query := c.Query("query")
limit_attribute := c.Query("limit")
limit, err := strconv.Atoi(limit_attribute)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
id_category_attribute := c.Query("id_category")
id_category, err := strconv.Atoi(id_category_attribute)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
price_lower_bound_attribute := c.Query("price_lower_bound")
price_lower_bound, err := strconv.ParseFloat(price_lower_bound_attribute, 64)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
price_upper_bound_attribute := c.Query("price_upper_bound")
price_upper_bound, err := strconv.ParseFloat(price_upper_bound_attribute, 64)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
}
test, err := h.meiliService.Search(id_lang, query, uint(limit), uint(id_category), price_lower_bound, price_upper_bound)
if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
}
return c.JSON(response.Make(&test, 0, i18n.T_(c, response.Message_OK)))
}