2024-05-18 05:12:00 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-05-23 18:10:31 +00:00
|
|
|
"cake_crm/internal/modules/messenger"
|
|
|
|
"cake_crm/internal/modules/storage"
|
|
|
|
"cake_crm/internal/services/card"
|
2024-05-26 20:21:47 +00:00
|
|
|
"cake_crm/internal/services/order"
|
2024-05-19 11:42:30 +00:00
|
|
|
proto "cake_crm/proto"
|
2024-05-18 05:12:00 +00:00
|
|
|
"context"
|
2024-05-27 20:11:47 +00:00
|
|
|
"fmt"
|
|
|
|
"google.golang.org/genproto/googleapis/api/httpbody"
|
|
|
|
"os"
|
2024-05-18 05:12:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2024-05-22 18:40:59 +00:00
|
|
|
proto.UnsafeCRMServer
|
2024-05-26 20:21:47 +00:00
|
|
|
storage storage.IStorage
|
|
|
|
messenger messenger.IMessenger
|
|
|
|
cardService *card.Service
|
|
|
|
orderService *order.Service
|
2024-05-18 05:12:00 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 20:21:47 +00:00
|
|
|
func NewServer(
|
|
|
|
storage storage.IStorage,
|
|
|
|
messenger messenger.IMessenger,
|
|
|
|
cardService *card.Service,
|
|
|
|
orderService *order.Service,
|
|
|
|
) proto.CRMServer {
|
2024-05-18 05:12:00 +00:00
|
|
|
return &Server{
|
2024-05-26 20:21:47 +00:00
|
|
|
storage: storage,
|
|
|
|
messenger: messenger,
|
|
|
|
cardService: cardService,
|
|
|
|
orderService: orderService,
|
2024-05-18 05:12:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 11:42:30 +00:00
|
|
|
func (s *Server) GetCatalog(ctx context.Context, _ *proto.GetCatalogReq) (*proto.CatalogRsp, error) {
|
2024-05-18 05:12:00 +00:00
|
|
|
categories, err := s.storage.GetCatalog(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-19 11:42:30 +00:00
|
|
|
return &proto.CatalogRsp{Categories: categories}, nil
|
2024-05-18 05:12:00 +00:00
|
|
|
}
|
2024-05-18 05:36:31 +00:00
|
|
|
|
2024-05-19 11:42:30 +00:00
|
|
|
func (s *Server) GetPositions(ctx context.Context, req *proto.GetPositionsReq) (*proto.PositionsRsp, error) {
|
2024-05-18 05:36:31 +00:00
|
|
|
products, err := s.storage.GetPositions(ctx, req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-19 11:42:30 +00:00
|
|
|
return &proto.PositionsRsp{Products: products}, nil
|
2024-05-18 05:36:31 +00:00
|
|
|
}
|
2024-05-18 05:58:50 +00:00
|
|
|
|
2024-05-19 11:42:30 +00:00
|
|
|
func (s *Server) GetProduct(ctx context.Context, req *proto.GetProductReq) (*proto.ProductRsp, error) {
|
2024-05-18 05:58:50 +00:00
|
|
|
product, err := s.storage.GetProduct(ctx, req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-19 11:42:30 +00:00
|
|
|
return &proto.ProductRsp{Product: product}, nil
|
2024-05-18 05:58:50 +00:00
|
|
|
}
|
2024-05-18 06:16:57 +00:00
|
|
|
|
2024-05-19 11:42:30 +00:00
|
|
|
func (s *Server) GetBreadcrumbs(ctx context.Context, req *proto.GetBreadcrumbsReq) (*proto.BreadcrumbsRsp, error) {
|
2024-05-18 06:16:57 +00:00
|
|
|
breadcrumbs, err := s.storage.GetBreadcrumbs(ctx, req.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-19 11:42:30 +00:00
|
|
|
return &proto.BreadcrumbsRsp{Categories: breadcrumbs}, nil
|
2024-05-18 06:16:57 +00:00
|
|
|
}
|
2024-05-22 18:40:59 +00:00
|
|
|
|
|
|
|
func (s *Server) Order(ctx context.Context, req *proto.OrderReq) (*proto.OrderRsp, error) {
|
2024-05-23 18:10:31 +00:00
|
|
|
enrichItems, err := s.cardService.GetCard(ctx, req.Order.Items)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-05-22 18:40:59 +00:00
|
|
|
}
|
2024-05-26 20:21:47 +00:00
|
|
|
text, err := s.orderService.CreateOrderText(req, enrichItems)
|
2024-05-22 18:40:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.OrderRsp{}, s.messenger.SendMessage(text)
|
|
|
|
}
|
|
|
|
|
2024-05-23 18:10:31 +00:00
|
|
|
func (s *Server) GetCard(ctx context.Context, req *proto.CardReq) (*proto.CardRsp, error) {
|
|
|
|
enrichItems, err := s.cardService.GetCard(ctx, req.Items)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.CardRsp{Items: enrichItems}, nil
|
|
|
|
}
|
2024-05-27 20:11:47 +00:00
|
|
|
|
|
|
|
func (s *Server) GetImage(_ context.Context, req *proto.GetImageReq) (*httpbody.HttpBody, error) {
|
|
|
|
data, err := os.ReadFile(fmt.Sprintf("resources/images/%s", req.Name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &httpbody.HttpBody{
|
|
|
|
ContentType: "image/jpeg",
|
|
|
|
Data: data,
|
|
|
|
}, nil
|
|
|
|
}
|