-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtopics.go
175 lines (161 loc) Β· 4.06 KB
/
topics.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/gocraft/web"
"github.com/ubyssey/chatbotmgmt/models"
mgo "gopkg.in/mgo.v2"
)
// GET /topics
func (reqctx *RequestContext) ListTopics(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
topics := make([]models.Topic, 0)
if err := models.GetAllTopics(ctx, &topics); err != nil {
rw.WriteHeader(500)
return
}
j, err := json.Marshal(map[string](interface{}){
"results": topics,
})
if err != nil {
log.Print("list topics: failed to encode as json: ", err)
rw.WriteHeader(500)
return
}
fmt.Fprint(rw, string(j))
}
// POST /topics
func (reqctx *RequestContext) CreateTopic(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
decoder := json.NewDecoder(req.Body)
var t models.Topic
if err := decoder.Decode(&t); err != nil {
rw.WriteHeader(400)
fmt.Fprint(rw, "the request body could not be parsed as json or contained an improperly formatted field")
return
}
t.UUID = nil // ignore a uuid if they gave us one
if err := t.Save(ctx); err != nil {
switch err.(type) {
case *models.ValidationError:
rw.WriteHeader(400)
fmt.Fprint(rw, err)
default:
log.Print("create topic: internal error: ", err)
rw.WriteHeader(500)
}
return
}
top_url := fmt.Sprintf("/topics/%s", *t.UUID)
rw.Header().Set("Location", top_url)
rw.WriteHeader(201)
}
// GET /topics/:uuid
func (reqctx *ResourceRequestContext) GetTopic(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
var t models.Topic
err := t.GetById(ctx, reqctx.rid)
if err != nil {
if err == mgo.ErrNotFound {
rw.WriteHeader(404)
} else {
rw.WriteHeader(500)
log.Print(err)
}
return
}
j, err := json.Marshal(t)
if err != nil {
log.Print("get topic: failed to encode as json: ", err)
rw.WriteHeader(500)
return
}
fmt.Fprint(rw, string(j))
}
// PATCH /topics/:uuid
func (reqctx *ResourceRequestContext) UpdateTopic(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
decoder := json.NewDecoder(req.Body)
var t models.Topic
if err := decoder.Decode(&t); err != nil {
rw.WriteHeader(400)
fmt.Fprint(rw, "the request body could not be parsed as json or contained an improperly formatted field")
return
}
// enforce update rules, ensure t.UUID is populated
if t.UUID != nil {
if *t.UUID != reqctx.rid { // enforce body/url uuid matching
rw.WriteHeader(400)
fmt.Fprint(rw, "the uuid values in the url and request body must match")
return
}
} else {
t.UUID = new(string)
*t.UUID = reqctx.rid
}
if t.VersionUUID == nil {
rw.WriteHeader(400)
fmt.Fprint(rw, "a version uuid must be provided")
return
}
// save the record
if err := t.Save(ctx); err != nil {
rw.WriteHeader(500)
log.Print(err)
return
}
// return the full record as the response body
if err := t.GetById(ctx, reqctx.rid); err != nil {
rw.WriteHeader(500)
log.Print(err)
return
}
j, err := json.Marshal(t)
if err != nil {
log.Print("update topic: failed to encode as json: ", err)
rw.WriteHeader(500)
return
}
fmt.Fprint(rw, string(j))
}
// DELETE /topics/:uuid
func (reqctx *ResourceRequestContext) DeleteTopic(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
decoder := json.NewDecoder(req.Body)
var t models.Topic
if err := decoder.Decode(&t); err != nil {
rw.WriteHeader(400)
fmt.Fprint(rw, "the request body could not be parsed as json or contained an improperly formatted field")
return
}
if t.UUID == nil {
t.UUID = &reqctx.rid
} else {
if *t.UUID != reqctx.rid {
rw.WriteHeader(400)
fmt.Fprint(rw, "the uuid values in the body and url must match")
return
}
}
err := t.Delete(ctx)
if err != nil {
if err == mgo.ErrNotFound {
rw.WriteHeader(404)
return
}
switch err.(type) {
case *models.ValidationError:
rw.WriteHeader(400)
fmt.Fprint(rw, err)
case *models.DependentResourceError:
rw.WriteHeader(412)
fmt.Fprint(rw, err) // TODO this error message should be formatted according to spec
default:
rw.WriteHeader(500)
}
return
}
rw.WriteHeader(204)
}