add order service
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2024-05-27 03:21:47 +07:00
parent f2aedd7a98
commit 820306f654
3 changed files with 66 additions and 44 deletions

View File

@ -5,6 +5,7 @@ import (
"cake_crm/internal/modules/messenger/telegram" "cake_crm/internal/modules/messenger/telegram"
"cake_crm/internal/modules/storage/storage_file" "cake_crm/internal/modules/storage/storage_file"
"cake_crm/internal/services/card" "cake_crm/internal/services/card"
"cake_crm/internal/services/order"
proto "cake_crm/proto" proto "cake_crm/proto"
"context" "context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@ -23,6 +24,8 @@ func main() {
cardService := card.NewService(storage) cardService := card.NewService(storage)
orderService := order.NewService()
tokenData, err := os.ReadFile("resources/order_bot_token.txt") tokenData, err := os.ReadFile("resources/order_bot_token.txt")
if err != nil { if err != nil {
panic(err) panic(err)
@ -52,7 +55,7 @@ func main() {
// Create a gRPC server object // Create a gRPC server object
s := grpc.NewServer() s := grpc.NewServer()
// Attach the Greeter service to the server // 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 // Serve gRPC server
log.Println("Serving gRPC on 0.0.0.0:8080") log.Println("Serving gRPC on 0.0.0.0:8080")
go func() { go func() {

View File

@ -1,14 +1,12 @@
package app package app
import ( import (
"bytes"
"cake_crm/internal/modules/messenger" "cake_crm/internal/modules/messenger"
"cake_crm/internal/modules/storage" "cake_crm/internal/modules/storage"
"cake_crm/internal/services/card" "cake_crm/internal/services/card"
"cake_crm/internal/services/order"
proto "cake_crm/proto" proto "cake_crm/proto"
"context" "context"
"errors"
"fmt"
) )
type Server struct { type Server struct {
@ -16,13 +14,20 @@ type Server struct {
storage storage.IStorage storage storage.IStorage
messenger messenger.IMessenger messenger messenger.IMessenger
cardService *card.Service 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{ return &Server{
storage: storage, storage: storage,
messenger: messenger, messenger: messenger,
cardService: cardService, cardService: cardService,
orderService: orderService,
} }
} }
@ -63,45 +68,13 @@ func (s *Server) Order(ctx context.Context, req *proto.OrderReq) (*proto.OrderRs
if err != nil { if err != nil {
return nil, err return nil, err
} }
text, err := createOrderText(req, enrichItems) text, err := s.orderService.CreateOrderText(req, enrichItems)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &proto.OrderRsp{}, s.messenger.SendMessage(text) 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) { func (s *Server) GetCard(ctx context.Context, req *proto.CardReq) (*proto.CardRsp, error) {
enrichItems, err := s.cardService.GetCard(ctx, req.Items) enrichItems, err := s.cardService.GetCard(ctx, req.Items)
if err != nil { if err != nil {

View File

@ -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")
}