customer/internal/interface/controller/rest/account/account.go
2023-06-13 15:19:51 +03:00

142 lines
4.2 KiB
Go

package account
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"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/echotools"
)
type accountService interface {
GetAccountByUserID(ctx context.Context, userID string) (*models.Account, errors.Error)
GetAccountsList(ctx context.Context, pagination *models.Pagination) (*models.PaginationResponse[models.Account], errors.Error)
CreateAccount(ctx context.Context, account *models.Account) (*models.Account, errors.Error)
CreateAccountByUserID(ctx context.Context, userID string) (*models.Account, errors.Error)
RemoveAccount(ctx context.Context, userID string) (*models.Account, errors.Error)
DeleteAccount(ctx context.Context, userID string) (*models.Account, errors.Error)
}
type Deps struct {
Logger *zap.Logger
Service accountService
}
type Controller struct {
logger *zap.Logger
Service accountService
}
func New(deps *Deps) *Controller {
if deps == nil {
log.Panicln("deps is nil on <New (account controller)>")
}
if deps.Logger == nil {
log.Panicln("logger is nil on <New (account controller)>")
}
if deps.Service == nil {
log.Panicln("account service is nil on <New (account controller)>")
}
return &Controller{
logger: deps.Logger,
Service: deps.Service,
}
}
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,
))
}
account, err := receiver.Service.GetAccountByUserID(ctx.Request().Context(), userID)
if err != nil {
return echotools.ResponseError(ctx, err)
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) GetDirectAccount(ctx echo.Context, userID string) error {
account, err := receiver.Service.GetAccountByUserID(ctx.Request().Context(), userID)
if err != nil {
return echotools.ResponseError(ctx, err)
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) GetAccounts(ctx echo.Context, params swagger.PaginationAccountsParams) error {
response, err := receiver.Service.GetAccountsList(
ctx.Request().Context(),
utils.DeterminePagination(params.Page, params.Limit),
)
if err != nil {
return echotools.ResponseError(ctx, err)
}
return ctx.JSON(http.StatusOK, response)
}
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,
))
}
account, err := receiver.Service.CreateAccountByUserID(ctx.Request().Context(), userID)
if err != nil {
return echotools.ResponseError(ctx, err)
}
return ctx.JSON(http.StatusOK, account)
}
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,
))
}
account, err := receiver.Service.RemoveAccount(ctx.Request().Context(), userID)
if err != nil {
return echotools.ResponseError(ctx, err)
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) RemoveDirectAccount(ctx echo.Context, userID string) error {
account, err := receiver.Service.RemoveAccount(ctx.Request().Context(), userID)
if err != nil {
return echotools.ResponseError(ctx, err)
}
return ctx.JSON(http.StatusOK, account)
}