add products
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user