From 73cc936d56833971f4b0f7b471227df11c586183 Mon Sep 17 00:00:00 2001 From: Marek Goc Date: Tue, 24 Mar 2026 14:39:13 +0100 Subject: [PATCH] fix products listing --- app/api/openapi.json | 4 +- app/config/config.go | 9 +++ .../web/api/restricted/listProducts.go | 55 ------------------- app/model/product.go | 4 +- .../listProductsRepo/listProductsRepo.go | 21 ++----- .../listProductsService.go | 30 ---------- app/utils/query/find/find.go | 4 +- app/utils/query/query_params/params_query.go | 3 + 8 files changed, 22 insertions(+), 108 deletions(-) diff --git a/app/api/openapi.json b/app/api/openapi.json index fd09b1c..37420fe 100644 --- a/app/api/openapi.json +++ b/app/api/openapi.json @@ -1052,7 +1052,7 @@ ], "parameters": [ { - "name": "page", + "name": "p", "in": "query", "description": "Page number (1-based)", "required": false, @@ -1062,7 +1062,7 @@ } }, { - "name": "limit", + "name": "elems", "in": "query", "description": "Number of items per page", "required": false, diff --git a/app/config/config.go b/app/config/config.go index 2f9a1b7..e5be538 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -22,6 +22,7 @@ type Config struct { I18n I18n Pdf PdfPrinter GoogleTranslate GoogleTranslateConfig + Image ImageConfig } type I18n struct { @@ -32,6 +33,10 @@ type ServerConfig struct { Host string `env:"SERVER_HOST,0.0.0.0"` } +type ImageConfig struct { + ImagePrefix string `env:"IMAGE_PREFIX"` +} + type DatabaseConfig struct { Host string `env:"DB_HOST,localhost"` Port int `env:"DB_PORT"` @@ -167,6 +172,10 @@ func load() *Config { slog.Error("not possible to load env variables for google translate : ", err.Error(), "") } + err = loadEnv(&cfg.Image) + if err != nil { + slog.Error("not possible to load env variables for google translate : ", err.Error(), "") + } return cfg } diff --git a/app/delivery/web/api/restricted/listProducts.go b/app/delivery/web/api/restricted/listProducts.go index 85317a6..2478cc5 100644 --- a/app/delivery/web/api/restricted/listProducts.go +++ b/app/delivery/web/api/restricted/listProducts.go @@ -11,8 +11,6 @@ import ( "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. @@ -45,11 +43,6 @@ func (h *ListProductsHandler) GetListing(c fiber.Ctx) error { 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", ""), - // } - id_lang, ok := c.Locals("langID").(uint) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). @@ -67,60 +60,12 @@ func (h *ListProductsHandler) GetListing(c fiber.Ctx) error { 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) -} diff --git a/app/model/product.go b/app/model/product.go index 3c8faae..36b0d70 100644 --- a/app/model/product.go +++ b/app/model/product.go @@ -62,9 +62,9 @@ type Product struct { DeliveryDays uint `gorm:"column:delivery_days" json:"delivery_days" form:"delivery_days"` } type ProductInList struct { - ProductID uint `gorm:"column:ID;primaryKey" json:"product_id" form:"product_id"` + ProductID uint `gorm:"column:product_id;primaryKey" json:"product_id" form:"product_id"` Name string `gorm:"column:name" json:"name" form:"name"` - ImageID uint `gorm:"column:id_image"` + ImageID string `gorm:"column:id_image"` LinkRewrite string `gorm:"column:link_rewrite"` Active uint `gorm:"column:active" json:"active" form:"active"` } diff --git a/app/repos/listProductsRepo/listProductsRepo.go b/app/repos/listProductsRepo/listProductsRepo.go index 539b6fc..bd9fda3 100644 --- a/app/repos/listProductsRepo/listProductsRepo.go +++ b/app/repos/listProductsRepo/listProductsRepo.go @@ -1,6 +1,7 @@ package listProductsRepo import ( + "git.ma-al.com/goc_daniel/b2b/app/config" "git.ma-al.com/goc_daniel/b2b/app/db" "git.ma-al.com/goc_daniel/b2b/app/model" constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data" @@ -22,20 +23,6 @@ func (repo *ListProductsRepo) GetListing(id_lang uint, p find.Paging, filt *filt var listing []model.ProductInList var total int64 - // var resultIDs []uint - // q := db.DB. - // // SQL_CALC_FOUND_ROWS is a neat trick which works on MariaDB and - // // MySQL. It works when followed by `SELECT FOUND_ROWS();`. To learn - // // more see: https://mariarawmodel.com/kb/en/found_rows/ - // // WARN: This might not work on different SQL databases - // Select("DISTINCT SQL_CALC_FOUND_ROWS id"). - // // Debug(). - // Scopes(view.FromDBViewForDisplay(langID, countryIso)). - // Scopes(scopesForFiltersOnDisplay(db.DB, langID, countryIso, filt)). - // Scopes(filt.OfCategory(filters.ORDER_FILTER)...). - // Limit(p.Limit()). - // Offset(p.Offset()) - subQuery := db.DB. Table("ps_image"). Select("id_product, MIN(id_image) AS id_image"). @@ -44,12 +31,12 @@ func (repo *ListProductsRepo) GetListing(id_lang uint, p find.Paging, filt *filt err := db.DB. Table("ps_product"). Select(` - ps_product.id_product AS id, + ps_product.id_product AS product_id, ps_product_lang.name AS name, ps_product.active AS active, ps_product_lang.link_rewrite AS link_rewrite, - COALESCE(ps_image_shop.id_image, any_image.id_image) AS id_image - `). + COALESCE(CONCAT( ?, '/', ps_image_shop.id_image, '-small_default/', ps_product_lang.link_rewrite, '.webp'), CONCAT( ?, '/', any_image.id_image, '-small_default/', ps_product_lang.link_rewrite, '.webp')) AS id_image + `, config.Get().Image.ImagePrefix, config.Get().Image.ImagePrefix). Joins(` LEFT JOIN ps_product_lang ON ps_product_lang.id_product = ps_product.id_product diff --git a/app/service/listProductsService/listProductsService.go b/app/service/listProductsService/listProductsService.go index 93b5ddc..b173a0e 100644 --- a/app/service/listProductsService/listProductsService.go +++ b/app/service/listProductsService/listProductsService.go @@ -20,40 +20,10 @@ func New() *ListProductsService { func (s *ListProductsService) GetListing(id_lang uint, p find.Paging, filters *filters.FiltersList) (find.Found[model.ProductInList], error) { var products find.Found[model.ProductInList] - // currencyIso := c.Cookies("currency_iso", "") - // countryIso := c.Cookies("country_iso", "") - - // if overrides["override_currency"] != "" { - // currencyIso = overrides["override_currency"] - // } - // if overrides["override_country"] != "" { - // countryIso = overrides["override_country"] - // } - products, err := s.listProductsRepo.GetListing(id_lang, p, filters) if err != nil { return products, err } - // var loopErr error - // parallel.ForEach(products.Items, func(t model.Product, i int) { - // // products.Items[i].PriceTaxed *= currRate.Rate.InexactFloat64() - // // products.Items[i].PriceTaxed = tiny_util.RoundUpMonetary(products.Items[i].PriceTaxed) - - // if products.Items[i].Name.IsNull() { - // translation, err := s.listProductsRepo.GetTranslation(ctx, products.Items[i].ID, defaults.DefaultLanguageID) - // if err != nil { - // loopErr = err - // return - // } - // products.Items[i].Name = nullable.FromPrimitiveString(translation.Name) - // products.Items[i].DescriptionShort = nullable.FromPrimitiveString(translation.DescriptionShort) - // products.Items[i].LinkRewrite = nullable.FromPrimitiveString(translation.LinkRewrite) - // } - // }) - // if loopErr != nil { - // return products, errs.Handled(span, loopErr, errs.InternalError, errs.ERR_TODO) - // } - return products, nil } diff --git a/app/utils/query/find/find.go b/app/utils/query/find/find.go index 6f27c6e..7d810ec 100644 --- a/app/utils/query/find/find.go +++ b/app/utils/query/find/find.go @@ -10,8 +10,8 @@ import ( ) type Paging struct { - Page uint `json:"page_number" example:"5"` - Elements uint `json:"elements_per_page" example:"30"` + Page uint `json:"p" example:"5"` + Elements uint `json:"elems" example:"30"` } func (p Paging) Offset() int { diff --git a/app/utils/query/query_params/params_query.go b/app/utils/query/query_params/params_query.go index 9f5c64a..9362657 100644 --- a/app/utils/query/query_params/params_query.go +++ b/app/utils/query/query_params/params_query.go @@ -59,5 +59,8 @@ func ParseFieldFilters[T any](c fiber.Ctx, formColumnMapping ...map[string]strin func ParsePagination(c fiber.Ctx) (uint, uint) { pageNum, _ := strconv.ParseInt(c.Query("p", "1"), 10, 64) pageSize, _ := strconv.ParseInt(c.Query("elems", "30"), 10, 64) + if pageSize > 100 { + pageSize = 100 + } return uint(pageNum), uint(pageSize) }