Mercurial > gemma
comparison pkg/controllers/misc.go @ 5195:d6710d29516b new-fwa
Started to move code around.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 07 May 2020 15:46:51 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5194:6727ede03009 | 5195:d6710d29516b |
---|---|
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) 2019, 2020 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 | |
14 package controllers | |
15 | |
16 import ( | |
17 "fmt" | |
18 "net/http" | |
19 "strconv" | |
20 "time" | |
21 | |
22 "gemma.intevation.de/gemma/pkg/common" | |
23 ) | |
24 | |
25 func parseFormTime( | |
26 rw http.ResponseWriter, | |
27 req *http.Request, | |
28 field string, | |
29 def time.Time, | |
30 ) (time.Time, bool) { | |
31 f := req.FormValue(field) | |
32 if f == "" { | |
33 return def.UTC(), true | |
34 } | |
35 v, err := common.ParseTime(f) | |
36 if err != nil { | |
37 http.Error( | |
38 rw, fmt.Sprintf("Invalid format for '%s'.", field), | |
39 http.StatusBadRequest, | |
40 ) | |
41 return time.Time{}, false | |
42 } | |
43 return v.UTC(), true | |
44 } | |
45 | |
46 func parseFormInt( | |
47 rw http.ResponseWriter, | |
48 req *http.Request, | |
49 field string, | |
50 def int, | |
51 ) (int, bool) { | |
52 f := req.FormValue(field) | |
53 if f == "" { | |
54 return def, true | |
55 } | |
56 v, err := strconv.Atoi(f) | |
57 if err != nil { | |
58 http.Error( | |
59 rw, fmt.Sprintf("Invalid format for '%s'.", field), | |
60 http.StatusBadRequest, | |
61 ) | |
62 return 0, false | |
63 } | |
64 return v, true | |
65 } |