/
repository.go
167 lines (143 loc) · 4.61 KB
/
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package clientdb
import (
"context"
"time"
"github.com/gofiber/fiber/v3/log"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/ryrden/rinha-de-backend-go/internal/app/data/models"
"github.com/ryrden/rinha-de-backend-go/internal/app/domain/client"
)
type ClientRepository struct {
db *pgxpool.Pool
}
func (c *ClientRepository) CreateTransaction(clientID string, value int, kind string, description string) (*models.ClientTransactionResponse, error) {
log.Infof("Creating transaction for client %s", clientID)
// Start a transaction
log.Info("Starting transaction")
tx, err := c.db.Begin(context.Background())
if err != nil {
log.Errorf("Error starting transaction: %s", err)
return nil, err
}
defer tx.Rollback(context.Background())
// Fetch client from database
log.Infof("Attempting to find client by ID: %s", clientID)
var clientResult client.Client
log.Infof("Fetching client from database: %s", clientID)
err = tx.QueryRow(
context.Background(),
PessimistLockingClientQuery,
clientID,
).Scan(&clientResult.ID, &clientResult.Limit, &clientResult.Balance)
if err != nil {
if err == pgx.ErrNoRows {
return nil, client.ErrClientNotFound
}
log.Errorf("Error finding client by ID: %s, error: %s", clientID, err)
return nil, err
}
// Check if client can afford the transaction
log.Infof("Checking if client can afford the transaction: %s, value: %d, kind: %s", clientID, value, kind)
if !clientResult.CanAfford(value, kind) {
log.Infof("Client cannot afford the transaction: %s, value: %d, kind: %s", clientID, value, kind)
return nil, client.ErrClientCannotAfford
}
// Update client balance
clientResult.AddTransaction(value, kind)
log.Infof("Updating client balance: %s", clientID)
_, err = tx.Exec(
context.Background(),
UpdateClientBalanceQuery,
clientResult.Balance,
clientResult.ID,
)
if err != nil {
log.Errorf("Error updating client: %s, error: %s", clientID, err)
return nil, err
}
// Insert a transcation record
log.Infof("Inserting transaction record for client: %s, value: %d, kind: %s", clientID, value, kind)
_, err = tx.Exec(
context.Background(),
InsertTransactionQuery,
clientID,
value,
kind,
description,
)
if err != nil {
log.Errorf("Error creating transaction for client ID: %s, error: %s", clientID, err)
return nil, err
}
log.Info("Committing transaction")
err = tx.Commit(context.Background())
if err != nil {
log.Errorf("Error committing transaction: %s", err)
return nil, err
}
log.Infof("Transaction successfully created for client ID: %s, value: %d, kind: %s", clientID, value, kind)
return &models.ClientTransactionResponse{
Limit: clientResult.Limit,
Balance: clientResult.Balance,
}, nil
}
func (c *ClientRepository) GetClientExtract(clientID string) (*models.GetClientExtractResponse, error) {
log.Infof("Starting to get client extract for clientID: %s", clientID)
var transactions = new(models.GetClientExtractResponse)
var clientResult client.Client
log.Infof("Fetching client from database: %s", clientID)
err := c.db.QueryRow(
context.Background(),
GetClientQuery,
clientID,
).Scan(&clientResult.ID, &clientResult.Limit, &clientResult.Balance)
if err != nil {
if err == pgx.ErrNoRows {
return nil, client.ErrClientNotFound
}
log.Errorf("Error finding client by ID: %s, error: %s", clientID, err)
return nil, err
}
transactions.Balance = models.Balance{
Total: clientResult.Balance,
ExtractedAt: time.Now().Format(time.RFC3339),
Limit: clientResult.Limit,
}
log.Infof("Fetching last 10 transactions for clientID: %s", clientID)
rows, err := c.db.Query(
context.Background(),
GetClientExtractQuery,
clientID,
)
if err != nil {
log.Errorf("Error getting client extract for clientID: %s, error: %s", clientID, err)
return nil, err
}
defer rows.Close()
transactions.Transactions = make([]models.Transaction, 0)
for rows.Next() {
var amount int
var kind string
var description string
var createdAt time.Time
if err := rows.Scan(&amount, &kind, &description, &createdAt); err != nil {
log.Errorf("Error scanning transaction for clientID: %s, error: %s", clientID, err)
return nil, err
}
transaction := models.Transaction{
Value: amount,
Kind: kind,
Description: description,
CreatedAt: createdAt.Format("2006-01-02T15:04:05.000000Z"),
}
transactions.Transactions = append(transactions.Transactions, transaction)
}
log.Infof("Successfully retrieved client extract for clientID: %s", clientID)
return transactions, nil
}
func NewClientRepository(db *pgxpool.Pool) client.Repository {
return &ClientRepository{
db: db,
}
}