-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.go
354 lines (313 loc) · 9.02 KB
/
controllers.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
package main
import (
"encoding/json"
"net/http"
"recipe-walkthrough/models"
"strconv"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
var INVALIDSTATUS = "INVALID"
// handleBadRequest for the application
func handleBadRequest(w http.ResponseWriter, msgAndArgs ...interface{}) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(&struct {
Status string `json:"status"`
Msg interface{} `json:"msg"`
}{INVALIDSTATUS, msgAndArgs})
}
// NewRecipe initializes recipe by RECIPE_ID
// Payload: { "id": RECIPE_ID }
// Response: { "status": msg }
var NewRecipe = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Request struct {
ID int `json:"id"`
}
type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
}
req := &Request{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Error("Invalid Request in NewRecipe")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
// Setup the newRecipe
err = CurrentRecipe.newRecipe(req.ID)
if err != nil {
log.Error("Error setting up new recipe")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Msg: CurrentRecipe.CurrentStep.Data.String,
})
})
// Get all info about the CurrentStep, NextStep, and PrevStep
var GetCurrentStep = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(CurrentRecipe)
if err != nil {
log.Error("Error marshalling CurrentRecipe")
log.Error(err.Error())
handleBadRequest(w, err.Error())
}
})
// Increment N steps forward
// Payload: {"increment_steps": 1, "send_to_nlp": DEFAULT_FALSE}
var GotToNStep = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Request struct {
IncrementSteps int `json:"increment_steps"`
SendToNLP bool `json:"send_to_nlp"`
FromAction bool `json:"from_action"`
}
type Response struct {
Status string `json:"status"`
RecipeDone bool `json:"recipe_done"`
Msg string `json:"msg"`
StepInfo string `json:"step_info"`
}
req := Request{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Error("Invalid Request ")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
log.Warn(req.SendToNLP)
recipeDone, err := CurrentRecipe.incrementNSteps(req.IncrementSteps, req.FromAction)
if err != nil {
log.Errorf("Error incrementing %d steps", req.IncrementSteps)
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
if req.SendToNLP {
err = CurrentRecipe.SayCurrentStep()
if err != nil {
log.Error(err.Error())
}
}
var msg string
if recipeDone {
log.Infof("Recipe(%d) is now completed", CurrentRecipe.ID.Int64)
msg = "Recipe completed"
} else {
msg = "Go forward " + strconv.Itoa(req.IncrementSteps) + " step(s)"
}
stepInfo := ""
if CurrentRecipe.CurrentStep != nil {
stepInfo = CurrentRecipe.CurrentStep.Data.String
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
RecipeDone: recipeDone,
Msg: msg,
StepInfo: stepInfo,
})
})
var GetRecipes = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Response struct {
Status string `json:"status"`
Data []*models.Recipe `json:"recipes"`
}
recipes, err := new(models.Recipe).GetAll(database, queries)
if err != nil {
log.Error("Error getting all recipes")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: recipes,
})
})
var GetRecipeByID = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Response struct {
Status string `json:"status"`
Data *models.Recipe `json:"data"`
}
id, ok := mux.Vars(r)["id"]
if !ok {
msg := "No {ID} sent in route"
log.Error(msg)
handleBadRequest(w, msg)
return
}
idInt, err := strconv.Atoi(id)
if err != nil {
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
recipe, err := new(models.Recipe).GetByID(database, queries, idInt)
if err != nil {
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: recipe,
})
})
var GetIngredients = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Response struct {
Status string `json:"status"`
Data []*models.Ingredient `json:"ingredients"`
}
ingredients, err := new(models.Ingredient).GetAll(database, queries)
if err != nil {
log.Error("Error getting all ingredients")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: ingredients,
})
})
var GetRecipeIngredients = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type Response struct {
Status string `json:"status"`
Data []*models.Ingredient `json:"ingredients"`
}
w.Header().Set("Content-Type", "application/json")
id, ok := mux.Vars(r)["id"]
if !ok {
log.Error("Error parsing ID from request route")
handleBadRequest(w, "Error parsing ID from request route")
return
}
ingredients, err := new(models.Ingredient).GetAllByRecipe(database, queries, id)
if err != nil {
log.Errorf("Error getting all ingredients for recipe (%d)", CurrentRecipe.ID)
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: ingredients,
})
})
var GetRecipeStepIngredients = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type Response struct {
Status string `json:"status"`
Data []*models.Ingredient `json:"ingredients"`
}
w.Header().Set("Content-Type", "application/json")
id, ok := mux.Vars(r)["id"]
if !ok {
log.Error("Error parsing ID from request route")
handleBadRequest(w, "Error parsing ID from request route")
return
}
stepNumber, ok := mux.Vars(r)["step_number"]
if !ok {
log.Error("Error parsing step_number from request route")
handleBadRequest(w, "Error parsing step_number from request route")
return
}
ingredients, err := new(models.Ingredient).GetAllByRecipeStep(database, queries, id, stepNumber)
if err != nil {
log.Error("Error ingredients by step")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: ingredients,
})
})
var GetUtensils = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Response struct {
Status string `json:"status"`
Data []*models.Utensil `json:"utensils"`
}
utensils, err := new(models.Utensil).GetAll(database, queries)
if err != nil {
log.Error("Error getting all utensils")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: utensils,
})
})
var GetRecipeUtensils = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Response struct {
Status string `json:"status"`
Data []*models.Utensil `json:"utensils"`
}
id, ok := mux.Vars(r)["id"]
if !ok {
log.Error("Error parsing ID from request route")
handleBadRequest(w, "Error parsing ID from request route")
return
}
utensils, err := new(models.Utensil).GetAllByRecipe(database, queries, id)
if err != nil {
log.Error("Error getting all utensils for recipe")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: utensils,
})
})
var GetUtensilStepIngredients = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
type Response struct {
Status string `json:"status"`
Data []*models.Utensil `json:"utensils"`
}
id, ok := mux.Vars(r)["id"]
if !ok {
log.Error("Error parsing ID from request route")
handleBadRequest(w, "Error parsing ID from request route")
return
}
stepNumber, ok := mux.Vars(r)["step_number"]
if !ok {
log.Error("Error parsing step_number from request route")
handleBadRequest(w, "Error parsing step_number from request route")
return
}
utensils, err := new(models.Utensil).GetAllByRecipeStep(database, queries, id, stepNumber)
if err != nil {
log.Error("Error getting all utensils for recipe-step")
log.Error(err.Error())
handleBadRequest(w, err.Error())
return
}
json.NewEncoder(w).Encode(&Response{
Status: "success",
Data: utensils,
})
})
// Server testing controllers
var Pong = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Info("Pong!")
w.Write([]byte("Pong!\n"))
})