shoppingcart.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package service
  2. import (
  3. "net/http"
  4. "sghgogs.com/micro/common/errorcode"
  5. req "sghgogs.com/micro/shopping-service/domain/model/request"
  6. pb "sghgogs.com/micro/shopping-service/proto"
  7. "time"
  8. )
  9. func (svc *Service) CreateShoppingCart(add *pb.CreateShoppingCartRequest) error {
  10. if err := svc.Repository.CreateShoppingCart(&req.ShoppingCart{
  11. UserID: add.UserId,
  12. Quantity: add.Quantity,
  13. }); err != nil {
  14. return errorcode.New(svc.Namespace, err.Error(), http.StatusBadRequest)
  15. }
  16. return nil
  17. }
  18. func (svc *Service) CreateMultipleShoppingCartItem(cartItems *pb.CreateShoppingCartItemRequest) error {
  19. items := make([]*req.ShoppingCartItem, 0)
  20. for _, item := range cartItems.Items {
  21. items = append(items, &req.ShoppingCartItem{
  22. ShoppingCartID: item.ShoppingCartId,
  23. ProductID: item.ProductId,
  24. Quantity: item.Quantity,
  25. TotalPrice: item.TotalPrice,
  26. CreatedAt: time.Now(),
  27. UpdatedAt: nil,
  28. })
  29. }
  30. if err := svc.Repository.CreateMultipleShoppingCartItem(items); err != nil {
  31. return errorcode.New(svc.Namespace, err.Error(), http.StatusBadRequest)
  32. }
  33. return nil
  34. }