Mercurial > gemma
comparison 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 |
comparison
equal
deleted
inserted
replaced
1666:56b29406a163 | 1667:aaa05d3c4aac |
---|---|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 | |
2 // without warranty, see README.md and license for details. | |
3 // | |
4 // SPDX-License-Identifier: AGPL-3.0-or-later | |
5 // License-Filename: LICENSES/AGPL-3.0.txt | |
6 // | |
7 // Copyright (C) 2018 by via donau | |
8 // – Österreichische Wasserstraßen-Gesellschaft mbH | |
9 // Software engineering by Intevation GmbH | |
10 // | |
11 // Author(s): | |
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> | |
13 // * Raimund Renkert <raimund.renkert@intevation.de> | |
14 | |
15 package controllers | |
16 | |
17 import ( | |
18 "database/sql" | |
19 "log" | |
20 "net/http" | |
21 | |
22 "gemma.intevation.de/gemma/pkg/auth" | |
23 "gemma.intevation.de/gemma/pkg/common" | |
24 "gemma.intevation.de/gemma/pkg/imports" | |
25 "gemma.intevation.de/gemma/pkg/models" | |
26 ) | |
27 | |
28 func importBottleneck(input interface{}) (interface{}, bool, bool) { | |
29 bi := input.(*models.BottleneckImport) | |
30 bn := &imports.Bottleneck{ | |
31 URL: bi.URL, | |
32 Insecure: bi.Insecure, | |
33 } | |
34 return bn, bi.SendEmail, false | |
35 } | |
36 | |
37 func importGaugeMeasurement(input interface{}) (interface{}, bool, bool) { | |
38 gi := input.(*models.GaugeMeasurementImport) | |
39 gm := &imports.GaugeMeasurement{ | |
40 URL: gi.URL, | |
41 Insecure: gi.Insecure, | |
42 } | |
43 return gm, gi.SendEmail, true | |
44 } | |
45 | |
46 func importFairwayAvailability(input interface{}) (interface{}, bool, bool) { | |
47 fai := input.(*models.FairwayAvailabilityImport) | |
48 fa := &imports.FairwayAvailability{ | |
49 URL: fai.URL, | |
50 Insecure: fai.Insecure, | |
51 } | |
52 return fa, fai.SendEmail, true | |
53 } | |
54 | |
55 func manualImport( | |
56 kind imports.JobKind, | |
57 setup func(interface{}) (interface{}, bool, bool), | |
58 ) func(interface{}, *http.Request, *sql.Conn) (JSONResult, error) { | |
59 | |
60 return func(input interface{}, req *http.Request, _ *sql.Conn) ( | |
61 jr JSONResult, err error) { | |
62 | |
63 what, sendEmail, autoAccept := setup(input) | |
64 | |
65 var serialized string | |
66 if serialized, err = common.ToJSONString(what); err != nil { | |
67 return | |
68 } | |
69 | |
70 session, _ := auth.GetSession(req) | |
71 | |
72 var jobID int64 | |
73 if jobID, err = imports.AddJob( | |
74 kind, | |
75 session.User, | |
76 sendEmail, autoAccept, | |
77 serialized, | |
78 ); err != nil { | |
79 return | |
80 } | |
81 | |
82 log.Printf("info: added import #%d to queue\n", jobID) | |
83 | |
84 result := struct { | |
85 ID int64 `json:"id"` | |
86 }{ | |
87 ID: jobID, | |
88 } | |
89 | |
90 jr = JSONResult{ | |
91 Code: http.StatusCreated, | |
92 Result: &result, | |
93 } | |
94 return | |
95 } | |
96 } |