customer/internal/interface/repository/currency.go

130 lines
3.3 KiB
Go
Raw Normal View History

2023-05-18 19:43:43 +00:00
package repository
import (
"context"
"fmt"
2023-05-19 04:50:40 +00:00
"log"
2023-05-19 05:00:31 +00:00
"time"
2023-05-18 19:43:43 +00:00
"go.mongodb.org/mongo-driver/bson"
2023-05-19 05:00:31 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-05-18 19:43:43 +00:00
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
2024-02-02 12:15:03 +00:00
mongoWrapper "penahub.gitlab.yandexcloud.net/backend/penahub_common/mongo"
2024-11-18 07:23:41 +00:00
"gitea.pena/PenaSide/customer/internal/errors"
"gitea.pena/PenaSide/customer/internal/fields"
"gitea.pena/PenaSide/customer/internal/models"
2023-05-18 19:43:43 +00:00
)
type CurrencyRepositoryDeps struct {
Logger *zap.Logger
MongoDB *mongo.Collection
}
type CurrencyRepository struct {
logger *zap.Logger
mongoDB *mongo.Collection
}
2023-06-13 22:51:34 +00:00
func NewCurrencyRepository(deps CurrencyRepositoryDeps) *CurrencyRepository {
2023-05-19 04:50:40 +00:00
if deps.Logger == nil {
log.Panicln("logger is nil on <NewCurrencyRepository>")
}
if deps.MongoDB == nil {
log.Panicln("mongodb is nil on <NewCurrencyRepository>")
2023-05-19 04:50:40 +00:00
}
2023-05-18 19:43:43 +00:00
return &CurrencyRepository{
mongoDB: deps.MongoDB,
logger: deps.Logger,
}
}
func (receiver *CurrencyRepository) FindCurrenciesList(ctx context.Context, name string) (*models.CurrencyList, errors.Error) {
filter := bson.M{
fields.Currency.Name: name,
fields.Currency.IsDeleted: false,
2023-05-18 19:43:43 +00:00
}
currencyList, err := mongoWrapper.FindOne[models.CurrencyList](ctx, &mongoWrapper.RequestSettings{
Driver: receiver.mongoDB,
Filter: filter,
})
if err != nil {
receiver.logger.Error("failed to find list on <FindCurrenciesList> of <CurrencyRepository>",
zap.String("name", name),
zap.Error(err),
)
findError := errors.New(
fmt.Errorf("failed to find list with <%s> on <FindCurrenciesList> of <CurrencyRepository>: %w", name, err),
errors.ErrInternalError,
)
if err == mongo.ErrNoDocuments {
findError.SetType(errors.ErrNotFound)
}
return nil, findError
}
return currencyList, nil
}
2023-05-19 05:21:52 +00:00
func (receiver *CurrencyRepository) ReplaceCurrencies(ctx context.Context, list *models.CurrencyList) (*models.CurrencyList, errors.Error) {
2023-05-18 19:43:43 +00:00
currencyList := models.CurrencyList{}
2023-05-19 05:00:31 +00:00
filter := bson.M{
fields.Currency.Name: list.Name,
fields.Currency.IsDeleted: false,
2023-05-19 05:00:31 +00:00
}
2023-05-18 19:43:43 +00:00
update := bson.M{
2023-05-19 05:00:31 +00:00
"$set": bson.M{
2023-05-19 05:21:52 +00:00
fields.Currency.UpdatedAt: time.Now(),
fields.Currency.Currencies: list.Currencies,
2023-05-18 19:43:43 +00:00
},
}
if err := receiver.mongoDB.FindOneAndUpdate(ctx, filter, update).Decode(&currencyList); err != nil {
receiver.logger.Error("failed to insert currencies on <InsertCurrenciesToList> of <CurrencyRepository>",
2023-05-19 05:00:31 +00:00
zap.String("name", list.Name),
2023-05-18 19:43:43 +00:00
zap.Error(err),
)
removeErr := errors.New(
2023-05-19 05:00:31 +00:00
fmt.Errorf("failed to insert currencies <%s> on <InsertCurrenciesToList> of <CurrencyRepository>: %w", list.Name, err),
2023-05-18 19:43:43 +00:00
errors.ErrInternalError,
)
if err == mongo.ErrNoDocuments {
removeErr.SetType(errors.ErrNotFound)
}
return nil, removeErr
}
return &currencyList, nil
}
2023-05-19 05:00:31 +00:00
func (receiver *CurrencyRepository) Insert(ctx context.Context, list *models.CurrencyList) (*models.CurrencyList, errors.Error) {
2023-06-15 12:45:38 +00:00
result, err := receiver.mongoDB.InsertOne(ctx, list.Sanitize())
2023-05-19 05:00:31 +00:00
if err != nil {
receiver.logger.Error("failed to insert currency list on <Insert> of <CurrencyRepository>",
zap.Any("list", list),
zap.Error(err),
)
return nil, errors.New(
fmt.Errorf("failed to insert currency list on <Insert> of <CurrencyRepository>: %w", err),
errors.ErrInternalError,
)
}
insertedID := result.InsertedID.(primitive.ObjectID).Hex()
list.ID = insertedID
return list, nil
}