/
postgres_order_repo.go
104 lines (92 loc) · 3.13 KB
/
postgres_order_repo.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
package adapters
import (
"context"
"fmt"
"github.com/bensivo/hexagonal-architecture-study/internal/orders"
"github.com/jackc/pgx/v5"
"go.uber.org/zap"
)
// Implementation of OrderRepository using a postgres database as the storage mechanism.
//
// NOTE: this repo does not bootstrap the tables it uses, look at postgres/migrations.go for the migration which creates the order table.
// In a bigger project, each domain module would bootstrap its own tables when the adapter is initialized - using a different schema for each domain module.
type PostgresOrderRepo struct {
conn *pgx.Conn // TODO: we should be using a connection pool here, instead of a single connection
logger *zap.SugaredLogger
}
var _ orders.OrderRepository = (*PostgresOrderRepo)(nil)
func NewPostgresOrderRepo(conn *pgx.Conn, logger *zap.SugaredLogger) *PostgresOrderRepo {
return &PostgresOrderRepo{
conn: conn,
logger: logger,
}
}
func (por *PostgresOrderRepo) Save(order *orders.Order) error {
rows, err := por.conn.Query(
context.Background(),
"INSERT INTO orders (id, product, quantity, status) VALUES (@id, @product, @quantity, @status)",
pgx.NamedArgs{
"id": order.ID,
"product": order.Product,
"quantity": order.Quantity,
"status": order.Status,
},
)
if err != nil {
por.logger.Error("Failed inserting order: ", err)
return err
}
rows.Close()
return nil
}
func (por *PostgresOrderRepo) GetMany() ([]orders.Order, error) {
res := []orders.Order{}
rows, err := por.conn.Query(context.Background(), "SELECT id, product, quantity, status from orders")
if err != nil {
por.logger.Error("Failed querying orders")
return nil, err
}
for rows.Next() {
var order_row struct {
id [16]byte // postgres stores UUIDs as byte arrays, not strings
product string
quantity int
status string
}
err = rows.Scan(&order_row.id, &order_row.product, &order_row.quantity, &order_row.status)
if err != nil {
por.logger.Error("Failed scanning row: ", err)
return nil, err
}
res = append(res, orders.Order{
ID: fmt.Sprintf("%x-%x-%x-%x-%x", order_row.id[0:4], order_row.id[4:6], order_row.id[6:8], order_row.id[8:10], order_row.id[10:16]),
Product: order_row.product,
Quantity: order_row.quantity,
Status: orders.OrderStatus(order_row.status),
})
}
rows.Close()
return res, nil
}
func (por *PostgresOrderRepo) GetOne(id string) (*orders.Order, error) {
row := por.conn.QueryRow(context.Background(), "SELECT id, product, quantity, status from orders where id = @id", pgx.NamedArgs{
"id": id,
})
var order_row struct {
id [16]byte // postgres stores UUIDs as byte arrays, not strings
product string
quantity int
status string
}
err := row.Scan(&order_row.id, &order_row.product, &order_row.quantity, &order_row.status)
if err != nil {
por.logger.Error("Failed scanning row: ", err)
return nil, err
}
return &orders.Order{
ID: fmt.Sprintf("%x-%x-%x-%x-%x", order_row.id[0:4], order_row.id[4:6], order_row.id[6:8], order_row.id[8:10], order_row.id[10:16]),
Product: order_row.product,
Quantity: order_row.quantity,
Status: orders.OrderStatus(order_row.status),
}, nil
}