-
Notifications
You must be signed in to change notification settings - Fork 1
/
locationHandler.go
136 lines (111 loc) · 3.52 KB
/
locationHandler.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
package api
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
/*
HTTP handler for get requests on /location
*/
func locationGetHandler(w http.ResponseWriter, r *http.Request) {
//retrieve the UserID variable
uuid := getUserID(r.Context().Value("user"))
dataJson := DynaDB.queryLocations(uuid, kitchenTable)
//Set response headers.
w.Header().Add("statusDescription", "200 OK")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(dataJson)
return
}
/*
HTTP handler for post requests on /location
*/
func locationPostHandler(w http.ResponseWriter, r *http.Request) {
uuid := getUserID(r.Context().Value("user"))
locName, ok := r.URL.Query()["location_name"]
//If we can't find the correct parameter then return a status 400.
if !ok {
fmt.Println("Location Name missing from request")
w.Header().Add("statusDescription", "400 Bad Request")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 Bad Request."))
return
}
status := DynaDB.addLocation(uuid, kitchenTable, locName[0])
//Set response headers.
//Set the HTTP status header to the one that we got from the DB add.
w.Header().Add("statusDescription", http.StatusText(status))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
return
}
/*
HTTP handler for get requests on /location/product
*/
func productGetHandler(w http.ResponseWriter, r *http.Request) {
//retrieve the UserID variable
uuid := getUserID(r.Context().Value("user"))
location := mux.Vars(r)["location"]
dataJson := queryProducts(uuid, location, kitchenTable)
//Set response headers.
w.Header().Add("statusDescription", "200 OK")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(dataJson)
return
}
/*
HTTP handler for post requests on /location/product
*/
func productPostHandler(w http.ResponseWriter, r *http.Request) {
//retrieve the UserID variable
uuid := getUserID(r.Context().Value("user"))
location := mux.Vars(r)["location"]
productName, ok := r.URL.Query()["product"]
//If we can't find the correct parameter then return a status 400.
if !ok {
fmt.Println("Location Name missing from request")
w.Header().Add("statusDescription", "400 Bad Request")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 Bad Request."))
return
}
quantity, ok := r.URL.Query()["quantity"]
//If we can't find the correct parameter then return a status 400.
if !ok {
fmt.Println("Location Name missing from request")
w.Header().Add("statusDescription", "400 Bad Request")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 Bad Request."))
return
}
addProduct(uuid, kitchenTable, location, productName[0], quantity[0])
//Set response headers.
w.Header().Add("statusDescription", "200 OK")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return
}
/*
func locationDeleteHandler(w http.ResponseWriter, r *http.Request) {
//retrieve the UserID variable
//Get the uuid pased from the authMiddleware context
user := r.Context().Value("user");
var uuid interface {}
for k, v := range user.(*jwt.Token).Claims.(jwt.MapClaims) {
if k == "sub" {
uuid = v
}
}
locName, ok := r.URL.Query()["location_name"]
//If we can't find the correct parameter then return a status 400.
if !ok {
fmt.Println("Location Name missing from request")
w.Header().Add("statusDescription", "400 Bad Request")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 Bad Request."))
return
}
deleteLocation(uuid.(string), kitchenTable, locName[0])
}*/