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 {
|
func NewApi(credentials Credentials) Method {
|
||||||
return &Api{
|
return &Api{
|
||||||
Client: client.NewClient(credentials),
|
Client: client.NewClient(credentials.Username, credentials.Password),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,13 @@ import (
|
|||||||
"gitea.24example.ru/spavelit/bpiek/utils"
|
"gitea.24example.ru/spavelit/bpiek/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var categories []model.Category
|
var _categories []model.Category
|
||||||
var parentCategories []model.Category
|
var _parentCategories []model.Category
|
||||||
var treeCategories []model.TreeCategories
|
var _treeCategories []model.TreeCategories
|
||||||
|
|
||||||
func (a *Api) GetCategories() ([]model.Category, error) {
|
func (a *Api) GetCategories() ([]model.Category, error) {
|
||||||
if len(categories) > 0 {
|
if len(_categories) > 0 {
|
||||||
return categories, nil
|
return _categories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
parentCategories, err := a.GetParentCategories()
|
parentCategories, err := a.GetParentCategories()
|
||||||
@ -22,23 +22,26 @@ func (a *Api) GetCategories() ([]model.Category, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, category := range parentCategories {
|
for _, category := range parentCategories {
|
||||||
|
|
||||||
|
_categories = append(_categories, category)
|
||||||
|
|
||||||
categoriesAndProduct, err := a.GetCategoriesAndProductsBySlugParentCategory(category.Slug)
|
categoriesAndProduct, err := a.GetCategoriesAndProductsBySlugParentCategory(category.Slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(categoriesAndProduct.Categories) != 0 {
|
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) {
|
func (a *Api) GetParentCategories() ([]model.Category, error) {
|
||||||
|
|
||||||
if len(parentCategories) > 0 {
|
if len(_parentCategories) > 0 {
|
||||||
return parentCategories, nil
|
return _parentCategories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
apiResponse, err := a.Client.HTTPClient.R().SetResult(&model.CategoryResponse{}).Get("client/catalog")
|
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")
|
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) {
|
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) {
|
func (a *Api) GetTreeCategories() ([]model.TreeCategories, error) {
|
||||||
if len(treeCategories) > 0 {
|
if len(_treeCategories) > 0 {
|
||||||
return treeCategories, nil
|
return _treeCategories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tree := map[string]interface{}{}
|
tree := map[string]interface{}{}
|
||||||
|
|
||||||
parentCategories, err := a.GetParentCategories()
|
categories, err := a.GetCategories()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, parent := range parentCategories {
|
utils.ConvertListToNestedDict(categories, tree)
|
||||||
tree[parent.Slug] = map[string]interface{}{
|
|
||||||
"name": parent.Name,
|
|
||||||
"slug": parent.Slug,
|
|
||||||
"url": parent.Url,
|
|
||||||
"children": map[string]interface{}{},
|
|
||||||
}
|
|
||||||
|
|
||||||
categoriesAndProduct, err := a.GetCategoriesAndProductsBySlugParentCategory(parent.Slug)
|
_treeCategories = utils.ConvertToNestedList(tree)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.ConvertListToNestedDict(categoriesAndProduct.Categories, tree)
|
return _treeCategories, nil
|
||||||
}
|
|
||||||
|
|
||||||
treeCategories = utils.ConvertToNestedList(tree)
|
|
||||||
|
|
||||||
return treeCategories, nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gitea.24example.ru/spavelit/bpiek/api"
|
|
||||||
"github.com/go-resty/resty/v2"
|
"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{}
|
authSuccess := &AuthSuccess{}
|
||||||
client := resty.New()
|
client := resty.New()
|
||||||
client.SetBaseURL(BaseApiUrl)
|
client.SetBaseURL(BaseApiUrl)
|
||||||
|
|
||||||
response, err := client.R().
|
response, err := client.R().
|
||||||
SetFormData(map[string]string{
|
SetFormData(map[string]string{
|
||||||
"username": credentials.Username,
|
"username": username,
|
||||||
"password": credentials.Password,
|
"password": password,
|
||||||
}).
|
}).
|
||||||
SetResult(&authSuccess).
|
SetResult(&authSuccess).
|
||||||
Post(AuthApiUrl)
|
Post(AuthApiUrl)
|
||||||
|
Loading…
Reference in New Issue
Block a user