Refactor category variables and client authentication.
Renamed category variables to use underscores for better clarity and consistency. Updated client authentication method to accept username and password directly instead of using a Credentials struct.
This commit is contained in:
parent
8eac71e65c
commit
05b8245f11
@ -26,6 +26,6 @@ type (
|
||||
|
||||
func NewApi(credentials Credentials) Method {
|
||||
return &Api{
|
||||
Client: client.NewClient(credentials),
|
||||
Client: client.NewClient(credentials.Username, credentials.Password),
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,13 @@ import (
|
||||
"gitea.24example.ru/spavelit/bpiek/utils"
|
||||
)
|
||||
|
||||
var categories []model.Category
|
||||
var parentCategories []model.Category
|
||||
var treeCategories []model.TreeCategories
|
||||
var _categories []model.Category
|
||||
var _parentCategories []model.Category
|
||||
var _treeCategories []model.TreeCategories
|
||||
|
||||
func (a *Api) GetCategories() ([]model.Category, error) {
|
||||
if len(categories) > 0 {
|
||||
return categories, nil
|
||||
if len(_categories) > 0 {
|
||||
return _categories, nil
|
||||
}
|
||||
|
||||
parentCategories, err := a.GetParentCategories()
|
||||
@ -22,23 +22,26 @@ func (a *Api) GetCategories() ([]model.Category, error) {
|
||||
}
|
||||
|
||||
for _, category := range parentCategories {
|
||||
|
||||
_categories = append(_categories, category)
|
||||
|
||||
categoriesAndProduct, err := a.GetCategoriesAndProductsBySlugParentCategory(category.Slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(categoriesAndProduct.Categories) != 0 {
|
||||
categories = append(categories, categoriesAndProduct.Categories...)
|
||||
_categories = append(_categories, categoriesAndProduct.Categories...)
|
||||
}
|
||||
}
|
||||
|
||||
return categories, nil
|
||||
return _categories, nil
|
||||
}
|
||||
|
||||
func (a *Api) GetParentCategories() ([]model.Category, error) {
|
||||
|
||||
if len(parentCategories) > 0 {
|
||||
return parentCategories, nil
|
||||
if len(_parentCategories) > 0 {
|
||||
return _parentCategories, nil
|
||||
}
|
||||
|
||||
apiResponse, err := a.Client.HTTPClient.R().SetResult(&model.CategoryResponse{}).Get("client/catalog")
|
||||
@ -56,9 +59,9 @@ func (a *Api) GetParentCategories() ([]model.Category, error) {
|
||||
return nil, errors.New("no categories found")
|
||||
}
|
||||
|
||||
parentCategories = result
|
||||
_parentCategories = result
|
||||
|
||||
return parentCategories, nil
|
||||
return _parentCategories, nil
|
||||
}
|
||||
|
||||
func (a *Api) GetCategoriesAndProductsBySlugParentCategory(slug string) (*model.CategoriesAndProductsBySlugParentCategoryResponse, error) {
|
||||
@ -78,34 +81,20 @@ func (a *Api) GetCategoriesAndProductsBySlugParentCategory(slug string) (*model.
|
||||
}
|
||||
|
||||
func (a *Api) GetTreeCategories() ([]model.TreeCategories, error) {
|
||||
if len(treeCategories) > 0 {
|
||||
return treeCategories, nil
|
||||
if len(_treeCategories) > 0 {
|
||||
return _treeCategories, nil
|
||||
}
|
||||
|
||||
tree := map[string]interface{}{}
|
||||
|
||||
parentCategories, err := a.GetParentCategories()
|
||||
categories, err := a.GetCategories()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, parent := range parentCategories {
|
||||
tree[parent.Slug] = map[string]interface{}{
|
||||
"name": parent.Name,
|
||||
"slug": parent.Slug,
|
||||
"url": parent.Url,
|
||||
"children": map[string]interface{}{},
|
||||
}
|
||||
utils.ConvertListToNestedDict(categories, tree)
|
||||
|
||||
categoriesAndProduct, err := a.GetCategoriesAndProductsBySlugParentCategory(parent.Slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_treeCategories = utils.ConvertToNestedList(tree)
|
||||
|
||||
utils.ConvertListToNestedDict(categoriesAndProduct.Categories, tree)
|
||||
}
|
||||
|
||||
treeCategories = utils.ConvertToNestedList(tree)
|
||||
|
||||
return treeCategories, nil
|
||||
return _treeCategories, nil
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"gitea.24example.ru/spavelit/bpiek/api"
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
@ -25,15 +24,15 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func NewClient(credentials api.Credentials) *Client {
|
||||
func NewClient(username string, password string) *Client {
|
||||
authSuccess := &AuthSuccess{}
|
||||
client := resty.New()
|
||||
client.SetBaseURL(BaseApiUrl)
|
||||
|
||||
response, err := client.R().
|
||||
SetFormData(map[string]string{
|
||||
"username": credentials.Username,
|
||||
"password": credentials.Password,
|
||||
"username": username,
|
||||
"password": password,
|
||||
}).
|
||||
SetResult(&authSuccess).
|
||||
Post(AuthApiUrl)
|
||||
|
Loading…
Reference in New Issue
Block a user