customer/internal/interface/controller/rest/cart/cart.go

124 lines
4.1 KiB
Go
Raw Normal View History

2023-05-19 06:57:36 +00:00
package cart
import (
"context"
"fmt"
"log"
"net/http"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
2023-06-22 09:32:06 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger"
2023-05-19 06:57:36 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
2023-05-19 09:08:15 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
2023-05-19 06:57:36 +00:00
)
type cartService interface {
2023-05-19 07:09:04 +00:00
Remove(ctx context.Context, userID, itemID string) ([]string, errors.Error)
2023-06-29 13:31:16 +00:00
Add(context.Context, *models.AddItemToCart) ([]string, errors.Error)
2023-07-06 18:50:46 +00:00
Pay(ctx context.Context, token, userID string) errors.Error
2023-05-19 06:57:36 +00:00
}
type Deps struct {
Logger *zap.Logger
CartService cartService
}
type Controller struct {
logger *zap.Logger
cartService cartService
}
2023-06-13 22:51:34 +00:00
func New(deps Deps) *Controller {
2023-05-19 06:57:36 +00:00
if deps.Logger == nil {
log.Panicln("logger is nil on <New (cart controller)>")
}
if deps.CartService == nil {
log.Panicln("cart service is nil on <New (cart controller)>")
}
return &Controller{
logger: deps.Logger,
cartService: deps.CartService,
}
}
func (receiver *Controller) Remove(ctx echo.Context, params swagger.RemoveFromCartParams) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <Remove> of <CartController>")
2023-06-29 13:31:16 +00:00
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
2023-05-19 06:57:36 +00:00
}
2023-05-19 09:08:15 +00:00
if validate.IsStringEmpty(params.Id) {
return errors.HTTP(ctx, errors.New(
2023-05-19 09:08:15 +00:00
fmt.Errorf("failed to remove cart item from user <%s>: empty item id", userID),
errors.ErrInvalidArgs,
))
}
2023-05-19 06:57:36 +00:00
cartItems, err := receiver.cartService.Remove(ctx.Request().Context(), userID, params.Id)
if err != nil {
2023-06-29 13:31:16 +00:00
receiver.logger.Error("failed to remove item from cart on <Remove> of <CartController>", zap.Error(err))
return errors.HTTP(ctx, err)
2023-05-19 06:57:36 +00:00
}
return ctx.JSON(http.StatusOK, cartItems)
}
2023-05-19 09:08:15 +00:00
func (receiver *Controller) Add(ctx echo.Context, params swagger.Add2cartParams) error {
2023-05-19 06:57:36 +00:00
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <Add> of <CartController>")
2023-06-29 13:31:16 +00:00
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
}
2023-05-19 06:57:36 +00:00
2023-06-29 13:31:16 +00:00
token, ok := ctx.Get(models.AuthJWTDecodedAccessTokenKey).(string)
if !ok {
receiver.logger.Error("failed to convert access token payload to string on <Add> of <CartController>")
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert access token payload to string", errors.ErrInvalidArgs))
2023-05-19 06:57:36 +00:00
}
2023-05-19 09:08:15 +00:00
if validate.IsStringEmpty(params.Id) {
return errors.HTTP(ctx, errors.New(
2023-05-19 09:08:15 +00:00
fmt.Errorf("failed to add cart item to user <%s>: empty item id", userID),
2023-05-19 06:57:36 +00:00
errors.ErrInvalidArgs,
))
}
2023-06-29 13:31:16 +00:00
cartItems, err := receiver.cartService.Add(ctx.Request().Context(), &models.AddItemToCart{
UserID: userID,
TariffID: params.Id,
AccessToken: token,
})
2023-05-19 06:57:36 +00:00
if err != nil {
2023-06-29 13:31:16 +00:00
receiver.logger.Error("failed to add item to cart on <Add> of <CartController>", zap.Error(err))
return errors.HTTP(ctx, err)
2023-05-19 06:57:36 +00:00
}
return ctx.JSON(http.StatusOK, cartItems)
}
2023-05-30 11:33:57 +00:00
func (receiver *Controller) Pay(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <Pay> of <CartController>")
2023-06-29 13:31:16 +00:00
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
}
2023-05-30 11:33:57 +00:00
2023-06-29 13:31:16 +00:00
token, ok := ctx.Get(models.AuthJWTDecodedAccessTokenKey).(string)
if !ok {
receiver.logger.Error("failed to convert access token payload to string on <Pay> of <CartController>")
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert access token payload to string", errors.ErrInvalidArgs))
2023-05-30 11:33:57 +00:00
}
2023-07-06 18:50:46 +00:00
if err := receiver.cartService.Pay(ctx.Request().Context(), token, userID); err != nil {
2023-05-30 11:33:57 +00:00
receiver.logger.Error("failed to pay cart on <Pay> of <CartController>", zap.Error(err))
return errors.HTTP(ctx, err)
2023-05-30 11:33:57 +00:00
}
return ctx.JSON(http.StatusOK, true)
}