update cart
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-05-30 03:54:12 +07:00
parent 323f50fb43
commit 073a7e6d8c
10 changed files with 207 additions and 163 deletions
+8 -8
View File
@@ -3,7 +3,7 @@ package app
import (
"cake_crm/internal/modules/messenger"
"cake_crm/internal/modules/storage"
"cake_crm/internal/services/card"
"cake_crm/internal/services/cart"
"cake_crm/internal/services/order"
proto "cake_crm/proto"
"context"
@@ -16,20 +16,20 @@ type Server struct {
proto.UnsafeCRMServer
storage storage.IStorage
messenger messenger.IMessenger
cardService *card.Service
cartService *cart.Service
orderService *order.Service
}
func NewServer(
storage storage.IStorage,
messenger messenger.IMessenger,
cardService *card.Service,
cartService *cart.Service,
orderService *order.Service,
) proto.CRMServer {
return &Server{
storage: storage,
messenger: messenger,
cardService: cardService,
cartService: cartService,
orderService: orderService,
}
}
@@ -67,7 +67,7 @@ func (s *Server) GetBreadcrumbs(ctx context.Context, req *proto.GetBreadcrumbsRe
}
func (s *Server) Order(ctx context.Context, req *proto.OrderReq) (*proto.OrderRsp, error) {
enrichItems, err := s.cardService.GetCard(ctx, req.Order.Items)
enrichItems, err := s.cartService.GetCart(ctx, req.Order.Items)
if err != nil {
return nil, err
}
@@ -78,12 +78,12 @@ func (s *Server) Order(ctx context.Context, req *proto.OrderReq) (*proto.OrderRs
return &proto.OrderRsp{}, s.messenger.SendMessage(text)
}
func (s *Server) GetCard(ctx context.Context, req *proto.CardReq) (*proto.CardRsp, error) {
enrichItems, err := s.cardService.GetCard(ctx, req.Items)
func (s *Server) GetCart(ctx context.Context, req *proto.CartReq) (*proto.CartRsp, error) {
resp, err := s.cartService.GetCart(ctx, req.Items)
if err != nil {
return nil, err
}
return &proto.CardRsp{Items: enrichItems}, nil
return resp, nil
}
func (s *Server) GetImage(_ context.Context, req *proto.GetImageReq) (*httpbody.HttpBody, error) {
@@ -1,4 +1,4 @@
package card
package cart
import (
"cake_crm/internal/modules/storage"
@@ -23,8 +23,10 @@ func NewService(storage storage.IStorage) *Service {
}
}
func (s *Service) GetCard(ctx context.Context, items []*proto.OrderItem) ([]*proto.CardItem, error) {
res := make([]*proto.CardItem, 0, len(items))
func (s *Service) GetCart(ctx context.Context, items []*proto.OrderItem) (*proto.CartRsp, error) {
var cartAmount int64
var cartAmountOld int64
res := make([]*proto.CartItem, 0, len(items))
for _, item := range items {
product, err := s.storage.GetProduct(ctx, item.ProductId)
if err != nil {
@@ -41,7 +43,7 @@ func (s *Service) GetCard(ctx context.Context, items []*proto.OrderItem) ([]*pro
}
res = append(
res,
&proto.CardItem{
&proto.CartItem{
Id: product.Id,
Article: product.Article,
Name: product.Name,
@@ -56,8 +58,14 @@ func (s *Service) GetCard(ctx context.Context, items []*proto.OrderItem) ([]*pro
Labels: product.Labels,
},
)
cartAmount += amount
cartAmountOld += amountOld
}
return res, nil
return &proto.CartRsp{
Items: res,
Amount: cartAmount,
AmountOld: cartAmountOld,
}, nil
}
func calcItemAmount(item *ProductAndCount) (int64, int64, error) {
+3 -5
View File
@@ -13,12 +13,11 @@ func NewService() *Service {
return &Service{}
}
func (s *Service) CreateOrderText(req *proto.OrderReq, items []*proto.CardItem) (string, error) {
func (s *Service) CreateOrderText(req *proto.OrderReq, cart *proto.CartRsp) (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 {
for _, item := range cart.Items {
buffer.WriteString(item.Name)
buffer.WriteString("\n")
unit, err := unitToText(item.Unit)
@@ -26,12 +25,11 @@ func (s *Service) CreateOrderText(req *proto.OrderReq, items []*proto.CardItem)
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))
buffer.WriteString(fmt.Sprintf("ИТОГО: %.00fр\n", float64(cart.Amount)/100))
return buffer.String(), nil
}