feat: init cart controller

This commit is contained in:
Kirill 2023-05-19 09:57:36 +03:00
parent 127bb1ed53
commit ced9d0c8e3
6 changed files with 151 additions and 20 deletions

@ -56,6 +56,8 @@ func New(deps *Deps) *Controller {
func (receiver *Controller) GetAccount(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <GetAccount> of <AccountController>")
return echotools.ResponseError(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
@ -94,6 +96,8 @@ func (receiver *Controller) GetAccounts(ctx echo.Context, params swagger.Paginat
func (receiver *Controller) CreateAccount(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <CreateAccount> of <AccountController>")
return echotools.ResponseError(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
@ -111,6 +115,8 @@ func (receiver *Controller) CreateAccount(ctx echo.Context) error {
func (receiver *Controller) RemoveAccount(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <RemoveAccount> of <AccountController>")
return echotools.ResponseError(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,

@ -0,0 +1,110 @@
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)
}

@ -68,7 +68,7 @@ func (receiver *Controller) PutCurrencies(ctx echo.Context) error {
)
return echotools.ResponseError(ctx, errors.New(
fmt.Errorf("failed to parse body: %w", bindErr),
errors.ErrInternalError,
errors.ErrInvalidArgs,
))
}

@ -43,8 +43,6 @@ func TestNewAPI(t *testing.T) {
api := initialize.NewAPI(controllers)
assert.NotNil(t, api)
assert.NotNil(t, api.AccountController)
assert.NotNil(t, api.CurrencyController)
})
})
}

5
internal/models/cart.go Normal file

@ -0,0 +1,5 @@
package models
type AddItemToCart struct {
ID string `json:"id"`
}

@ -16,7 +16,7 @@ type accountController interface {
CreateAccount(ctx echo.Context) error
RemoveDirectAccount(ctx echo.Context, userID string) error
GetDirectAccount(ctx echo.Context, userID string) error
GetAccounts(ctx echo.Context, params PaginationAccountsParams) error
GetAccounts(echo.Context, PaginationAccountsParams) error
}
type currencyController interface {
@ -24,14 +24,21 @@ type currencyController interface {
PutCurrencies(ctx echo.Context) error
}
type cartController interface {
Remove(echo.Context, RemoveFromCartParams) error
Add(echo.Context) error
}
type Deps struct {
AccountController accountController
CurrencyController currencyController
CartController cartController
}
type API struct {
AccountController accountController
CurrencyController currencyController
accountController accountController
currencyController currencyController
cartController cartController
}
func New(deps *Deps) *API {
@ -44,49 +51,54 @@ func New(deps *Deps) *API {
}
if deps.CurrencyController == nil {
log.Panicln("CurrencyController is nil on <New (API)>")
log.Panicln("currencyController is nil on <New (API)>")
}
if deps.CartController == nil {
log.Panicln("cartController is nil on <New (API)>")
}
return &API{
AccountController: deps.AccountController,
CurrencyController: deps.CurrencyController,
accountController: deps.AccountController,
currencyController: deps.CurrencyController,
cartController: deps.CartController,
}
}
// Account
func (receiver *API) DeleteAccount(ctx echo.Context) error {
return receiver.AccountController.RemoveAccount(ctx)
return receiver.accountController.RemoveAccount(ctx)
}
func (receiver *API) GetAccount(ctx echo.Context) error {
return receiver.AccountController.GetAccount(ctx)
return receiver.accountController.GetAccount(ctx)
}
func (receiver *API) AddAccount(ctx echo.Context) error {
return receiver.AccountController.CreateAccount(ctx)
return receiver.accountController.CreateAccount(ctx)
}
func (receiver *API) DeleteDirectAccount(ctx echo.Context, userID string) error {
return receiver.AccountController.RemoveDirectAccount(ctx, userID)
return receiver.accountController.RemoveDirectAccount(ctx, userID)
}
func (receiver *API) GetDirectAccount(ctx echo.Context, userID string) error {
return receiver.AccountController.GetDirectAccount(ctx, userID)
return receiver.accountController.GetDirectAccount(ctx, userID)
}
func (receiver *API) PaginationAccounts(ctx echo.Context, params PaginationAccountsParams) error {
return receiver.AccountController.GetAccounts(ctx, params)
return receiver.accountController.GetAccounts(ctx, params)
}
// Cart
func (receiver *API) RemoveFromCart(ctx echo.Context, _ RemoveFromCartParams) error {
return ctx.String(http.StatusNotImplemented, "method not implemented")
func (receiver *API) RemoveFromCart(ctx echo.Context, params RemoveFromCartParams) error {
return receiver.cartController.Remove(ctx, params)
}
func (receiver *API) Add2cart(ctx echo.Context) error {
return ctx.String(http.StatusNotImplemented, "method not implemented")
return receiver.cartController.Add(ctx)
}
func (receiver *API) PayCart(ctx echo.Context) error {
@ -96,11 +108,11 @@ func (receiver *API) PayCart(ctx echo.Context) error {
// Currency
func (receiver *API) GetCurrencies(ctx echo.Context) error {
return receiver.CurrencyController.GetCurrencies(ctx)
return receiver.currencyController.GetCurrencies(ctx)
}
func (receiver *API) UpdateCurrencies(ctx echo.Context) error {
return receiver.CurrencyController.PutCurrencies(ctx)
return receiver.currencyController.PutCurrencies(ctx)
}
// History