/
good_repository.go
226 lines (192 loc) · 5.7 KB
/
good_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package postgres
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/pkg/errors"
"github.com/chatbotgang/go-clean-architecture-template/internal/domain/barter"
"github.com/chatbotgang/go-clean-architecture-template/internal/domain/common"
)
type repoGood struct {
ID int `db:"id"`
Name string `db:"name"`
OwnerID int `db:"owner_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type repoColumnPatternGood struct {
ID string
Name string
OwnerID string
CreatedAt string
UpdatedAt string
}
const repoTableGood = "good"
var repoColumnGood = repoColumnPatternGood{
ID: "id",
Name: "name",
OwnerID: "owner_id",
CreatedAt: "created_at",
UpdatedAt: "updated_at",
}
func (c *repoColumnPatternGood) columns() string {
return strings.Join([]string{
c.ID,
c.Name,
c.OwnerID,
c.CreatedAt,
c.UpdatedAt,
}, ", ")
}
func (r *PostgresRepository) CreateGood(ctx context.Context, param barter.Good) (*barter.Good, common.Error) {
insert := map[string]interface{}{
repoColumnGood.Name: param.Name,
repoColumnGood.OwnerID: param.OwnerID,
}
// build SQL query
query, args, err := r.pgsq.Insert(repoTableGood).
SetMap(insert).
Suffix(fmt.Sprintf("returning %s", repoColumnGood.columns())).
ToSql()
if err != nil {
return nil, common.NewError(common.ErrorCodeInternalProcess, err)
}
// execute SQL query
var row repoGood
if err = r.db.GetContext(ctx, &row, query, args...); err != nil {
return nil, common.NewError(common.ErrorCodeRemoteProcess, err)
}
good := barter.Good(row)
return &good, nil
}
func (r *PostgresRepository) GetGoodByID(ctx context.Context, id int) (*barter.Good, common.Error) {
where := sq.And{
sq.Eq{repoColumnGood.ID: id},
}
// build SQL query
query, args, err := r.pgsq.Select(repoColumnGood.columns()).
From(repoTableGood).
Where(where).
Limit(1).
ToSql()
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, common.NewError(common.ErrorCodeResourceNotFound, err)
}
return nil, common.NewError(common.ErrorCodeInternalProcess, err)
}
// execute SQL query
var row repoGood
if err = r.db.GetContext(ctx, &row, query, args...); err != nil {
return nil, common.NewError(common.ErrorCodeRemoteProcess, err)
}
good := barter.Good(row)
return &good, nil
}
func (r *PostgresRepository) ListGoods(ctx context.Context) ([]barter.Good, common.Error) {
return r.listGoods(ctx, r.db, sq.And{})
}
func (r *PostgresRepository) ListGoodsByOwner(ctx context.Context, ownerID int) ([]barter.Good, common.Error) {
return r.listGoods(ctx, r.db, sq.And{
sq.Eq{repoColumnGood.OwnerID: ownerID},
})
}
func (r *PostgresRepository) listGoods(ctx context.Context, db sqlContextGetter, where sq.And) ([]barter.Good, common.Error) {
// build SQL query
query, args, err := r.pgsq.Select(repoColumnGood.columns()).
From(repoTableGood).
Where(where).
OrderBy(fmt.Sprintf("%s desc", repoColumnGood.CreatedAt)).
ToSql()
if err != nil {
return nil, common.NewError(common.ErrorCodeInternalProcess, err)
}
// execute SQL query
var rows []repoGood
if err = db.SelectContext(ctx, &rows, query, args...); err != nil {
return nil, common.NewError(common.ErrorCodeRemoteProcess, err)
}
var goods []barter.Good
for _, row := range rows {
good := barter.Good(row)
goods = append(goods, good)
}
return goods, nil
}
func (r *PostgresRepository) UpdateGood(ctx context.Context, good barter.Good) (updatedGood *barter.Good, err common.Error) {
// When using beginTx(), we need to make sure we have used named return values 'err'.
// Otherwise, the defer function finishTx() won't work.
tx, err := r.beginTx()
if err != nil {
return nil, err
}
defer func() {
err = r.finishTx(err, tx)
}()
return r.updateGood(ctx, tx, good)
}
func (r *PostgresRepository) UpdateGoods(ctx context.Context, goods []barter.Good) (updatedGoods []barter.Good, err common.Error) {
// When using beginTx(), we need to make sure we have used named return values 'err'.
// Otherwise, the defer function finishTx() won't work.
tx, err := r.beginTx()
if err != nil {
return nil, err
}
defer func() {
err = r.finishTx(err, tx)
}()
for i := range goods {
updatedGood, err := r.updateGood(ctx, tx, goods[i])
if err != nil {
return nil, err
}
updatedGoods = append(updatedGoods, *updatedGood)
}
return updatedGoods, nil
}
func (r *PostgresRepository) updateGood(ctx context.Context, db sqlContextGetter, good barter.Good) (*barter.Good, common.Error) {
where := sq.And{
sq.Eq{repoColumnGood.ID: good.ID},
}
update := map[string]interface{}{
repoColumnGood.Name: good.Name,
repoColumnGood.OwnerID: good.OwnerID,
repoColumnGood.UpdatedAt: time.Now(),
}
// build SQL query
query, args, err := r.pgsq.Update(repoTableGood).
SetMap(update).
Where(where).
Suffix(fmt.Sprintf("returning %s", repoColumnGood.columns())).
ToSql()
if err != nil {
return nil, common.NewError(common.ErrorCodeInternalProcess, err)
}
// execute SQL query
var row repoGood
if err = db.GetContext(ctx, &row, query, args...); err != nil {
return nil, common.NewError(common.ErrorCodeRemoteProcess, err)
}
updatedGood := barter.Good(row)
return &updatedGood, nil
}
func (r *PostgresRepository) DeleteGoodByID(ctx context.Context, id int) common.Error {
where := sq.And{
sq.Eq{repoColumnGood.ID: id},
}
// build SQL query
query, args, err := r.pgsq.Delete(repoTableGood).
Where(where).
ToSql()
if err != nil {
return common.NewError(common.ErrorCodeInternalProcess, err)
}
// execute SQL query
if _, err = r.db.ExecContext(ctx, query, args...); err != nil {
return common.NewError(common.ErrorCodeRemoteProcess, err)
}
return nil
}