Mercurial > gemma
changeset 1667:aaa05d3c4aac
Deduplicated code for triggering manual imports.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 23 Dec 2018 20:38:52 +0100 |
parents | 56b29406a163 |
children | eb832d81b67a |
files | pkg/controllers/bnimports.go pkg/controllers/faimports.go pkg/controllers/gmimports.go pkg/controllers/manualimports.go pkg/controllers/routes.go |
diffstat | 5 files changed, 102 insertions(+), 212 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/bnimports.go Sun Dec 23 19:43:58 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -// This is Free Software under GNU Affero General Public License v >= 3.0 -// without warranty, see README.md and license for details. -// -// SPDX-License-Identifier: AGPL-3.0-or-later -// License-Filename: LICENSES/AGPL-3.0.txt -// -// Copyright (C) 2018 by via donau -// – Österreichische Wasserstraßen-Gesellschaft mbH -// Software engineering by Intevation GmbH -// -// Author(s): -// * Sascha L. Teichmann <sascha.teichmann@intevation.de> - -package controllers - -import ( - "database/sql" - "log" - "net/http" - - "gemma.intevation.de/gemma/pkg/auth" - "gemma.intevation.de/gemma/pkg/common" - "gemma.intevation.de/gemma/pkg/imports" - "gemma.intevation.de/gemma/pkg/models" -) - -func importBottleneck( - input interface{}, - req *http.Request, - conn *sql.Conn, -) (jr JSONResult, err error) { - - bi := input.(*models.BottleneckImport) - - bn := &imports.Bottleneck{ - URL: bi.URL, - Insecure: bi.Insecure, - } - - var serialized string - if serialized, err = common.ToJSONString(bn); err != nil { - return - } - - session, _ := auth.GetSession(req) - - var jobID int64 - if jobID, err = imports.AddJob( - imports.BNJobKind, session.User, - bi.SendEmail, false, - serialized, - ); err != nil { - return - } - - log.Printf("info: added import #%d to queue\n", jobID) - - result := struct { - ID int64 `json:"id"` - }{ - ID: jobID, - } - - jr = JSONResult{ - Code: http.StatusCreated, - Result: &result, - } - return -}
--- a/pkg/controllers/faimports.go Sun Dec 23 19:43:58 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -// This is Free Software under GNU Affero General Public License v >= 3.0 -// without warranty, see README.md and license for details. -// -// SPDX-License-Identifier: AGPL-3.0-or-later -// License-Filename: LICENSES/AGPL-3.0.txt -// -// Copyright (C) 2018 by via donau -// – Österreichische Wasserstraßen-Gesellschaft mbH -// Software engineering by Intevation GmbH -// -// Author(s): -// * Raimund Renkert <raimund.renkert@intevation.de> - -package controllers - -import ( - "database/sql" - "log" - "net/http" - - "gemma.intevation.de/gemma/pkg/auth" - "gemma.intevation.de/gemma/pkg/common" - "gemma.intevation.de/gemma/pkg/imports" - "gemma.intevation.de/gemma/pkg/models" -) - -func importFairwayAvailability( - input interface{}, - req *http.Request, - conn *sql.Conn, -) (jr JSONResult, err error) { - - fai := input.(*models.FairwayAvailabilityImport) - - fa := &imports.FairwayAvailability{ - URL: fai.URL, - Insecure: fai.Insecure, - } - - var serialized string - if serialized, err = common.ToJSONString(fa); err != nil { - return - } - - session, _ := auth.GetSession(req) - - var jobID int64 - if jobID, err = imports.AddJob( - imports.FAJobKind, - session.User, - fai.SendEmail, true, - serialized, - ); err != nil { - return - } - - log.Printf("info: added import #%d to queue\n", jobID) - - result := struct { - ID int64 `json:"id"` - }{ - ID: jobID, - } - - jr = JSONResult{ - Code: http.StatusCreated, - Result: &result, - } - return -}
--- a/pkg/controllers/gmimports.go Sun Dec 23 19:43:58 2018 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -// This is Free Software under GNU Affero General Public License v >= 3.0 -// without warranty, see README.md and license for details. -// -// SPDX-License-Identifier: AGPL-3.0-or-later -// License-Filename: LICENSES/AGPL-3.0.txt -// -// Copyright (C) 2018 by via donau -// – Österreichische Wasserstraßen-Gesellschaft mbH -// Software engineering by Intevation GmbH -// -// Author(s): -// * Raimund Renkert <raimund.renkert@intevation.de> - -package controllers - -import ( - "database/sql" - "log" - "net/http" - - "gemma.intevation.de/gemma/pkg/auth" - "gemma.intevation.de/gemma/pkg/common" - "gemma.intevation.de/gemma/pkg/imports" - "gemma.intevation.de/gemma/pkg/models" -) - -func importGaugeMeasurement( - input interface{}, - req *http.Request, - conn *sql.Conn, -) (jr JSONResult, err error) { - - gi := input.(*models.GaugeMeasurementImport) - - gm := &imports.GaugeMeasurement{ - URL: gi.URL, - Insecure: gi.Insecure, - } - - var serialized string - if serialized, err = common.ToJSONString(gm); err != nil { - return - } - - session, _ := auth.GetSession(req) - - var jobID int64 - if jobID, err = imports.AddJob( - imports.GMJobKind, - session.User, - gi.SendEmail, true, - serialized, - ); err != nil { - return - } - - log.Printf("info: added import #%d to queue\n", jobID) - - result := struct { - ID int64 `json:"id"` - }{ - ID: jobID, - } - - jr = JSONResult{ - Code: http.StatusCreated, - Result: &result, - } - return -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/controllers/manualimports.go Sun Dec 23 20:38:52 2018 +0100 @@ -0,0 +1,96 @@ +// This is Free Software under GNU Affero General Public License v >= 3.0 +// without warranty, see README.md and license for details. +// +// SPDX-License-Identifier: AGPL-3.0-or-later +// License-Filename: LICENSES/AGPL-3.0.txt +// +// Copyright (C) 2018 by via donau +// – Österreichische Wasserstraßen-Gesellschaft mbH +// Software engineering by Intevation GmbH +// +// Author(s): +// * Sascha L. Teichmann <sascha.teichmann@intevation.de> +// * Raimund Renkert <raimund.renkert@intevation.de> + +package controllers + +import ( + "database/sql" + "log" + "net/http" + + "gemma.intevation.de/gemma/pkg/auth" + "gemma.intevation.de/gemma/pkg/common" + "gemma.intevation.de/gemma/pkg/imports" + "gemma.intevation.de/gemma/pkg/models" +) + +func importBottleneck(input interface{}) (interface{}, bool, bool) { + bi := input.(*models.BottleneckImport) + bn := &imports.Bottleneck{ + URL: bi.URL, + Insecure: bi.Insecure, + } + return bn, bi.SendEmail, false +} + +func importGaugeMeasurement(input interface{}) (interface{}, bool, bool) { + gi := input.(*models.GaugeMeasurementImport) + gm := &imports.GaugeMeasurement{ + URL: gi.URL, + Insecure: gi.Insecure, + } + return gm, gi.SendEmail, true +} + +func importFairwayAvailability(input interface{}) (interface{}, bool, bool) { + fai := input.(*models.FairwayAvailabilityImport) + fa := &imports.FairwayAvailability{ + URL: fai.URL, + Insecure: fai.Insecure, + } + return fa, fai.SendEmail, true +} + +func manualImport( + kind imports.JobKind, + setup func(interface{}) (interface{}, bool, bool), +) func(interface{}, *http.Request, *sql.Conn) (JSONResult, error) { + + return func(input interface{}, req *http.Request, _ *sql.Conn) ( + jr JSONResult, err error) { + + what, sendEmail, autoAccept := setup(input) + + var serialized string + if serialized, err = common.ToJSONString(what); err != nil { + return + } + + session, _ := auth.GetSession(req) + + var jobID int64 + if jobID, err = imports.AddJob( + kind, + session.User, + sendEmail, autoAccept, + serialized, + ); err != nil { + return + } + + log.Printf("info: added import #%d to queue\n", jobID) + + result := struct { + ID int64 `json:"id"` + }{ + ID: jobID, + } + + jr = JSONResult{ + Code: http.StatusCreated, + Result: &result, + } + return + } +}
--- a/pkg/controllers/routes.go Sun Dec 23 19:43:58 2018 +0100 +++ b/pkg/controllers/routes.go Sun Dec 23 20:38:52 2018 +0100 @@ -173,17 +173,20 @@ api.Handle("/imports/bottleneck", waterwayAdmin(&JSONHandler{ Input: func() interface{} { return new(models.BottleneckImport) }, - Handle: importBottleneck, + Handle: manualImport(imports.BNJobKind, importBottleneck), + NoConn: true, })).Methods(http.MethodPost) api.Handle("/imports/gaugemeasurement", waterwayAdmin(&JSONHandler{ Input: func() interface{} { return new(models.GaugeMeasurementImport) }, - Handle: importGaugeMeasurement, + Handle: manualImport(imports.GMJobKind, importGaugeMeasurement), + NoConn: true, })).Methods(http.MethodPost) api.Handle("/imports/fairwayavailability", waterwayAdmin(&JSONHandler{ Input: func() interface{} { return new(models.FairwayAvailabilityImport) }, - Handle: importFairwayAvailability, + Handle: manualImport(imports.FAJobKind, importFairwayAvailability), + NoConn: true, })).Methods(http.MethodPost) // Import scheduler configuration