-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchase.go
73 lines (64 loc) · 1.32 KB
/
purchase.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
package main
import (
"fmt"
"log"
"net/http"
)
func packInfoHandler(w http.ResponseWriter, r *http.Request) {
var (
userID int
err error
)
if NeedAuth {
userID, err = verifyBearerAuth(r.Header.Get("Authorization"))
if err != nil {
c := Container{false, nil, 203}
http.Error(w, c.toJSON(), http.StatusUnauthorized)
return
}
} else {
userID = staticUserID
}
tojson, err := getPackInfo(userID, r)
if err != nil {
log.Println(err)
}
fmt.Fprint(w, tojson.toJSON())
}
func getPackInfo(_ int, _ *http.Request) (ToJSON, error) {
container := []PackInfo{}
rows, err := db.Query(sqlStmtPackInfo)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
info := new(PackInfo)
rows.Scan(
&info.Name,
&info.Price,
&info.OrigPrice,
&info.DiscountFrom,
&info.DiscountTo,
)
items := []PackItem{}
itemRows, err := db.Query(sqlStmtPackItem, info.Name)
if err != nil {
return nil, err
}
defer itemRows.Close()
for itemRows.Next() {
item := new(PackItem)
itemRows.Scan(&item.ID, &item.ItemType, &item.IsAvailable)
items = append(items, *item)
}
if err := itemRows.Err(); err != nil {
return nil, err
}
container = append(container, *info)
}
if err := rows.Err(); err != nil {
return nil, err
}
return (*PackInfoContainer)(&container), nil
}