generated from PenaSide/GolangTemplate
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package currency
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"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 {
|
|
if deps == nil {
|
|
log.Panicln("deps is nil on <New (currency service)>")
|
|
}
|
|
|
|
if deps.Logger == nil {
|
|
log.Panicln("logger is nil on <New (currency service)>")
|
|
}
|
|
|
|
if deps.Repository == nil {
|
|
log.Panicln("repository is nil on <New (currency 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
|
|
}
|