/
account_mongodb.go
executable file
·137 lines (114 loc) · 3.49 KB
/
account_mongodb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package repository
import (
"context"
"time"
"github.com/gsabadini/go-clean-architecture/domain"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type accountBSON struct {
ID string `bson:"id"`
Name string `bson:"name"`
CPF string `bson:"cpf"`
Balance int64 `bson:"balance"`
CreatedAt time.Time `bson:"created_at"`
}
type AccountNoSQL struct {
collectionName string
db NoSQL
}
func NewAccountNoSQL(db NoSQL) AccountNoSQL {
return AccountNoSQL{
db: db,
collectionName: "accounts",
}
}
func (a AccountNoSQL) Create(ctx context.Context, account domain.Account) (domain.Account, error) {
var accountBSON = accountBSON{
ID: account.ID().String(),
Name: account.Name(),
CPF: account.CPF(),
Balance: account.Balance().Int64(),
CreatedAt: account.CreatedAt(),
}
if err := a.db.Store(ctx, a.collectionName, accountBSON); err != nil {
return domain.Account{}, errors.Wrap(err, "error creating account")
}
return account, nil
}
func (a AccountNoSQL) UpdateBalance(ctx context.Context, ID domain.AccountID, balance domain.Money) error {
var (
query = bson.M{"id": ID}
update = bson.M{"$set": bson.M{"balance": balance}}
)
if err := a.db.Update(ctx, a.collectionName, query, update); err != nil {
switch err {
case mongo.ErrNilDocument:
return errors.Wrap(domain.ErrAccountNotFound, "error updating account balance")
default:
return errors.Wrap(err, "error updating account balance")
}
}
return nil
}
func (a AccountNoSQL) FindAll(ctx context.Context) ([]domain.Account, error) {
var accountsBSON = make([]accountBSON, 0)
if err := a.db.FindAll(ctx, a.collectionName, bson.M{}, &accountsBSON); err != nil {
switch err {
case mongo.ErrNilDocument:
return []domain.Account{}, errors.Wrap(domain.ErrAccountNotFound, "error listing accounts")
default:
return []domain.Account{}, errors.Wrap(err, "error listing accounts")
}
}
var accounts = make([]domain.Account, 0)
for _, accountBSON := range accountsBSON {
var account = domain.NewAccount(
domain.AccountID(accountBSON.ID),
accountBSON.Name,
accountBSON.CPF,
domain.Money(accountBSON.Balance),
accountBSON.CreatedAt,
)
accounts = append(accounts, account)
}
return accounts, nil
}
func (a AccountNoSQL) FindByID(ctx context.Context, ID domain.AccountID) (domain.Account, error) {
var (
accountBSON = &accountBSON{}
query = bson.M{"id": ID}
)
if err := a.db.FindOne(ctx, a.collectionName, query, nil, accountBSON); err != nil {
switch err {
case mongo.ErrNoDocuments:
return domain.Account{}, domain.ErrAccountNotFound
default:
return domain.Account{}, errors.Wrap(err, "error fetching account")
}
}
return domain.NewAccount(
domain.AccountID(accountBSON.ID),
accountBSON.Name,
accountBSON.CPF,
domain.Money(accountBSON.Balance),
accountBSON.CreatedAt,
), nil
}
func (a AccountNoSQL) FindBalance(ctx context.Context, ID domain.AccountID) (domain.Account, error) {
var (
accountBSON = &accountBSON{}
query = bson.M{"id": ID}
projection = bson.M{"balance": 1, "_id": 0}
)
if err := a.db.FindOne(ctx, a.collectionName, query, projection, accountBSON); err != nil {
switch err {
case mongo.ErrNoDocuments:
return domain.Account{}, domain.ErrAccountNotFound
default:
return domain.Account{}, errors.Wrap(err, "error fetching account balance")
}
}
return domain.NewAccountBalance(domain.Money(accountBSON.Balance)), nil
}