bpiek/api/products.go
Pavel Sinitsin 8c3541a892
Add tree structure to categories API
Introduced a new method `GetTreeCategories` to provide a hierarchical structure for categories. Fixed typos in variable names and integrated utility functions for converting flat lists to nested dictionaries and vice versa. Added `TreeCategories` model for representing nested category structures.
2024-09-07 21:18:36 +03:00

32 lines
614 B
Go

package api
import "gitea.24example.ru/spavelit/bpiek/model"
var products []model.Product
func (a *Api) GetProducts() ([]model.Product, error) {
if len(products) > 0 {
return products, nil
}
parentCategories, err := a.GetParentCategories()
if err != nil {
return nil, err
}
for _, category := range parentCategories {
categoriesAndProduct, err := a.GetCategoriesAndProductsBySlugParentCategory(category.Slug)
if err != nil {
return nil, err
}
if len(categoriesAndProduct.Products) != 0 {
products = append(products, categoriesAndProduct.Products...)
}
}
return products, nil
}