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

142 lines
4.2 KiB
Go
Raw Normal View History

2023-05-17 20:27:09 +00:00
package account
import (
"context"
"fmt"
2023-05-19 04:50:40 +00:00
"log"
2023-05-17 20:27:09 +00:00
"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"
2023-05-18 19:43:43 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/echotools"
2023-05-17 20:27:09 +00:00
)
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)
2023-05-17 20:27:09 +00:00
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
2023-05-19 04:50:40 +00:00
Service accountService
2023-05-17 20:27:09 +00:00
}
func New(deps *Deps) *Controller {
2023-05-19 04:50:40 +00:00
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)>")
}
2023-05-17 20:27:09 +00:00
return &Controller{
logger: deps.Logger,
2023-05-19 04:50:40 +00:00
Service: deps.Service,
2023-05-17 20:27:09 +00:00
}
}
func (receiver *Controller) GetAccount(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
2023-05-19 06:57:36 +00:00
receiver.logger.Error("failed to convert jwt payload to string on <GetAccount> of <AccountController>")
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, errors.New(
2023-05-17 20:27:09 +00:00
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
}
2023-05-19 04:50:40 +00:00
account, err := receiver.Service.GetAccountByUserID(ctx.Request().Context(), userID)
2023-05-17 20:27:09 +00:00
if err != nil {
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, err)
2023-05-17 20:27:09 +00:00
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) GetDirectAccount(ctx echo.Context, userID string) error {
2023-05-19 04:50:40 +00:00
account, err := receiver.Service.GetAccountByUserID(ctx.Request().Context(), userID)
2023-05-17 20:27:09 +00:00
if err != nil {
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, err)
2023-05-17 20:27:09 +00:00
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) GetAccounts(ctx echo.Context, params swagger.PaginationAccountsParams) error {
2023-05-19 04:50:40 +00:00
response, err := receiver.Service.GetAccountsList(
2023-05-17 20:27:09 +00:00
ctx.Request().Context(),
utils.DeterminePagination(params.Page, params.Limit),
)
if err != nil {
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, err)
2023-05-17 20:27:09 +00:00
}
return ctx.JSON(http.StatusOK, response)
2023-05-17 20:27:09 +00:00
}
func (receiver *Controller) CreateAccount(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
2023-05-19 06:57:36 +00:00
receiver.logger.Error("failed to convert jwt payload to string on <CreateAccount> of <AccountController>")
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, errors.New(
2023-05-17 20:27:09 +00:00
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
}
2023-05-19 04:50:40 +00:00
account, err := receiver.Service.CreateAccountByUserID(ctx.Request().Context(), userID)
2023-05-17 20:27:09 +00:00
if err != nil {
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, err)
2023-05-17 20:27:09 +00:00
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) RemoveAccount(ctx echo.Context) error {
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
2023-05-19 06:57:36 +00:00
receiver.logger.Error("failed to convert jwt payload to string on <RemoveAccount> of <AccountController>")
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, errors.New(
2023-05-17 20:27:09 +00:00
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
}
2023-05-19 04:50:40 +00:00
account, err := receiver.Service.RemoveAccount(ctx.Request().Context(), userID)
2023-05-17 20:27:09 +00:00
if err != nil {
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, err)
2023-05-17 20:27:09 +00:00
}
return ctx.JSON(http.StatusOK, account)
}
func (receiver *Controller) RemoveDirectAccount(ctx echo.Context, userID string) error {
2023-05-19 04:50:40 +00:00
account, err := receiver.Service.RemoveAccount(ctx.Request().Context(), userID)
2023-05-17 20:27:09 +00:00
if err != nil {
2023-05-18 19:43:43 +00:00
return echotools.ResponseError(ctx, err)
2023-05-17 20:27:09 +00:00
}
return ctx.JSON(http.StatusOK, account)
}