-
Notifications
You must be signed in to change notification settings - Fork 0
/
buy.go
198 lines (143 loc) · 3.89 KB
/
buy.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
package pgx
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5/pgconn"
"github.com/modern-questions-team-13/orange-stock-market/internal/database"
"github.com/modern-questions-team-13/orange-stock-market/internal/model"
"github.com/modern-questions-team-13/orange-stock-market/internal/repository/repoerrs"
"time"
)
type Buy struct {
pg *database.Postgres
}
func (b *Buy) DeleteExpired(ctx context.Context) (id []model.BidInfo, err error) {
sql := "delete from buys where now() - created_at > '1 minute' returning user_id, company_id, price"
rows, err := b.pg.Pool.Query(ctx, sql)
if err != nil {
return nil, err
}
ids := make([]model.BidInfo, 0, rows.CommandTag().RowsAffected())
for rows.Next() {
var cur model.BidInfo
err = rows.Scan(&cur.UserId, &cur.CompanyId, &cur.Price)
if err != nil {
return nil, err
}
ids = append(ids, cur)
}
return ids, nil
}
func (b *Buy) Get(ctx context.Context, id int) (model.Buy, error) {
sql, args, err := b.pg.Sq.Select("user_id", "company_id", "price", "created_at").
From("buys").
Where("id = ?", id).
ToSql()
if err != nil {
return model.Buy{}, err
}
var (
userId int
companyId int
price int
createdAt time.Time
)
err = b.pg.Pool.QueryRow(ctx, sql, args...).Scan(&userId, &companyId, &price, &createdAt)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23503" {
return model.Buy{}, fmt.Errorf("error get buy with id=%d: %w", id, repoerrs.ErrNotExists)
}
}
return model.Buy{}, err
}
return model.Buy{Id: id, UserId: userId, CompanyId: companyId, Price: price, CreatedAt: createdAt}, err
}
func (b *Buy) GetBuys(ctx context.Context, companyId int, minPrice int, limit uint64) (id []int, err error) {
sql, args, err := b.pg.Sq.Select("id").
From("buys").
Where("company_id=? and price >= ?", companyId, minPrice).
Limit(limit).ToSql()
if err != nil {
return nil, err
}
rows, err := b.pg.Pool.Query(ctx, sql, args...)
if err != nil {
return nil, err
}
ids := make([]int, 0, rows.CommandTag().RowsAffected())
for rows.Next() {
var curId int
err = rows.Scan(&curId)
if err != nil {
return nil, err
}
ids = append(ids, curId)
}
return ids, nil
}
func (b *Buy) GetAllBuys(ctx context.Context, companyId int, limit, offset uint64) (price []int, err error) {
sql, args, err := b.pg.Sq.Select("price").
From("buys").
Where("company_id=?", companyId).
Limit(limit).Offset(offset).ToSql()
if err != nil {
return nil, err
}
rows, err := b.pg.Pool.Query(ctx, sql, args...)
if err != nil {
return nil, err
}
price = make([]int, 0, rows.CommandTag().RowsAffected())
for rows.Next() {
var curPrice int
err = rows.Scan(&curPrice)
if err != nil {
return nil, err
}
price = append(price, curPrice)
}
return price, nil
}
func NewBuy(pg *database.Postgres) *Buy {
return &Buy{pg: pg}
}
func (b *Buy) Create(ctx context.Context, userId, companyId int, price int) (model.Buy, error) {
sql, args, err := b.pg.Sq.Insert("buys").
Columns("user_id", "company_id", "price").
Values(userId, companyId, price).
Suffix("returning id, created_at").
ToSql()
if err != nil {
return model.Buy{}, err
}
var (
id int
createdAt time.Time
)
err = b.pg.Pool.QueryRow(ctx, sql, args...).Scan(&id, &createdAt)
if err != nil {
return model.Buy{}, err
}
return model.Buy{Id: id, UserId: userId, CompanyId: companyId, CreatedAt: createdAt, Price: price}, nil
}
func (b *Buy) Delete(ctx context.Context, id int) (model.Buy, error) {
buy, err := b.Get(ctx, id)
if err != nil {
return model.Buy{}, err
}
sql, args, err := b.pg.Sq.Delete("buys").Where("id = ?", id).ToSql()
if err != nil {
return model.Buy{}, err
}
res, err := b.pg.Pool.Exec(ctx, sql, args...)
if err != nil {
return model.Buy{}, err
}
if res.RowsAffected() == 0 {
return model.Buy{}, repoerrs.ErrNotExists
}
return buy, nil
}