customer/internal/service/currency/currency.go

64 lines
1.6 KiB
Go
Raw Normal View History

2023-05-18 19:43:43 +00:00
package currency
import (
"context"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)
type currencyRepository interface {
FindCurrenciesList(ctx context.Context, name string) (*models.CurrencyList, errors.Error)
InsertCurrenciesToList(ctx context.Context, name string, currencies []string) (*models.CurrencyList, errors.Error)
}
type Deps struct {
Logger *zap.Logger
Repository currencyRepository
}
type Service struct {
logger *zap.Logger
repository currencyRepository
}
func New(deps *Deps) *Service {
return &Service{
logger: deps.Logger,
repository: deps.Repository,
}
}
func (receiver *Service) GetCurrencies(ctx context.Context) ([]string, errors.Error) {
currencyList, err := receiver.repository.FindCurrenciesList(ctx, models.DefaultCurrencyListName)
if err != nil && err.Type() != errors.ErrNotFound {
receiver.logger.Error(
"failed to get currencies on <GetCurrencies> of <CurrencyService>",
zap.Error(err.Extract()),
)
return []string{}, err
}
if err.Type() == errors.ErrNotFound {
return []string{}, nil
}
return currencyList.Currencies, nil
}
func (receiver *Service) PutCurrencies(ctx context.Context, currencies []string) ([]string, errors.Error) {
currencyList, err := receiver.repository.InsertCurrenciesToList(ctx, models.DefaultCurrencyListName, currencies)
if err != nil {
receiver.logger.Error(
"failed to put currencies on <PutCurrencies> of <CurrencyService>",
zap.Error(err.Extract()),
)
return []string{}, err
}
return currencyList.Currencies, nil
}