new endpoint to return product list
This commit is contained in:
120
app/delivery/web/api/restricted/listProducts.go
Normal file
120
app/delivery/web/api/restricted/listProducts.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package restricted
|
||||
|
||||
import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/config"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/service/listProductsService"
|
||||
"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/query/filters"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/query_params"
|
||||
"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"
|
||||
"github.com/samber/lo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ListProductsHandler handles endpoints that receive, save and translate product descriptions.
|
||||
type ListProductsHandler struct {
|
||||
listProductsService *listProductsService.ListProductsService
|
||||
config *config.Config
|
||||
}
|
||||
|
||||
// NewListProductsHandler creates a new ListProductsHandler instance
|
||||
func NewListProductsHandler() *ListProductsHandler {
|
||||
listProductsService := listProductsService.New()
|
||||
return &ListProductsHandler{
|
||||
listProductsService: listProductsService,
|
||||
config: config.Get(),
|
||||
}
|
||||
}
|
||||
|
||||
func ListProductsHandlerRoutes(r fiber.Router) fiber.Router {
|
||||
handler := NewListProductsHandler()
|
||||
|
||||
r.Get("/get-listing", handler.GetListing)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *ListProductsHandler) GetListing(c fiber.Ctx) error {
|
||||
paging, filters, err := ParseProductFilters(c)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
// overrides := map[string]string{
|
||||
// "override_country": c.Query("override_country", ""),
|
||||
// "override_currency": c.Query("override_currency", ""),
|
||||
// }
|
||||
|
||||
listing, err := h.listProductsService.GetListing(paging, filters)
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(&listing.Items, int(listing.Count), i18n.T_(c, response.Message_OK)))
|
||||
}
|
||||
|
||||
var columnMapping map[string]string = map[string]string{}
|
||||
|
||||
// var columnMapping map[string]string = map[string]string{
|
||||
// "product_id": "id",
|
||||
// "price": "price_taxed",
|
||||
// "name": "name",
|
||||
// "category_id": "category_id",
|
||||
// "feature_id": "feature_id",
|
||||
// "feature": "feature_name",
|
||||
// "value_id": "value_id",
|
||||
// "value": "value_name",
|
||||
// "status": "active_sale",
|
||||
// "stock": "in_stock",
|
||||
// }
|
||||
|
||||
func ParseProductFilters(c fiber.Ctx) (find.Paging, *filters.FiltersList, error) {
|
||||
var p find.Paging
|
||||
fl := filters.NewFiltersList()
|
||||
// productFilters := new(model.ProductFilters)
|
||||
|
||||
// err := c.Bind().Query(productFilters)
|
||||
// if err != nil {
|
||||
// return p, &fl, err
|
||||
// }
|
||||
|
||||
// if productFilters.Name != "" {
|
||||
// fl.Append(filters.Where("name LIKE ?", fmt.Sprintf("%%%s%%", productFilters.Name)))
|
||||
// }
|
||||
|
||||
// if productFilters.Sort != "" {
|
||||
// ord, err := query_params.ParseOrdering[model.Product](c, columnMapping)
|
||||
// if err != nil {
|
||||
// return p, &fl, err
|
||||
// }
|
||||
// for _, o := range ord {
|
||||
// fl.Append(filters.Order(o.Column, o.IsDesc))
|
||||
// }
|
||||
// }
|
||||
|
||||
// if len(productFilters.Features) > 0 {
|
||||
// fl.Append(featureValueFilters(productFilters.Features))
|
||||
// }
|
||||
|
||||
// fl.Append(query_params.ParseWhereScopes[model.Product](c, []string{"name"}, columnMapping)...)
|
||||
|
||||
pageNum, pageElems := query_params.ParsePagination(c)
|
||||
p = find.Paging{Page: pageNum, Elements: pageElems}
|
||||
|
||||
return p, &fl, nil
|
||||
}
|
||||
|
||||
type FeatVal = map[uint][]uint
|
||||
|
||||
func featureValueFilters(feats FeatVal) filters.Filter {
|
||||
filt := func(db *gorm.DB) *gorm.DB {
|
||||
return db.Where("value_id IN ?", lo.Flatten(lo.Values(feats))).Group("id").Having("COUNT(id) = ?", len(lo.Keys(feats)))
|
||||
}
|
||||
return filters.NewFilter(filters.FEAT_VAL_PRODUCT_FILTER, filt)
|
||||
}
|
||||
@@ -70,9 +70,8 @@ func (h *ProductDescriptionHandler) GetProductDescription(c fiber.Ctx) error {
|
||||
|
||||
description, err := h.productDescriptionService.GetProductDescription(userID, uint(productID), uint(productShopID), uint(productLangID))
|
||||
if err != nil {
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).JSON(fiber.Map{
|
||||
"error": responseErrors.GetErrorCode(c, err),
|
||||
})
|
||||
return c.Status(responseErrors.GetErrorStatus(err)).
|
||||
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))
|
||||
}
|
||||
|
||||
return c.JSON(response.Make(description, 1, i18n.T_(c, response.Message_OK)))
|
||||
|
||||
@@ -89,10 +89,14 @@ func (s *Server) Setup() error {
|
||||
auth := s.public.Group("/auth")
|
||||
public.AuthHandlerRoutes(auth)
|
||||
|
||||
// Repo routes (restricted)
|
||||
// product description routes (restricted)
|
||||
productDescription := s.restricted.Group("/product-description")
|
||||
restricted.ProductDescriptionHandlerRoutes(productDescription)
|
||||
|
||||
// listing products routes (restricted)
|
||||
listProducts := s.restricted.Group("/list-products")
|
||||
restricted.ListProductsHandlerRoutes(listProducts)
|
||||
|
||||
// // Restricted routes example
|
||||
// restricted := s.api.Group("/restricted")
|
||||
// restricted.Use(middleware.AuthMiddleware())
|
||||
|
||||
Reference in New Issue
Block a user