/
collections.go
182 lines (175 loc) · 5.34 KB
/
collections.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
package main
import (
"encoding/json"
"fmt"
"log"
"mime"
"net/http"
"github.com/ourrootsorg/cms-server/model"
)
// GetCollections returns all collections in the database
// @summary returns all collections
// @router /collections [get]
// @tags collections
// @id getCollections
// @produce application/json
// @success 200 {array} model.Collection "OK"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) GetCollections(w http.ResponseWriter, req *http.Request) {
enc := json.NewEncoder(w)
w.Header().Set("Content-Type", contentType)
cols, errors := app.api.GetCollections(req.Context())
if errors != nil {
ErrorsResponse(w, errors)
return
}
err := enc.Encode(cols)
if err != nil {
serverError(w, err)
return
}
}
// GetCollection gets a Collection from the database
// @summary gets a Collection
// @router /collections/{id} [get]
// @tags collections
// @id getCollection
// @Param id path integer true "Collection ID"
// @produce application/json
// @success 200 {object} model.Collection "OK"
// @failure 404 {object} api.Error "Not found"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) GetCollection(w http.ResponseWriter, req *http.Request) {
collID, errors := getIDFromRequest(req)
if errors != nil {
ErrorsResponse(w, errors)
return
}
enc := json.NewEncoder(w)
w.Header().Set("Content-Type", contentType)
collection, errors := app.api.GetCollection(req.Context(), collID)
if errors != nil {
ErrorsResponse(w, errors)
return
}
err := enc.Encode(collection)
if err != nil {
serverError(w, err)
return
}
}
// PostCollection adds a new Collection to the database
// @summary adds a new Collection
// @router /collections [post]
// @tags collections
// @id addCollection
// @Param collection body model.CollectionIn true "Add Collection"
// @accept application/json
// @produce application/json
// @success 201 {object} model.Collection "OK"
// @failure 415 {object} api.Error "Bad Content-Type"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) PostCollection(w http.ResponseWriter, req *http.Request) {
mt, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil || mt != contentType {
msg := fmt.Sprintf("Bad Content-Type '%s'", mt)
ErrorResponse(w, http.StatusUnsupportedMediaType, msg)
return
}
in := model.CollectionIn{}
err = json.NewDecoder(req.Body).Decode(&in)
if err != nil {
msg := fmt.Sprintf("Bad request: %v", err)
ErrorResponse(w, http.StatusBadRequest, msg)
return
}
collection, errors := app.api.AddCollection(req.Context(), in)
if errors != nil {
log.Printf("[DEBUG] PostCollection AddCollection %v\n", errors)
ErrorsResponse(w, errors)
return
}
w.Header().Set("Content-Type", contentType)
w.WriteHeader(http.StatusCreated)
enc := json.NewEncoder(w)
err = enc.Encode(collection)
if err != nil {
serverError(w, err)
return
}
}
// PutCollection updates a Collection in the database
// @summary updates a Collection
// @router /collections/{id} [put]
// @tags collections
// @id updateCollection
// @Param id path integer true "Collection ID"
// @Param collection body model.Collection true "Update Collection"
// @accept application/json
// @produce application/json
// @success 200 {object} model.Collection "OK"
// @failure 415 {object} api.Error "Bad Content-Type"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) PutCollection(w http.ResponseWriter, req *http.Request) {
collID, errors := getIDFromRequest(req)
if errors != nil {
ErrorsResponse(w, errors)
return
}
mt, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil || mt != contentType {
msg := fmt.Sprintf("Bad Content-Type '%s'", mt)
ErrorResponse(w, http.StatusUnsupportedMediaType, msg)
return
}
var in model.Collection
err = json.NewDecoder(req.Body).Decode(&in)
if err != nil {
msg := fmt.Sprintf("Bad request: %v", err)
ErrorResponse(w, http.StatusBadRequest, msg)
return
}
collection, errors := app.api.UpdateCollection(req.Context(), collID, in)
if errors != nil {
ErrorsResponse(w, errors)
return
}
w.Header().Set("Content-Type", contentType)
enc := json.NewEncoder(w)
err = enc.Encode(collection)
if err != nil {
serverError(w, err)
return
}
}
// DeleteCollection deletes a Collection from the database
// @summary deletes a Collection
// @router /collections/{id} [delete]
// @tags collections
// @id deleteCollection
// @Param id path integer true "Collection ID"
// @success 204 {object} model.Collection "OK"
// @failure 500 {object} api.Error "Server error"
// @Security OAuth2Implicit[cms,openid,profile,email]
// @Security OAuth2AuthCode[cms,openid,profile,email]
func (app App) DeleteCollection(w http.ResponseWriter, req *http.Request) {
collID, errors := getIDFromRequest(req)
if errors != nil {
ErrorsResponse(w, errors)
return
}
errors = app.api.DeleteCollection(req.Context(), collID)
if errors != nil {
ErrorsResponse(w, errors)
return
}
w.WriteHeader(http.StatusNoContent)
}