/
catering_handlers.go
114 lines (102 loc) · 3.57 KB
/
catering_handlers.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
package main
import (
"encoding/json"
"github.com/ckpt/backend-services/caterings"
"github.com/m4rw3r/uuid"
"github.com/zenazn/goji/web"
"net/http"
)
func createNewCatering(c web.C, w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
nCatering := new(caterings.Catering)
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(nCatering); err != nil {
return &appError{err, "Invalid JSON", 400}
}
nCatering, err := caterings.NewCatering(nCatering.Tournament, nCatering.Info)
if err != nil {
return &appError{err, "Failed to create new catering", 500}
}
w.Header().Set("Location", "/caterings/"+nCatering.UUID.String())
w.WriteHeader(201)
return nil
}
func listAllCaterings(c web.C, w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
list, err := caterings.AllCaterings()
if err != nil {
return &appError{err, "Cant load caterings", 500}
}
encoder := json.NewEncoder(w)
encoder.Encode(list)
return nil
}
func getCatering(c web.C, w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
uuid, err := uuid.FromString(c.URLParams["uuid"])
catering, err := caterings.CateringByUUID(uuid)
if err != nil {
return &appError{err, "Cant find catering", 404}
}
encoder := json.NewEncoder(w)
encoder.Encode(catering)
return nil
}
func updateCateringInfo(c web.C, w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
uuid, err := uuid.FromString(c.URLParams["uuid"])
catering, err := caterings.CateringByUUID(uuid)
if err != nil {
return &appError{err, "Cant find catering", 404}
}
tempInfo := new(caterings.Info)
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(tempInfo); err != nil {
return &appError{err, "Invalid JSON", 400}
}
if err := catering.UpdateInfo(*tempInfo); err != nil {
return &appError{err, "Failed to update catering info", 500}
}
w.WriteHeader(204)
return nil
}
func addCateringVote(c web.C, w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
uuid, err := uuid.FromString(c.URLParams["uuid"])
catering, err := caterings.CateringByUUID(uuid)
if err != nil {
return &appError{err, "Cant find catering", 404}
}
tempInfo := new(caterings.Vote)
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(tempInfo); err != nil {
return &appError{err, "Invalid JSON", 400}
}
if err := catering.AddVote(tempInfo.Player, tempInfo.Score); err != nil {
return &appError{err, "Failed to add catering vote", 500}
}
w.WriteHeader(204)
return nil
}
func updateCateringVote(c web.C, w http.ResponseWriter, r *http.Request) *appError {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
cateringuuid, err := uuid.FromString(c.URLParams["uuid"])
playeruuid, err := uuid.FromString(c.URLParams["playeruuid"])
catering, err := caterings.CateringByUUID(cateringuuid)
if err != nil {
return &appError{err, "Cant find catering", 404}
}
tempInfo := new(caterings.Vote)
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(tempInfo); err != nil {
return &appError{err, "Invalid JSON", 400}
}
if err := catering.RemoveVote(playeruuid); err != nil {
return &appError{err, "Failed to remove old catering vote when updating", 500}
}
if err := catering.AddVote(playeruuid, tempInfo.Score); err != nil {
return &appError{err, "Failed to add updated catering vote", 500}
}
w.WriteHeader(204)
return nil
}