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" "git.ma-al.com/goc_daniel/b2b/app/utils/response" "git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors" "github.com/gofiber/fiber/v3" ) type MeiliSearchHandler struct { meiliService *meiliService.MeiliService } func NewMeiliSearchHandler() *MeiliSearchHandler { meiliService := meiliService.New() return &MeiliSearchHandler{ meiliService: meiliService, } } func MeiliSearchHandlerRoutes(r fiber.Router) fiber.Router { handler := NewMeiliSearchHandler() // for superadmin only r.Get("/create-index", handler.CreateIndex) r.Get("/test", handler.Test) // for all users r.Get("/search", handler.Search) return r } func (h *MeiliSearchHandler) CreateIndex(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))) } err := h.meiliService.CreateIndex(id_lang) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } nothing := "" return c.JSON(response.Make(¬hing, 0, i18n.T_(c, response.Message_OK))) } func (h *MeiliSearchHandler) Test(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))) } test, err := h.meiliService.Test(id_lang) 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))) } 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))) }