generated from PenaSide/GolangTemplate
158 lines
4.2 KiB
Go
158 lines
4.2 KiB
Go
![]() |
package account
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
|
||
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
||
|
)
|
||
|
|
||
|
type accountRepository interface {
|
||
|
FindByUserID(ctx context.Context, id string) (*models.Account, errors.Error)
|
||
|
FindMany(ctx context.Context, page, limit int64) ([]models.Account, errors.Error)
|
||
|
Insert(ctx context.Context, account *models.Account) (*models.Account, errors.Error)
|
||
|
Remove(ctx context.Context, id string) (*models.Account, errors.Error)
|
||
|
Delete(ctx context.Context, id string) (*models.Account, errors.Error)
|
||
|
}
|
||
|
|
||
|
type authClient interface {
|
||
|
GetUser(ctx context.Context, userID string) (*models.User, errors.Error)
|
||
|
}
|
||
|
|
||
|
type Deps struct {
|
||
|
Logger *zap.Logger
|
||
|
Repository accountRepository
|
||
|
AuthClient authClient
|
||
|
}
|
||
|
|
||
|
type Service struct {
|
||
|
logger *zap.Logger
|
||
|
repository accountRepository
|
||
|
authClient authClient
|
||
|
}
|
||
|
|
||
|
func New(deps *Deps) *Service {
|
||
|
return &Service{
|
||
|
logger: deps.Logger,
|
||
|
repository: deps.Repository,
|
||
|
authClient: deps.AuthClient,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (receiver *Service) GetAccountByUserID(ctx context.Context, userID string) (*models.Account, errors.Error) {
|
||
|
account, err := receiver.repository.FindByUserID(ctx, userID)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to get account by id on <GetAccountByUserID> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.String("userID", userID),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return account, nil
|
||
|
}
|
||
|
|
||
|
func (receiver *Service) GetAccountsList(ctx context.Context, pagination *models.Pagination) ([]models.Account, errors.Error) {
|
||
|
if pagination == nil {
|
||
|
return nil, errors.New(
|
||
|
fmt.Errorf("pagination is nil on <GetAccountsList> of <AccountService>: %w", errors.ErrInternalError),
|
||
|
errors.ErrInternalError,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
accounts, err := receiver.repository.FindMany(ctx, pagination.Page, pagination.Limit)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to get accounts list on <GetAccountsList> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.Int64("page", pagination.Page),
|
||
|
zap.Int64("limit", pagination.Limit),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return accounts, nil
|
||
|
}
|
||
|
|
||
|
func (receiver *Service) CreateAccount(ctx context.Context, account *models.Account) (*models.Account, errors.Error) {
|
||
|
accounts, err := receiver.repository.Insert(ctx, account)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to create account on <CreateAccount> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.Any("account", account),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return accounts, nil
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
CreateAccountByUserID
|
||
|
|
||
|
TODO: Дополнить проверку на дефолтное значение Currency
|
||
|
*/
|
||
|
func (receiver *Service) CreateAccountByUserID(ctx context.Context, userID string) (*models.Account, errors.Error) {
|
||
|
user, err := receiver.authClient.GetUser(ctx, userID)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to get user on <CreateAccountByUserID> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.String("userID", userID),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
account, err := receiver.repository.Insert(ctx, &models.Account{
|
||
|
UserID: user.ID,
|
||
|
Cart: make([]string, 0),
|
||
|
Wallet: models.Wallet{
|
||
|
Cash: 0,
|
||
|
Money: 0,
|
||
|
Currency: "",
|
||
|
},
|
||
|
})
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to create account on <CreateAccountByUserID> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.String("userID", userID),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return account, nil
|
||
|
}
|
||
|
|
||
|
func (receiver *Service) RemoveAccount(ctx context.Context, userID string) (*models.Account, errors.Error) {
|
||
|
account, err := receiver.repository.Remove(ctx, userID)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to remove account on <RemoveAccount> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.String("userID", userID),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return account, nil
|
||
|
}
|
||
|
|
||
|
func (receiver *Service) DeleteAccount(ctx context.Context, userID string) (*models.Account, errors.Error) {
|
||
|
account, err := receiver.repository.Delete(ctx, userID)
|
||
|
if err != nil {
|
||
|
receiver.logger.Error("failed to delete account on <DeleteAccount> of <AccountService>",
|
||
|
zap.Error(err.Extract()),
|
||
|
zap.String("userID", userID),
|
||
|
)
|
||
|
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return account, nil
|
||
|
}
|