-
Notifications
You must be signed in to change notification settings - Fork 1
/
currency_repository.go
78 lines (66 loc) · 1.8 KB
/
currency_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package repositories
import (
"context"
"github.com/omiga-group/omiga/src/venue/shared/entities"
currencyrepo "github.com/omiga-group/omiga/src/venue/shared/entities/currency"
"github.com/omiga-group/omiga/src/venue/shared/models"
"go.uber.org/zap"
)
type CurrencyRepository interface {
CreateCurrencies(
ctx context.Context,
currencies []models.Currency) (map[string]int, error)
CreateCurrency(
ctx context.Context,
currency models.Currency) (int, error)
}
type currencyRepository struct {
logger *zap.SugaredLogger
entgoClient entities.EntgoClient
}
func NewCurrencyRepository(
logger *zap.SugaredLogger,
entgoClient entities.EntgoClient) (CurrencyRepository, error) {
return ¤cyRepository{
logger: logger,
entgoClient: entgoClient,
}, nil
}
func (cr *currencyRepository) CreateCurrencies(
ctx context.Context,
currencies []models.Currency) (map[string]int, error) {
createdCurrencies := make(map[string]int)
for _, currency := range currencies {
if savedCurrencyId, err := cr.CreateCurrency(ctx, currency); err != nil {
return nil, err
} else {
createdCurrencies[currency.Symbol] = savedCurrencyId
}
}
return createdCurrencies, nil
}
func (cr *currencyRepository) CreateCurrency(
ctx context.Context,
currency models.Currency) (int, error) {
client := cr.entgoClient.GetClient()
err := client.Currency.
Create().
SetSymbol(currency.Symbol).
SetNillableName(currency.Name).
SetType(currency.Type).
OnConflictColumns(currencyrepo.FieldSymbol).
UpdateNewValues().
Exec(ctx)
if err != nil {
cr.logger.Errorf("Failed to save currency. Error: %v", err)
return -1, err
}
savedCurrency, err := client.Currency.
Query().
Where(currencyrepo.SymbolEQ(currency.Symbol)).
First(ctx)
if err != nil {
return -1, err
}
return savedCurrency.ID, nil
}