-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_suggestions.go
374 lines (320 loc) · 10.1 KB
/
input_suggestions.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package buna
import (
"context"
"database/sql"
"fmt"
"reflect"
)
// limit determines the number of strings in the returned slice.
// The first suggestions are the most recently brewed coffees.
// The last suggestion is the most recently purchased coffee (The last two if limit > 5).
func (s *SQLiteDB) getCoffeeNameSuggestions(ctx context.Context, limit int) ([]string, error) {
brewedLimit := limit - 1
purchasedLimit := 1
if limit > 5 {
brewedLimit--
purchasedLimit++
}
var names []string
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
bRows, err := tx.QueryContext(ctx, `
SELECT DISTINCT c.name
FROM brewings as b
INNER JOIN coffees as c
ON b.coffee_id = c.id
ORDER BY b.id DESC
LIMIT :limit
`,
sql.Named("limit", brewedLimit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve recently brewed coffee names: %w", err)
}
defer bRows.Close()
for bRows.Next() {
var name string
if err := bRows.Scan(&name); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan bRow: %w", err)
}
names = append(names, name)
}
if err := bRows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last bRow: %w", err)
}
pRows, err := tx.QueryContext(ctx, `
SELECT DISTINCT c.name
FROM purchases as p
INNER JOIN coffees as c
ON p.coffee_id = c.id
ORDER BY p.id DESC
LIMIT :limit
`,
sql.Named("limit", purchasedLimit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve recently purchased coffee names: %w", err)
}
defer pRows.Close()
for pRows.Next() {
var name string
if err := pRows.Scan(&name); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan pRow: %w", err)
}
names = append(names, name)
}
if err := pRows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last pRow: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: input_suggestions: getCoffeeNameSuggestions transaction failed: %w", err)
}
names = removeStrDuplicates(names)
return names, nil
}
func (s *SQLiteDB) getLastCoffeeRoastDate(ctx context.Context, coffeeName string) (date, error) {
var (
dateStr string
isEmpty bool
)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT p.roast_date
FROM purchases as p
INNER JOIN coffees as c
ON p.coffee_id = c.id
WHERE c.name = :coffeeName
ORDER BY p.id DESC
LIMIT 1
`,
sql.Named("coffeeName", coffeeName),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve roast date: %w", err)
}
defer rows.Close()
// Check if no rows available
if ok := rows.Next(); !ok {
isEmpty = true
return nil
}
var val interface{}
if err := rows.Scan(&val); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan row: %w", err)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last row: %w", err)
}
// Deal with possible NULL values
if v := reflect.ValueOf(val); v.Kind() == reflect.String {
dateStr = val.(string)
} else {
isEmpty = true
return nil
}
return nil
}); err != nil {
return date{}, fmt.Errorf("buna: input_suggestions: getLastCoffeeRoastDate transaction failed: %w", err)
}
if isEmpty {
return date{}, nil
}
roastDate, err := createDateFromDateString(dateStr)
if err != nil {
return date{}, fmt.Errorf("buna: input_suggestions: failed to convert dateStr into date: %w", err)
}
return roastDate, nil
}
// limit determines the number of strings in the returned slice.
func (s *SQLiteDB) getMostRecentlyUsedBrewingMethodNames(ctx context.Context, limit int) ([]string, error) {
var names []string
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT DISTINCT m.name
FROM brewings as b
INNER JOIN brewing_methods as m
ON b.method_id = m.id
ORDER BY b.id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve brewing method name rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan row: %w", err)
}
names = append(names, name)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: input_suggestions: getMostRecentlyUsedBrewingMethodNames transaction failed: %w", err)
}
return names, nil
}
// limit determines the number of strings in the returned slice.
func (s *SQLiteDB) getMostRecentlyUsedCoffeeGrinderNames(ctx context.Context, limit int) ([]string, error) {
var names []string
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT DISTINCT g.name
FROM brewings as b
INNER JOIN grinders as g
ON b.grinder_id = g.id
ORDER BY b.id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve coffee grinder name rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan row: %w", err)
}
names = append(names, name)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: input_suggestions: getMostRecentlyUsedCoffeeGrinderNames transaction failed: %w", err)
}
return names, nil
}
// limit determines the number of strings in the returned slice.
// Weight is in grams.
func (s *SQLiteDB) getMostRecentlyUsedCoffeeWeights(ctx context.Context, brewingMethodName string, coffeeGrinderName string, limit int) ([]float64, error) {
var weights []float64
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT DISTINCT b.coffee_grams
FROM brewings as b
INNER JOIN brewing_methods as m
ON b.method_id = m.id
INNER JOIN grinders as g
ON b.grinder_id = g.id
WHERE m.name = :brewingMethodName AND g.name = :coffeeGrinderName
ORDER BY b.id DESC
LIMIT :limit
`,
sql.Named("brewingMethodName", brewingMethodName),
sql.Named("coffeeGrinderName", coffeeGrinderName),
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve coffee weight rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var weight float64
if err := rows.Scan(&weight); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan row: %w", err)
}
weights = append(weights, weight)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: input_suggestions: getMostRecentlyUsedCoffeeWeights transaction failed: %w", err)
}
return weights, nil
}
// limit determines the number of strings in the returned slice.
// Weight is in grams.
func (s *SQLiteDB) getMostRecentlyUsedWaterWeights(ctx context.Context, brewingMethodName string, coffeeGrinderName string, limit int) ([]float64, error) {
var weights []float64
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT DISTINCT b.water_grams
FROM brewings as b
INNER JOIN brewing_methods as m
ON b.method_id = m.id
INNER JOIN grinders as g
ON b.grinder_id = g.id
WHERE m.name = :brewingMethodName AND g.name = :coffeeGrinderName
ORDER BY b.id DESC
LIMIT :limit
`,
sql.Named("brewingMethodName", brewingMethodName),
sql.Named("coffeeGrinderName", coffeeGrinderName),
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve coffee weight rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var weight float64
if err := rows.Scan(&weight); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan row: %w", err)
}
weights = append(weights, weight)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: input_suggestions: getMostRecentlyUsedCoffeeWeights transaction failed: %w", err)
}
return weights, nil
}
// limit determines the number of strings in the returned slice.
func (s *SQLiteDB) getRoastersByCoffeeName(ctx context.Context, name string, limit int) ([]string, error) {
var roasters []string
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT roaster
FROM coffees
WHERE name = :name
ORDER BY id DESC
LIMIT :limit
`,
sql.Named("name", name),
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: input_suggestions: failed to retrieve coffee roaster rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var roaster string
if err := rows.Scan(&roaster); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan row: %w", err)
}
roasters = append(roasters, roaster)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: input_suggestions: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: input_suggestions: getRoastersByCoffeeName transaction failed: %w", err)
}
return roasters, nil
}
func removeStrDuplicates(strings []string) []string {
keys := make(map[string]bool)
var res []string
for _, entry := range strings {
if _, value := keys[entry]; !value {
keys[entry] = true
res = append(res, entry)
}
}
return res
}