Pavel Sinitsin
cea1f96bb4
Deleted client/credential.go and moved the Credentials struct to the api package for better modularity. Enhanced type safety in api by checking type assertions and added detailed comments in model structs.
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"gitea.24example.ru/spavelit/bpiek/model"
|
|
)
|
|
|
|
var products []model.Product
|
|
var remainsAndPlanresidues []model.ShortProduct
|
|
|
|
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
|
|
}
|
|
|
|
func (a *Api) GetProductByArticle(article string) (model.Product, error) {
|
|
apiResponse, err := a.Client.HTTPClient.
|
|
R().
|
|
SetResult(&model.Product{}).
|
|
Get("client/products/" + article)
|
|
|
|
if err != nil {
|
|
return model.Product{}, err
|
|
}
|
|
|
|
if apiResponse.StatusCode() != 200 {
|
|
return model.Product{}, errors.New(
|
|
"error getting product by article: " + article)
|
|
}
|
|
|
|
product, ok := apiResponse.Result().(*model.Product)
|
|
if !ok {
|
|
return model.Product{}, errors.New("failed to parse product from response")
|
|
}
|
|
|
|
return *product, nil
|
|
}
|
|
|
|
func (a *Api) GetRemainsAndPlanresidues() ([]model.ShortProduct, error) {
|
|
if len(remainsAndPlanresidues) != 0 {
|
|
return remainsAndPlanresidues, nil
|
|
}
|
|
|
|
parentCategories, err := a.GetParentCategories()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, category := range parentCategories {
|
|
apiResponse, err := a.Client.HTTPClient.
|
|
R().
|
|
SetResult(&model.RemainsAndPlanresiduesResponse{}).
|
|
Get("client/category/" + category.Slug + "/balances-json")
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if apiResponse.StatusCode() != 200 {
|
|
return nil, errors.New("file remains and planresidues not found")
|
|
}
|
|
|
|
result := apiResponse.Result().(*model.RemainsAndPlanresiduesResponse)
|
|
|
|
if len(result.Products) != 0 {
|
|
remainsAndPlanresidues = append(remainsAndPlanresidues, result.Products...)
|
|
}
|
|
}
|
|
|
|
return remainsAndPlanresidues, nil
|
|
|
|
}
|