From be7b5c505e44494377518d7d6965c0bbc2cecc8f Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sat, 18 May 2024 12:12:00 +0700 Subject: [PATCH] add catalog --- cmd/cake_crm/main.go | 28 ++++--------------- internal/app/server.go | 26 +++++++++++++++++ internal/models/storage/interface.go | 6 +++- .../models/storage/storage_file/storage.go | 21 ++++++++++---- resources/catalog.json | 21 ++++++++++++++ 5 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 internal/app/server.go create mode 100644 resources/catalog.json diff --git a/cmd/cake_crm/main.go b/cmd/cake_crm/main.go index c70f507..8ffd614 100644 --- a/cmd/cake_crm/main.go +++ b/cmd/cake_crm/main.go @@ -1,6 +1,8 @@ package main import ( + "cake_crm/internal/app" + "cake_crm/internal/models/storage/storage_file" crm "cake_crm/proto" "context" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" @@ -11,30 +13,12 @@ import ( "net/http" ) -type server struct { - crm.UnimplementedCRMServer -} - -func NewServer() *server { - return &server{} -} - -func (s *server) GetCatalog(ctx context.Context, req *crm.GetCatalogReq) (*crm.CatalogRsp, error) { - return nil, nil -} - func main() { //ctx, cancel := context.WithCancel(context.Background()) //defer cancel() - // - //storage := storage_file.NewStorageFile("resources/db.json") - //_ = storage - // - //server := server_web.NewServer(storage, 8080) - //err := server.Run(ctx) - //if err != nil { - // panic(err) - //} + + storage := storage_file.NewStorageFile() + _ = storage // Create a listener on TCP port lis, err := net.Listen("tcp", ":8080") @@ -45,7 +29,7 @@ func main() { // Create a gRPC server object s := grpc.NewServer() // Attach the Greeter service to the server - crm.RegisterCRMServer(s, &server{}) + crm.RegisterCRMServer(s, app.NewServer(storage)) // Serve gRPC server log.Println("Serving gRPC on 0.0.0.0:8080") go func() { diff --git a/internal/app/server.go b/internal/app/server.go new file mode 100644 index 0000000..7ff4130 --- /dev/null +++ b/internal/app/server.go @@ -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 +} diff --git a/internal/models/storage/interface.go b/internal/models/storage/interface.go index 2dce758..5fdee27 100644 --- a/internal/models/storage/interface.go +++ b/internal/models/storage/interface.go @@ -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) } diff --git a/internal/models/storage/storage_file/storage.go b/internal/models/storage/storage_file/storage.go index e34592d..d7ba925 100644 --- a/internal/models/storage/storage_file/storage.go +++ b/internal/models/storage/storage_file/storage.go @@ -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) { diff --git a/resources/catalog.json b/resources/catalog.json new file mode 100644 index 0000000..5e8de45 --- /dev/null +++ b/resources/catalog.json @@ -0,0 +1,21 @@ +[ + { + "id": 0, + "name": "Главная", + "url": "/categories/0", + "children": [ + { + "id": 1, + "name": "Пряники", + "url": "/categories/1", + "children": [] + }, + { + "id": 2, + "name": "Пахлава", + "url": "/categories/2", + "children": [] + } + ] + } +]