-
Notifications
You must be signed in to change notification settings - Fork 0
/
warehouse.go
388 lines (345 loc) · 6.71 KB
/
warehouse.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package app
import (
"database/sql"
// used for decimal operations
_ "github.com/shopspring/decimal"
)
// Warehouse is a warehouse interface.
// A warehouse must manage two datasets -
// one with the existing stock items and one with the stock items' distributors.
type Warehouse interface {
CreateStock(Stock)
ReadStock(string) (Stock, bool)
UpdateStock(Stock)
DeleteStock(string)
CreateDistributor(Distributor)
ReadDistributor(string) (Distributor, bool)
UpdateDistributor(Distributor)
DeleteDistributor(string)
// Stock() returns a map with the ids of the current stock items in the DB,
// mapped to the corresponding stock items
Stock() map[string]Stock
// Size() returns number of unique stock items in DB
// TODO: should return number of all stock items in DB
Size() int
}
type dafaultWarehouse struct {
database *sql.DB
}
// NewWarehouse creates a warehouse that holds the stock items'
// and distriubutors' data in two separate sqlite3 tables inside the db
// that is passed as an argument.
func NewWarehouse(db *sql.DB) Warehouse {
wh := &dafaultWarehouse{database: db}
wh.initStockTable()
wh.initDistributorsTable()
return wh
}
func (wh *dafaultWarehouse) initStockTable() {
stockTable := `
CREATE TABLE IF NOT EXISTS warehouse(
id BLOB NOT NULL PRIMARY KEY,
type TEXT NOT NULL,
name TEXT,
quantity NUMERIC NOT NULL,
min_quantity NUMERIC,
expiration_date DATETIME,
distributor_id BLOB,
FOREIGN KEY (distributor_id) REFERENCES distributors (Id)
);
`
_, err := wh.database.Exec(stockTable)
if err != nil {
panic(err)
}
}
func (wh *dafaultWarehouse) initDistributorsTable() {
distributorsTable := `
CREATE TABLE IF NOT EXISTS
distributors (
id BLOB NOT NULL PRIMARY KEY,
name TEXT);
`
_, err := wh.database.Exec(distributorsTable)
if err != nil {
panic(err)
}
}
// Database CRUD methods for stock items
// insert in DB
func (wh *dafaultWarehouse) CreateStock(item Stock) {
stmt, err := wh.database.Prepare(`
INSERT INTO
warehouse (
id,
type,
name,
quantity,
min_quantity,
expiration_date,
distributor_id)
VALUES(?, ?, ?, ?, ?, ?, ?)
`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(
item.ID(),
item.Type(),
item.Name(),
item.Quantity().String(),
item.MinQuantity().String(),
item.ExpirationDate(),
item.DistributorID())
if err != nil {
panic(err)
}
}
// read from DB
func (wh *dafaultWarehouse) ReadStock(id string) (item Stock, ok bool) {
stmt, err := wh.database.Prepare(`
SELECT
type,
name,
quantity,
min_quantity,
expiration_date,
distributor_id
FROM
warehouse
WHERE
id = ?
`)
if err != nil {
panic(err)
}
defer stmt.Close()
var (
stockItem = defaultStock{id: id}
sType int8
)
err = stmt.QueryRow(id).Scan(
&sType,
&stockItem.name,
&stockItem.quantity,
&stockItem.minQuantity,
&stockItem.expirationDate,
&stockItem.distributorID)
switch {
case err == sql.ErrNoRows:
return nil, false
case err != nil:
panic(err)
}
switch stockType(sType) {
case MEDICINE:
return &medicine{stockItem}, true
case FEED:
return &feed{stockItem}, true
case ACCESSORY:
return &accessory{stockItem}, true
default:
panic("invalid stock type in DB record")
}
}
// update in DB
func (wh *dafaultWarehouse) UpdateStock(item Stock) {
stmt, err := wh.database.Prepare(`
UPDATE
warehouse
SET
type = ?,
name = ?,
quantity = ?,
min_quantity = ?,
expiration_date = ?,
distributor_id = ?
WHERE
id = ?
`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(
item.Type(),
item.Name(),
item.Quantity().String(),
item.MinQuantity().String(),
item.ExpirationDate(),
item.DistributorID(),
item.ID())
if err != nil {
panic(err)
}
}
// remove from DB
func (wh *dafaultWarehouse) DeleteStock(id string) {
stmt, err := wh.database.Prepare(`
DELETE FROM
warehouse
WHERE
id = ?
`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(id)
if err != nil {
panic(err)
}
}
// Database CRUD methods for distributors
// insert in DB
func (wh *dafaultWarehouse) CreateDistributor(d Distributor) {
stmt, err := wh.database.Prepare(`
INSERT INTO
distributors (
id,
name)
VALUES (?, ?)
`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(
d.ID(),
d.Name())
if err != nil {
panic(err)
}
}
// read from DB
func (wh *dafaultWarehouse) ReadDistributor(id string) (Distributor, bool) {
stmt, err := wh.database.Prepare(`
SELECT
name
FROM
distributors
WHERE
id = ?
`)
if err != nil {
panic(err)
}
defer stmt.Close()
var d = defaultDistributor{id: id}
err = stmt.QueryRow(id).Scan(&d.name)
switch {
case err == sql.ErrNoRows:
return nil, false
case err != nil:
panic(err)
}
return d, true
}
// update in DB
func (wh *dafaultWarehouse) UpdateDistributor(d Distributor) {
stmt, err := wh.database.Prepare(`
UPDATE
distributors
SET
name = ?,
WHERE
id = ?
`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(
d.Name(),
d.ID())
if err != nil {
panic(err)
}
}
// remove from DB
func (wh *dafaultWarehouse) DeleteDistributor(id string) {
stmt, err := wh.database.Prepare(`
DELETE FROM
distributors
WHERE
id = ?
`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(id)
if err != nil {
panic(err)
}
}
// Returns a map with the items in the warehouse with ids as keys and stock items as their values.
func (wh *dafaultWarehouse) Stock() (stock map[string]Stock) {
stock = make(map[string]Stock)
query := `
SELECT
id,
type,
name,
quantity,
min_quantity,
expiration_date,
distributor_id
FROM
warehouse
`
rows, err := wh.database.Query(query)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var (
stockItem = defaultStock{}
sType int8
)
err = rows.Scan(
&stockItem.id,
&sType,
&stockItem.name,
&stockItem.quantity,
&stockItem.minQuantity,
&stockItem.expirationDate,
&stockItem.distributorID)
if err != nil {
panic(err)
}
switch stockType(sType) {
case MEDICINE:
stock[stockItem.ID()] = &medicine{stockItem}
case FEED:
stock[stockItem.ID()] = &feed{stockItem}
case ACCESSORY:
stock[stockItem.ID()] = &accessory{stockItem}
default:
panic("invalid stock type in DB record")
}
}
err = rows.Err()
if err != nil {
panic(err)
}
return
}
// Size returns the number of rows in the warehouse table in the DB
func (wh *dafaultWarehouse) Size() (size int) {
stmt, err := wh.database.Prepare("SELECT COUNT(*) FROM warehouse;")
if err != nil {
panic(err)
}
defer stmt.Close()
err = stmt.QueryRow().Scan(&size)
switch {
case err == sql.ErrNoRows:
return 0
case err != nil:
panic(err)
}
return
}