add search
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-05-30 04:17:13 +07:00
parent 073a7e6d8c
commit 8be5d0b831
9 changed files with 320 additions and 53 deletions
+8
View File
@@ -96,3 +96,11 @@ func (s *Server) GetImage(_ context.Context, req *proto.GetImageReq) (*httpbody.
Data: data,
}, nil
}
func (s *Server) Search(ctx context.Context, req *proto.SearchReq) (*proto.PositionsRsp, error) {
products, err := s.storage.GetPositionsByText(ctx, req.Text)
if err != nil {
return nil, err
}
return &proto.PositionsRsp{Products: products}, nil
}
+1
View File
@@ -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
}