Mercurial > gemma
diff 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 |
line wrap: on
line diff
--- a/pkg/controllers/importconfig.go Thu Aug 22 10:54:08 2019 +0200 +++ b/pkg/controllers/importconfig.go Thu Aug 22 11:26:48 2019 +0200 @@ -26,16 +26,18 @@ "gemma.intevation.de/gemma/pkg/common" "gemma.intevation.de/gemma/pkg/imports" "gemma.intevation.de/gemma/pkg/scheduler" + + mw "gemma.intevation.de/gemma/pkg/middleware" ) -func runImportConfig(req *http.Request) (jr JSONResult, err error) { +func runImportConfig(req *http.Request) (jr mw.JSONResult, err error) { id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) ctx := req.Context() var jobID int64 - if jobID, err = imports.RunConfiguredImportContext(ctx, JSONConn(req), id); err != nil { + if jobID, err = imports.RunConfiguredImportContext(ctx, mw.JSONConn(req), id); err != nil { return } @@ -45,28 +47,28 @@ ID: jobID, } - jr = JSONResult{ + jr = mw.JSONResult{ Code: http.StatusCreated, Result: &result, } return } -func modifyImportConfig(req *http.Request) (jr JSONResult, err error) { +func modifyImportConfig(req *http.Request) (jr mw.JSONResult, err error) { ctx := req.Context() - raw := JSONInput(req).(*json.RawMessage) + raw := mw.JSONInput(req).(*json.RawMessage) id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) - conn := JSONConn(req) + conn := mw.JSONConn(req) var pc *imports.PersistentConfig pc, err = imports.LoadPersistentConfigContext(ctx, conn, id) switch { case err == sql.ErrNoRows: - err = JSONError{ + err = mw.JSONError{ Code: http.StatusNotFound, Message: fmt.Sprintf("No configuration %d found", id), } @@ -78,7 +80,7 @@ kind := imports.JobKind(pc.Kind) ctor := imports.ImportModelForJobKind(kind) if ctor == nil { - err = JSONError{ + err = mw.JSONError{ Code: http.StatusInternalServerError, Message: fmt.Sprintf("No constructor for kind '%s' found", pc.Kind), } @@ -139,11 +141,11 @@ ID: id, } - jr = JSONResult{Result: &result} + jr = mw.JSONResult{Result: &result} return } -func infoImportConfig(req *http.Request) (jr JSONResult, err error) { +func infoImportConfig(req *http.Request) (jr mw.JSONResult, err error) { ctx := req.Context() @@ -151,12 +153,12 @@ var cfg *imports.PersistentConfig - cfg, err = imports.LoadPersistentConfigContext(ctx, JSONConn(req), id) + cfg, err = imports.LoadPersistentConfigContext(ctx, mw.JSONConn(req), id) switch { case err != nil: return case cfg == nil: - err = JSONError{ + err = mw.JSONError{ Code: http.StatusNotFound, Message: fmt.Sprintf("No schedule %d found", id), } @@ -167,7 +169,7 @@ ctor := imports.ImportModelForJobKind(kind) if ctor == nil { - err = JSONError{ + err = mw.JSONError{ Code: http.StatusInternalServerError, Message: fmt.Sprintf("No constructor for kind '%s' found", cfg.Kind), } @@ -190,7 +192,7 @@ return } - jr = JSONResult{Result: &imports.ImportConfigOut{ + jr = mw.JSONResult{Result: &imports.ImportConfigOut{ ID: id, Kind: imports.ImportKind(cfg.Kind), Config: what, @@ -198,14 +200,14 @@ return } -func deleteImportConfig(req *http.Request) (jr JSONResult, err error) { +func deleteImportConfig(req *http.Request) (jr mw.JSONResult, err error) { ctx := req.Context() id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) var tx *sql.Tx - if tx, err = JSONConn(req).BeginTx(ctx, nil); err != nil { + if tx, err = mw.JSONConn(req).BeginTx(ctx, nil); err != nil { return } defer tx.Rollback() @@ -218,7 +220,7 @@ switch { case err == sql.ErrNoRows: - err = JSONError{ + err = mw.JSONError{ Code: http.StatusNotFound, Message: fmt.Sprintf("No configuration %d found", id), } @@ -240,20 +242,20 @@ ID: id, } - jr = JSONResult{Result: &result} + jr = mw.JSONResult{Result: &result} return } -func addImportConfig(req *http.Request) (jr JSONResult, err error) { +func addImportConfig(req *http.Request) (jr mw.JSONResult, err error) { - cfg := JSONInput(req).(*imports.ImportConfigIn) + cfg := mw.JSONInput(req).(*imports.ImportConfigIn) kind := imports.JobKind(cfg.Kind) ctor := imports.ImportModelForJobKind(kind) if ctor == nil { - err = JSONError{ + err = mw.JSONError{ Code: http.StatusBadRequest, Message: fmt.Sprintf("No kind %s found", string(cfg.Kind)), } @@ -276,7 +278,7 @@ ctx := req.Context() var tx *sql.Tx - if tx, err = JSONConn(req).BeginTx(ctx, nil); err != nil { + if tx, err = mw.JSONConn(req).BeginTx(ctx, nil); err != nil { return } defer tx.Rollback() @@ -304,20 +306,20 @@ ID: id, } - jr = JSONResult{ + jr = mw.JSONResult{ Code: http.StatusCreated, Result: &result, } return } -func listImportConfigs(req *http.Request) (jr JSONResult, err error) { +func listImportConfigs(req *http.Request) (jr mw.JSONResult, err error) { ctx := req.Context() configs := []*imports.ImportConfigOut{} if err = imports.ListAllPersistentConfigurationsContext( - ctx, JSONConn(req), + ctx, mw.JSONConn(req), func(config *imports.ImportConfigOut) error { configs = append(configs, config) return nil @@ -325,6 +327,6 @@ ); err != nil { return } - jr = JSONResult{Result: configs} + jr = mw.JSONResult{Result: configs} return }