-
Notifications
You must be signed in to change notification settings - Fork 6
/
purchaseOrders.go
77 lines (63 loc) · 1.39 KB
/
purchaseOrders.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
package db
import "errors"
// the abbreviation PO will be used in place of PurchaseOrder often for brevity
func CreatePO(book, customer int) (int, error) {
database := Connect().Db
database.Exec(`
INSERT INTO PurchaseOrders (bookId, customerId, shipped)
VALUES (?, ?, 0);
`, book, customer)
return GetPOByContents(book, customer)
}
func GetPOByContents(book, customer int) (int, error) {
database := Connect().Db
rows, err := database.Query(`
SELECT (id) FROM PurchaseOrders
WHERE bookId = ? and customerId = ?;
`, book, customer)
if err != nil {
return 0, err
}
defer rows.Close()
var pid int
err = rows.Scan(&pid)
if err != nil {
return 0, err
}
return pid, nil
}
func IsPOShipped(pid int) (bool, error) {
database := Connect().Db
rows, err := database.Query(`
SELECT (shipped) FROM Books
WHERE id = ?;
`, pid)
if err != nil {
return false, err
}
defer rows.Close()
var shipped int
err = rows.Scan(&shipped)
if err != nil {
return false, err
}
return shipped == 1, nil
}
func ShipPO(pid int) error {
database := Connect().Db
if isShipped, err := IsPOShipped(pid); err == nil && isShipped {
return errors.New("purchase order already shipped")
} else if err != nil {
return err
}
err := ChargeCustomerForPO(pid)
if err != nil {
return err
}
_, err = database.Exec(`
UPDATE PurchaseOrders
SET shipped = 1
WHERE id = ?;
`, pid)
return err
}