annotate pkg/models/common.go @ 2150:2c67c51d57ad

Print templates: Implemented GET of all templates.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 11:31:57 +0100
parents ae0021feaac8
children 97bba8b51b9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1017
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
2 // without warranty, see README.md and license for details.
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
3 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
6 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
7 // Copyright (C) 2018 by via donau
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
9 // Software engineering by Intevation GmbH
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
10 //
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
11 // Author(s):
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
a244b18cb916 Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
13
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package models
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
16 import (
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
17 "database/sql/driver"
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
18 "encoding/json"
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
19 "errors"
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
20 "fmt"
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
21 "strings"
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
22 "time"
2059
ae0021feaac8 Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1979
diff changeset
23
ae0021feaac8 Imports: Made stretch import marshable (even if it is not needed).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1979
diff changeset
24 "gemma.intevation.de/gemma/pkg/common"
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
25 )
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
645
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
27 var (
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
28 errNoString = errors.New("Not a string")
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
29 errNoByteSlice = errors.New("Not a byte slice")
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
30 )
1373
84e78d2e2d95 Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
31
84e78d2e2d95 Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
32 // WGS84 is the EPSG of the World Geodetic System 1984.
84e78d2e2d95 Backend: Centralized the definition of WGS84 constant in the models package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1017
diff changeset
33 const WGS84 = 4326
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
34
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
35 type (
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
36 Date struct{ time.Time }
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
37 Time struct{ time.Time }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
38
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
39 // Country is a valid country 2 letter code.
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
40 Country string
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
41 // UniqueCountries is a list of unique countries.
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
42 UniqueCountries []Country
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
43 )
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
44
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
45 func (d Date) MarshalJSON() ([]byte, error) {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
46 return json.Marshal(d.Format(common.DateFormat))
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
47 }
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
48
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
49 func (d *Date) UnmarshalJSON(data []byte) error {
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
50 var s string
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
51 if err := json.Unmarshal(data, &s); err != nil {
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
52 return err
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
53 }
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
54 d2, err := time.Parse(common.DateFormat, s)
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
55 if err == nil {
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
56 *d = Date{d2}
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
57 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
58 return err
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
59 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
60
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
61 func (t Time) MarshalJSON() ([]byte, error) {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
62 return json.Marshal(t.Format(common.TimeFormat))
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
63 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
64
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
65 func (t *Time) UnmarshalJSON(data []byte) error {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
66 var s string
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
67 if err := json.Unmarshal(data, &s); err != nil {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
68 return err
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
69 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
70 t2, err := time.Parse(common.TimeFormat, s)
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
71 if err == nil {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2059
diff changeset
72 *t = Time{t2}
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
73 }
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
74 return err
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
75 }
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
76
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
77 var (
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
78 validCountries = []string{
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
79 "AT", "BG", "DE", "HU", "HR",
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
80 "MD", "RO", "RS", "SK", "UA",
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
81 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
82 )
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
83
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
84 // UnmarshalJSON ensures that the given string forms a valid
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
85 // two letter country code.
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
86 func (c *Country) UnmarshalJSON(data []byte) error {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
87 var s string
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
88 if err := json.Unmarshal(data, &s); err != nil {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
89 return err
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
90 }
1979
0bc0312105e4 Improved the error message a bit if an invalid country name is passed via JSON api.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
91 u := strings.ToUpper(s)
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
92 for _, v := range validCountries {
1979
0bc0312105e4 Improved the error message a bit if an invalid country name is passed via JSON api.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
93 if v == u {
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
94 *c = Country(v)
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
95 return nil
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
96 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
97 }
1979
0bc0312105e4 Improved the error message a bit if an invalid country name is passed via JSON api.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1901
diff changeset
98 return fmt.Errorf("'%s' is not a valid country", s)
1901
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
99 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
100
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
101 // Value implements the driver.Valuer interface.
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
102 func (c Country) Value() (driver.Value, error) {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
103 return string(c), nil
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
104 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
105
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
106 // Scan implements the sql.Scanner interfaces.
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
107 func (c *Country) Scan(src interface{}) (err error) {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
108 if s, ok := src.(string); ok {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
109 *c = Country(s)
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
110 } else {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
111 err = errNoString
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
112 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
113 return
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
114 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
115
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
116 func (uc *UniqueCountries) UnmarshalJSON(data []byte) error {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
117 var countries []Country
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
118 if err := json.Unmarshal(data, &countries); err != nil {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
119 return err
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
120 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
121 unique := map[Country]struct{}{}
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
122 for _, c := range countries {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
123 if _, found := unique[c]; found {
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
124 return fmt.Errorf("country '%s' is not unique", string(c))
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
125 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
126 unique[c] = struct{}{}
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
127 }
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
128 *uc = countries
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
129 return nil
71b722809b2b Stretch import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
130 }