-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.go
127 lines (102 loc) · 3.15 KB
/
account.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
package postgres
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/helder-jaspion/go-springfield-bank/pkg/domain/model"
"github.com/helder-jaspion/go-springfield-bank/pkg/domain/repository"
)
type accountRepository struct {
db *pgxpool.Pool
}
// NewAccountRepository instantiates a new account postgres repository.
func NewAccountRepository(db *pgxpool.Pool) repository.AccountRepository {
return &accountRepository{db}
}
func (accRepo accountRepository) Create(ctx context.Context, account *model.Account) error {
var query = `
INSERT INTO
accounts (id, name, cpf, secret, balance, created_at)
VALUES
($1, $2, $3, $4, $5, $6)
`
_, err := getConnFromCtx(ctx, accRepo.db).Exec(
ctx,
query,
string(account.ID),
account.Name,
account.CPF,
account.Secret,
account.Balance,
account.CreatedAt,
)
if err != nil {
return err
}
return nil
}
func (accRepo accountRepository) ExistsByCPF(ctx context.Context, cpf model.CPF) (bool, error) {
var query = `SELECT EXISTS(SELECT id FROM accounts WHERE cpf = $1)`
accountExists := false
err := getConnFromCtx(ctx, accRepo.db).QueryRow(ctx, query, cpf).Scan(&accountExists)
return accountExists, err
}
func (accRepo accountRepository) GetByCPF(ctx context.Context, cpf model.CPF) (*model.Account, error) {
var query = "SELECT id, name, cpf, secret, balance, created_at FROM accounts WHERE cpf = $1"
account := new(model.Account)
err := getConnFromCtx(ctx, accRepo.db).QueryRow(ctx, query, cpf).Scan(&account.ID, &account.Name, &account.CPF, &account.Secret, &account.Balance, &account.CreatedAt)
if err != nil {
if err == pgx.ErrNoRows {
return nil, repository.ErrAccountNotFound
}
return nil, err
}
return account, nil
}
func (accRepo accountRepository) Fetch(ctx context.Context) ([]model.Account, error) {
var query = `
SELECT
id, name, cpf, secret, balance, created_at
FROM accounts
ORDER BY created_at asc
`
rows, err := getConnFromCtx(ctx, accRepo.db).Query(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var accounts = make([]model.Account, 0)
for rows.Next() {
var account model.Account
err := rows.Scan(&account.ID, &account.Name, &account.CPF, &account.Secret, &account.Balance, &account.CreatedAt)
if err != nil {
return nil, err
}
accounts = append(accounts, account)
}
if err := rows.Err(); err != nil {
return nil, err
}
return accounts, nil
}
func (accRepo accountRepository) GetBalance(ctx context.Context, id model.AccountID) (*model.Account, error) {
var query = "SELECT balance FROM accounts WHERE id = $1"
account := new(model.Account)
account.ID = id
err := getConnFromCtx(ctx, accRepo.db).QueryRow(ctx, query, string(id)).Scan(&account.Balance)
if err != nil {
if err == pgx.ErrNoRows {
return nil, repository.ErrAccountNotFound
}
return nil, err
}
return account, nil
}
func (accRepo accountRepository) UpdateBalance(ctx context.Context, id model.AccountID, balance model.Money) error {
query := "UPDATE accounts SET balance = $1 WHERE id = $2"
_, err := getConnFromCtx(ctx, accRepo.db).Exec(ctx, query, balance, string(id))
if err != nil {
return err
}
return nil
}