generated from PenaSide/GolangTemplate
111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
![]() |
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"
|
||
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
||
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/swagger"
|
||
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/echotools"
|
||
|
)
|
||
|
|
||
|
type cartService interface {
|
||
|
Remove(ctx context.Context, userID, tariffID string) ([]string, errors.Error)
|
||
|
Add(ctx context.Context, userID, tariffID string) ([]string, errors.Error)
|
||
|
}
|
||
|
|
||
|
type Deps struct {
|
||
|
Logger *zap.Logger
|
||
|
CartService cartService
|
||
|
}
|
||
|
|
||
|
type Controller struct {
|
||
|
logger *zap.Logger
|
||
|
cartService cartService
|
||
|
}
|
||
|
|
||
|
func New(deps *Deps) *Controller {
|
||
|
if deps == nil {
|
||
|
log.Panicln("deps is nil on <New (cart controller)>")
|
||
|
}
|
||
|
|
||
|
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>")
|
||
|
|
||
|
return echotools.ResponseError(ctx, errors.New(
|
||
|
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||
|
errors.ErrInvalidArgs,
|
||
|
))
|
||
|
}
|
||
|
|
||
|
cartItems, err := receiver.cartService.Remove(ctx.Request().Context(), userID, params.Id)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error(
|
||
|
"failed to remove item from cart on <Remove> of <CartController>",
|
||
|
zap.Error(err.Extract()),
|
||
|
)
|
||
|
|
||
|
return echotools.ResponseError(ctx, err)
|
||
|
}
|
||
|
|
||
|
return ctx.JSON(http.StatusOK, cartItems)
|
||
|
}
|
||
|
|
||
|
func (receiver *Controller) Add(ctx echo.Context) error {
|
||
|
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||
|
if !ok {
|
||
|
receiver.logger.Error("failed to convert jwt payload to string on <Add> of <CartController>")
|
||
|
|
||
|
return echotools.ResponseError(ctx, errors.New(
|
||
|
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||
|
errors.ErrInvalidArgs,
|
||
|
))
|
||
|
}
|
||
|
|
||
|
request, bindErr := echotools.Bind[models.AddItemToCart](ctx)
|
||
|
if bindErr != nil {
|
||
|
receiver.logger.Error(
|
||
|
"failed to parse body on <Add> of <CartController>",
|
||
|
zap.Error(bindErr),
|
||
|
)
|
||
|
|
||
|
return echotools.ResponseError(ctx, errors.New(
|
||
|
fmt.Errorf("failed to parse body: %w", bindErr),
|
||
|
errors.ErrInvalidArgs,
|
||
|
))
|
||
|
}
|
||
|
|
||
|
cartItems, err := receiver.cartService.Add(ctx.Request().Context(), userID, request.ID)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error(
|
||
|
"failed to add item to cart on <Add> of <CartController>",
|
||
|
zap.Error(err.Extract()),
|
||
|
)
|
||
|
|
||
|
return echotools.ResponseError(ctx, err)
|
||
|
}
|
||
|
|
||
|
return ctx.JSON(http.StatusOK, cartItems)
|
||
|
}
|