Mercurial > gemma
diff pkg/controllers/manualimports.go @ 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 | pkg/controllers/bnimports.go@a0982c38eac0 |
children | 53304db85888 |
line wrap: on
line diff
--- /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 + } +}