103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
package restricted
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/service/menuService"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
|
|
"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 MenuHandler struct {
|
|
menuService *menuService.MenuService
|
|
}
|
|
|
|
func NewMenuHandler() *MenuHandler {
|
|
menuService := menuService.New()
|
|
return &MenuHandler{
|
|
menuService: menuService,
|
|
}
|
|
}
|
|
|
|
func MenuHandlerRoutes(r fiber.Router) fiber.Router {
|
|
handler := NewMenuHandler()
|
|
|
|
r.Get("/get-category-tree", handler.GetCategoryTree)
|
|
r.Get("/get-breadcrumb", handler.GetBreadcrumb)
|
|
r.Get("/get-top-menu", handler.GetTopMenu)
|
|
|
|
return r
|
|
}
|
|
|
|
func (h *MenuHandler) GetCategoryTree(c fiber.Ctx) error {
|
|
lang_id, ok := localeExtractor.GetLangID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
root_category_id_attribute := c.Query("root_category_id")
|
|
root_category_id, err := strconv.Atoi(root_category_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
category_tree, err := h.menuService.GetCategoryTree(uint(root_category_id), lang_id)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(&category_tree, 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
func (h *MenuHandler) GetBreadcrumb(c fiber.Ctx) error {
|
|
lang_id, ok := localeExtractor.GetLangID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
root_category_id_attribute := c.Query("root_category_id")
|
|
root_category_id, err := strconv.Atoi(root_category_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
category_id_attribute := c.Query("category_id")
|
|
category_id, err := strconv.Atoi(category_id_attribute)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
|
|
breadcrumb, err := h.menuService.GetBreadcrumb(uint(root_category_id), uint(category_id), lang_id)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(&breadcrumb, 0, i18n.T_(c, response.Message_OK)))
|
|
}
|
|
|
|
func (h *MenuHandler) GetTopMenu(c fiber.Ctx) error {
|
|
lang_id, ok := localeExtractor.GetLangID(c)
|
|
if !ok {
|
|
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute)))
|
|
}
|
|
menu, err := h.menuService.GetTopMenu(lang_id)
|
|
if err != nil {
|
|
return c.Status(responseErrors.GetErrorStatus(err)).
|
|
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
|
}
|
|
|
|
return c.JSON(response.Make(&menu, len(menu), i18n.T_(c, response.Message_OK)))
|
|
}
|