add catalog
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"cake_crm/internal/models/storage"
|
||||
crm "cake_crm/proto"
|
||||
"context"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
crm.UnimplementedCRMServer
|
||||
storage storage.IStorage
|
||||
}
|
||||
|
||||
func NewServer(storage storage.IStorage) *Server {
|
||||
return &Server{
|
||||
storage: storage,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) GetCatalog(ctx context.Context, _ *crm.GetCatalogReq) (*crm.CatalogRsp, error) {
|
||||
categories, err := s.storage.GetCatalog(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &crm.CatalogRsp{Categories: categories}, nil
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package storage
|
||||
|
||||
import "context"
|
||||
import (
|
||||
crm "cake_crm/proto"
|
||||
"context"
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
ID int `json:"id"`
|
||||
@@ -17,4 +20,5 @@ type IStorage interface {
|
||||
GetAllProducts(ctx context.Context) ([]Product, error)
|
||||
GetProductByID(ctx context.Context, id int) (Product, error)
|
||||
GetBreadcrumbs(ctx context.Context, id int) ([]Breadcrumb, error)
|
||||
GetCatalog(ctx context.Context) ([]*crm.CatalogRsp_Category, error)
|
||||
}
|
||||
|
||||
@@ -2,17 +2,28 @@ package storage_file
|
||||
|
||||
import (
|
||||
"cake_crm/internal/models/storage"
|
||||
crm "cake_crm/proto"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
type storageFile struct {
|
||||
filepath string
|
||||
type storageFile struct{}
|
||||
|
||||
func NewStorageFile() storage.IStorage {
|
||||
return &storageFile{}
|
||||
}
|
||||
|
||||
func NewStorageFile(filepath string) storage.IStorage {
|
||||
return &storageFile{
|
||||
filepath: filepath,
|
||||
func (s *storageFile) GetCatalog(_ context.Context) ([]*crm.CatalogRsp_Category, error) {
|
||||
data, err := os.ReadFile("resources/catalog.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var res []*crm.CatalogRsp_Category
|
||||
if err := json.Unmarshal(data, &res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *storageFile) GetAllProducts(ctx context.Context) ([]storage.Product, error) {
|
||||
|
||||
Reference in New Issue
Block a user