add products

This commit is contained in:
2024-05-18 12:58:50 +07:00
parent afd5536246
commit 6b3306bb96
8 changed files with 389 additions and 31 deletions
+8
View File
@@ -32,3 +32,11 @@ func (s *Server) GetPositions(ctx context.Context, req *crm.GetPositionsReq) (*c
}
return &crm.PositionsRsp{Products: products}, nil
}
func (s *Server) GetProduct(ctx context.Context, req *crm.GetProductReq) (*crm.ProductRsp, error) {
product, err := s.storage.GetProduct(ctx, req.Id)
if err != nil {
return nil, err
}
return &crm.ProductRsp{Product: product}, nil
}
+1
View File
@@ -19,4 +19,5 @@ type Breadcrumb struct {
type IStorage interface {
GetCatalog(ctx context.Context) ([]*crm.Category, error)
GetPositions(ctx context.Context, id int64) ([]*crm.Product, error)
GetProduct(ctx context.Context, id int64) (*crm.Product, error)
}
@@ -5,9 +5,14 @@ import (
crm "cake_crm/proto"
"context"
"encoding/json"
"errors"
"os"
)
var (
ErrProductNotFound = errors.New("product not found")
)
type storageFile struct{}
func NewStorageFile() storage.IStorage {
@@ -43,3 +48,20 @@ func (s *storageFile) GetPositions(_ context.Context, id int64) ([]*crm.Product,
}
return res, nil
}
func (s *storageFile) GetProduct(_ context.Context, id int64) (*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
}
for _, product := range products {
if product.Id == id {
return product, nil
}
}
return nil, ErrProductNotFound
}