comparison pkg/controllers/importconfig.go @ 4244:4394daeea96a json-handler-middleware

Moved JSONHandler into middleware package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 22 Aug 2019 11:26:48 +0200
parents d776110b4db0
children 7b6a62d4117e
comparison
equal deleted inserted replaced
4243:d776110b4db0 4244:4394daeea96a
24 24
25 "gemma.intevation.de/gemma/pkg/auth" 25 "gemma.intevation.de/gemma/pkg/auth"
26 "gemma.intevation.de/gemma/pkg/common" 26 "gemma.intevation.de/gemma/pkg/common"
27 "gemma.intevation.de/gemma/pkg/imports" 27 "gemma.intevation.de/gemma/pkg/imports"
28 "gemma.intevation.de/gemma/pkg/scheduler" 28 "gemma.intevation.de/gemma/pkg/scheduler"
29
30 mw "gemma.intevation.de/gemma/pkg/middleware"
29 ) 31 )
30 32
31 func runImportConfig(req *http.Request) (jr JSONResult, err error) { 33 func runImportConfig(req *http.Request) (jr mw.JSONResult, err error) {
32 34
33 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) 35 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
34 36
35 ctx := req.Context() 37 ctx := req.Context()
36 38
37 var jobID int64 39 var jobID int64
38 if jobID, err = imports.RunConfiguredImportContext(ctx, JSONConn(req), id); err != nil { 40 if jobID, err = imports.RunConfiguredImportContext(ctx, mw.JSONConn(req), id); err != nil {
39 return 41 return
40 } 42 }
41 43
42 var result = struct { 44 var result = struct {
43 ID int64 `json:"id"` 45 ID int64 `json:"id"`
44 }{ 46 }{
45 ID: jobID, 47 ID: jobID,
46 } 48 }
47 49
48 jr = JSONResult{ 50 jr = mw.JSONResult{
49 Code: http.StatusCreated, 51 Code: http.StatusCreated,
50 Result: &result, 52 Result: &result,
51 } 53 }
52 return 54 return
53 } 55 }
54 56
55 func modifyImportConfig(req *http.Request) (jr JSONResult, err error) { 57 func modifyImportConfig(req *http.Request) (jr mw.JSONResult, err error) {
56 58
57 ctx := req.Context() 59 ctx := req.Context()
58 60
59 raw := JSONInput(req).(*json.RawMessage) 61 raw := mw.JSONInput(req).(*json.RawMessage)
60 62
61 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) 63 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
62 64
63 conn := JSONConn(req) 65 conn := mw.JSONConn(req)
64 66
65 var pc *imports.PersistentConfig 67 var pc *imports.PersistentConfig
66 pc, err = imports.LoadPersistentConfigContext(ctx, conn, id) 68 pc, err = imports.LoadPersistentConfigContext(ctx, conn, id)
67 switch { 69 switch {
68 case err == sql.ErrNoRows: 70 case err == sql.ErrNoRows:
69 err = JSONError{ 71 err = mw.JSONError{
70 Code: http.StatusNotFound, 72 Code: http.StatusNotFound,
71 Message: fmt.Sprintf("No configuration %d found", id), 73 Message: fmt.Sprintf("No configuration %d found", id),
72 } 74 }
73 return 75 return
74 case err != nil: 76 case err != nil:
76 } 78 }
77 79
78 kind := imports.JobKind(pc.Kind) 80 kind := imports.JobKind(pc.Kind)
79 ctor := imports.ImportModelForJobKind(kind) 81 ctor := imports.ImportModelForJobKind(kind)
80 if ctor == nil { 82 if ctor == nil {
81 err = JSONError{ 83 err = mw.JSONError{
82 Code: http.StatusInternalServerError, 84 Code: http.StatusInternalServerError,
83 Message: fmt.Sprintf("No constructor for kind '%s' found", pc.Kind), 85 Message: fmt.Sprintf("No constructor for kind '%s' found", pc.Kind),
84 } 86 }
85 return 87 return
86 } 88 }
137 ID int64 `json:"id"` 139 ID int64 `json:"id"`
138 }{ 140 }{
139 ID: id, 141 ID: id,
140 } 142 }
141 143
142 jr = JSONResult{Result: &result} 144 jr = mw.JSONResult{Result: &result}
143 return 145 return
144 } 146 }
145 147
146 func infoImportConfig(req *http.Request) (jr JSONResult, err error) { 148 func infoImportConfig(req *http.Request) (jr mw.JSONResult, err error) {
147 149
148 ctx := req.Context() 150 ctx := req.Context()
149 151
150 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) 152 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
151 153
152 var cfg *imports.PersistentConfig 154 var cfg *imports.PersistentConfig
153 155
154 cfg, err = imports.LoadPersistentConfigContext(ctx, JSONConn(req), id) 156 cfg, err = imports.LoadPersistentConfigContext(ctx, mw.JSONConn(req), id)
155 switch { 157 switch {
156 case err != nil: 158 case err != nil:
157 return 159 return
158 case cfg == nil: 160 case cfg == nil:
159 err = JSONError{ 161 err = mw.JSONError{
160 Code: http.StatusNotFound, 162 Code: http.StatusNotFound,
161 Message: fmt.Sprintf("No schedule %d found", id), 163 Message: fmt.Sprintf("No schedule %d found", id),
162 } 164 }
163 return 165 return
164 } 166 }
165 167
166 kind := imports.JobKind(cfg.Kind) 168 kind := imports.JobKind(cfg.Kind)
167 169
168 ctor := imports.ImportModelForJobKind(kind) 170 ctor := imports.ImportModelForJobKind(kind)
169 if ctor == nil { 171 if ctor == nil {
170 err = JSONError{ 172 err = mw.JSONError{
171 Code: http.StatusInternalServerError, 173 Code: http.StatusInternalServerError,
172 Message: fmt.Sprintf("No constructor for kind '%s' found", cfg.Kind), 174 Message: fmt.Sprintf("No constructor for kind '%s' found", cfg.Kind),
173 } 175 }
174 return 176 return
175 } 177 }
188 what := ctor() 190 what := ctor()
189 if err = filteredAttributes.Unmarshal(what); err != nil { 191 if err = filteredAttributes.Unmarshal(what); err != nil {
190 return 192 return
191 } 193 }
192 194
193 jr = JSONResult{Result: &imports.ImportConfigOut{ 195 jr = mw.JSONResult{Result: &imports.ImportConfigOut{
194 ID: id, 196 ID: id,
195 Kind: imports.ImportKind(cfg.Kind), 197 Kind: imports.ImportKind(cfg.Kind),
196 Config: what, 198 Config: what,
197 }} 199 }}
198 return 200 return
199 } 201 }
200 202
201 func deleteImportConfig(req *http.Request) (jr JSONResult, err error) { 203 func deleteImportConfig(req *http.Request) (jr mw.JSONResult, err error) {
202 204
203 ctx := req.Context() 205 ctx := req.Context()
204 206
205 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) 207 id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
206 208
207 var tx *sql.Tx 209 var tx *sql.Tx
208 if tx, err = JSONConn(req).BeginTx(ctx, nil); err != nil { 210 if tx, err = mw.JSONConn(req).BeginTx(ctx, nil); err != nil {
209 return 211 return
210 } 212 }
211 defer tx.Rollback() 213 defer tx.Rollback()
212 214
213 err = imports.DeletePersistentConfigurationContext( 215 err = imports.DeletePersistentConfigurationContext(
216 id, 218 id,
217 ) 219 )
218 220
219 switch { 221 switch {
220 case err == sql.ErrNoRows: 222 case err == sql.ErrNoRows:
221 err = JSONError{ 223 err = mw.JSONError{
222 Code: http.StatusNotFound, 224 Code: http.StatusNotFound,
223 Message: fmt.Sprintf("No configuration %d found", id), 225 Message: fmt.Sprintf("No configuration %d found", id),
224 } 226 }
225 return 227 return
226 case err != nil: 228 case err != nil:
238 ID int64 `json:"id"` 240 ID int64 `json:"id"`
239 }{ 241 }{
240 ID: id, 242 ID: id,
241 } 243 }
242 244
243 jr = JSONResult{Result: &result} 245 jr = mw.JSONResult{Result: &result}
244 246
245 return 247 return
246 } 248 }
247 249
248 func addImportConfig(req *http.Request) (jr JSONResult, err error) { 250 func addImportConfig(req *http.Request) (jr mw.JSONResult, err error) {
249 251
250 cfg := JSONInput(req).(*imports.ImportConfigIn) 252 cfg := mw.JSONInput(req).(*imports.ImportConfigIn)
251 253
252 kind := imports.JobKind(cfg.Kind) 254 kind := imports.JobKind(cfg.Kind)
253 255
254 ctor := imports.ImportModelForJobKind(kind) 256 ctor := imports.ImportModelForJobKind(kind)
255 if ctor == nil { 257 if ctor == nil {
256 err = JSONError{ 258 err = mw.JSONError{
257 Code: http.StatusBadRequest, 259 Code: http.StatusBadRequest,
258 Message: fmt.Sprintf("No kind %s found", string(cfg.Kind)), 260 Message: fmt.Sprintf("No kind %s found", string(cfg.Kind)),
259 } 261 }
260 return 262 return
261 } 263 }
274 pc.Attributes.Marshal(config) 276 pc.Attributes.Marshal(config)
275 277
276 ctx := req.Context() 278 ctx := req.Context()
277 279
278 var tx *sql.Tx 280 var tx *sql.Tx
279 if tx, err = JSONConn(req).BeginTx(ctx, nil); err != nil { 281 if tx, err = mw.JSONConn(req).BeginTx(ctx, nil); err != nil {
280 return 282 return
281 } 283 }
282 defer tx.Rollback() 284 defer tx.Rollback()
283 285
284 var id int64 286 var id int64
302 ID int64 `json:"id"` 304 ID int64 `json:"id"`
303 }{ 305 }{
304 ID: id, 306 ID: id,
305 } 307 }
306 308
307 jr = JSONResult{ 309 jr = mw.JSONResult{
308 Code: http.StatusCreated, 310 Code: http.StatusCreated,
309 Result: &result, 311 Result: &result,
310 } 312 }
311 return 313 return
312 } 314 }
313 315
314 func listImportConfigs(req *http.Request) (jr JSONResult, err error) { 316 func listImportConfigs(req *http.Request) (jr mw.JSONResult, err error) {
315 317
316 ctx := req.Context() 318 ctx := req.Context()
317 configs := []*imports.ImportConfigOut{} 319 configs := []*imports.ImportConfigOut{}
318 320
319 if err = imports.ListAllPersistentConfigurationsContext( 321 if err = imports.ListAllPersistentConfigurationsContext(
320 ctx, JSONConn(req), 322 ctx, mw.JSONConn(req),
321 func(config *imports.ImportConfigOut) error { 323 func(config *imports.ImportConfigOut) error {
322 configs = append(configs, config) 324 configs = append(configs, config)
323 return nil 325 return nil
324 }, 326 },
325 ); err != nil { 327 ); err != nil {
326 return 328 return
327 } 329 }
328 jr = JSONResult{Result: configs} 330 jr = mw.JSONResult{Result: configs}
329 return 331 return
330 } 332 }