@@ -10,4 +10,5 @@ type IStorage interface {
|
||||
GetPositions(ctx context.Context, id int64) ([]*crm.Product, error)
|
||||
GetProduct(ctx context.Context, id int64) (*crm.Product, error)
|
||||
GetBreadcrumbs(ctx context.Context, id int64) ([]*crm.Category, error)
|
||||
GetPositionsByText(ctx context.Context, text string) ([]*crm.Product, error)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
@@ -160,3 +161,24 @@ func getBreadcrumbs(categories []*crm.Category, id int64) []*crm.Category {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *storageFile) GetPositionsByText(_ context.Context, text string) ([]*crm.Product, error) {
|
||||
data, err := os.ReadFile("resources/products.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var products []*crm.Product
|
||||
if err := json.Unmarshal(data, &products); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := make([]*crm.Product, 0, len(products))
|
||||
searchText := strings.TrimSpace(strings.ToLower(text))
|
||||
for _, product := range products {
|
||||
name := strings.ToLower(product.Name)
|
||||
if strings.Contains(name, searchText) {
|
||||
s.enrichedProduct(product)
|
||||
res = append(res, product)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user