From 820306f654f74fc9fb884cbfc2122958f759b2d3 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Mon, 27 May 2024 03:21:47 +0700 Subject: [PATCH] add order service --- cmd/cake_crm/main.go | 5 ++- internal/app/server.go | 59 +++++++++----------------------- internal/services/order/order.go | 46 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 44 deletions(-) create mode 100644 internal/services/order/order.go diff --git a/cmd/cake_crm/main.go b/cmd/cake_crm/main.go index 1de898c..e5b25b7 100644 --- a/cmd/cake_crm/main.go +++ b/cmd/cake_crm/main.go @@ -5,6 +5,7 @@ import ( "cake_crm/internal/modules/messenger/telegram" "cake_crm/internal/modules/storage/storage_file" "cake_crm/internal/services/card" + "cake_crm/internal/services/order" proto "cake_crm/proto" "context" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" @@ -23,6 +24,8 @@ func main() { cardService := card.NewService(storage) + orderService := order.NewService() + tokenData, err := os.ReadFile("resources/order_bot_token.txt") if err != nil { panic(err) @@ -52,7 +55,7 @@ func main() { // Create a gRPC server object s := grpc.NewServer() // Attach the Greeter service to the server - proto.RegisterCRMServer(s, app.NewServer(storage, messenger, cardService)) + proto.RegisterCRMServer(s, app.NewServer(storage, messenger, cardService, orderService)) // 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 index 3ab8f04..19a3183 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -1,28 +1,33 @@ package app import ( - "bytes" "cake_crm/internal/modules/messenger" "cake_crm/internal/modules/storage" "cake_crm/internal/services/card" + "cake_crm/internal/services/order" proto "cake_crm/proto" "context" - "errors" - "fmt" ) type Server struct { proto.UnsafeCRMServer - storage storage.IStorage - messenger messenger.IMessenger - cardService *card.Service + storage storage.IStorage + messenger messenger.IMessenger + cardService *card.Service + orderService *order.Service } -func NewServer(storage storage.IStorage, messenger messenger.IMessenger, cardService *card.Service) proto.CRMServer { +func NewServer( + storage storage.IStorage, + messenger messenger.IMessenger, + cardService *card.Service, + orderService *order.Service, +) proto.CRMServer { return &Server{ - storage: storage, - messenger: messenger, - cardService: cardService, + storage: storage, + messenger: messenger, + cardService: cardService, + orderService: orderService, } } @@ -63,45 +68,13 @@ func (s *Server) Order(ctx context.Context, req *proto.OrderReq) (*proto.OrderRs if err != nil { return nil, err } - text, err := createOrderText(req, enrichItems) + text, err := s.orderService.CreateOrderText(req, enrichItems) if err != nil { return nil, err } return &proto.OrderRsp{}, s.messenger.SendMessage(text) } -func createOrderText(req *proto.OrderReq, items []*proto.CardItem) (string, error) { - buffer := bytes.Buffer{} - var orderAmount int64 - buffer.WriteString(fmt.Sprintf("Заказ от:\n%s\n%s\n", req.Order.Name, req.Order.Phone)) - buffer.WriteString("\n") - for _, item := range items { - buffer.WriteString(item.Name) - buffer.WriteString("\n") - unit, err := unitToText(item.Unit) - if err != nil { - return "", err - } - buffer.WriteString(fmt.Sprintf("Количество: %d%s\n", item.Count, unit)) - orderAmount += item.Amount - buffer.WriteString(fmt.Sprintf("Сумма: %.00fр\n", float64(item.Amount)/100)) - buffer.WriteString("\n") - } - buffer.WriteString("\n") - buffer.WriteString(fmt.Sprintf("ИТОГО: %.00fр\n", float64(orderAmount)/100)) - return buffer.String(), nil -} - -func unitToText(unit string) (string, error) { - switch unit { - case "kg": - return "кг", nil - case "piece": - return "шт", nil - } - return "", errors.New("unit not found") -} - func (s *Server) GetCard(ctx context.Context, req *proto.CardReq) (*proto.CardRsp, error) { enrichItems, err := s.cardService.GetCard(ctx, req.Items) if err != nil { diff --git a/internal/services/order/order.go b/internal/services/order/order.go new file mode 100644 index 0000000..0e7295c --- /dev/null +++ b/internal/services/order/order.go @@ -0,0 +1,46 @@ +package order + +import ( + "bytes" + "cake_crm/proto" + "errors" + "fmt" +) + +type Service struct{} + +func NewService() *Service { + return &Service{} +} + +func (s *Service) CreateOrderText(req *proto.OrderReq, items []*proto.CardItem) (string, error) { + buffer := bytes.Buffer{} + var orderAmount int64 + buffer.WriteString(fmt.Sprintf("Заказ от:\n%s\n%s\n", req.Order.Name, req.Order.Phone)) + buffer.WriteString("\n") + for _, item := range items { + buffer.WriteString(item.Name) + buffer.WriteString("\n") + unit, err := unitToText(item.Unit) + if err != nil { + return "", err + } + buffer.WriteString(fmt.Sprintf("Количество: %d%s\n", item.Count, unit)) + orderAmount += item.Amount + buffer.WriteString(fmt.Sprintf("Сумма: %.00fр\n", float64(item.Amount)/100)) + buffer.WriteString("\n") + } + buffer.WriteString("\n") + buffer.WriteString(fmt.Sprintf("ИТОГО: %.00fр\n", float64(orderAmount)/100)) + return buffer.String(), nil +} + +func unitToText(unit string) (string, error) { + switch unit { + case "kg": + return "кг", nil + case "piece": + return "шт", nil + } + return "", errors.New("unit not found") +}