Mercurial > gemma
comparison pkg/models/sr.go @ 1224:bc4b642c8d04
Started with an upload sounding result to temp upload area.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 19 Nov 2018 17:14:42 +0100 |
parents | |
children | 1c0c9190fcf2 |
comparison
equal
deleted
inserted
replaced
1223:a20fcfacda98 | 1224:bc4b642c8d04 |
---|---|
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 // * Bernhard E. Reiter <bernhard.reiter@intevation.de> | |
14 | |
15 package models | |
16 | |
17 import ( | |
18 "context" | |
19 "database/sql" | |
20 "encoding/json" | |
21 "errors" | |
22 "fmt" | |
23 "io" | |
24 "time" | |
25 ) | |
26 | |
27 const ( | |
28 SoundingResultDateFormat = "2006-01-02" | |
29 WGS84 = 4326 | |
30 ) | |
31 | |
32 type ( | |
33 SoundingResultDate struct{ time.Time } | |
34 | |
35 SoundingResultMeta struct { | |
36 Date SoundingResultDate `json:"date"` | |
37 Bottleneck string `json:"bottleneck"` | |
38 EPSG uint `json:"epsg"` | |
39 DepthReference string `json:"depth-reference"` | |
40 } | |
41 ) | |
42 | |
43 const ( | |
44 checkDepthReferenceSQL = ` | |
45 SELECT true FROM depth_references WHERE depth_reference = $1` | |
46 | |
47 checkBottleneckSQL = ` | |
48 SELECT true FROM waterway.bottlenecks WHERE objnam = $1` | |
49 ) | |
50 | |
51 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error { | |
52 var s string | |
53 if err := json.Unmarshal(data, &s); err != nil { | |
54 return err | |
55 } | |
56 d, err := time.Parse(SoundingResultDateFormat, s) | |
57 if err == nil { | |
58 *srd = SoundingResultDate{d} | |
59 } | |
60 return err | |
61 } | |
62 | |
63 func (m *SoundingResultMeta) Decode(r io.Reader) error { | |
64 err := json.NewDecoder(r).Decode(m) | |
65 if err == nil && m.EPSG == 0 { | |
66 m.EPSG = WGS84 | |
67 } | |
68 return err | |
69 } | |
70 | |
71 func (m *SoundingResultMeta) Validate(conn *sql.Conn, ctx context.Context) []error { | |
72 | |
73 var errs []error | |
74 | |
75 var b bool | |
76 err := conn.QueryRowContext(ctx, | |
77 checkDepthReferenceSQL, | |
78 m.DepthReference).Scan(&b) | |
79 switch { | |
80 case err == sql.ErrNoRows: | |
81 errs = append(errs, fmt.Errorf("Unknown depth reference '%s'\n", m.DepthReference)) | |
82 case err != nil: | |
83 errs = append(errs, err) | |
84 case !b: | |
85 errs = append(errs, errors.New("Unexpected depth reference")) | |
86 } | |
87 | |
88 err = conn.QueryRowContext(context.Background(), | |
89 checkBottleneckSQL, | |
90 m.Bottleneck).Scan(&b) | |
91 switch { | |
92 case err == sql.ErrNoRows: | |
93 errs = append(errs, fmt.Errorf("Unknown bottleneck '%s'\n", m.Bottleneck)) | |
94 case err != nil: | |
95 errs = append(errs, err) | |
96 case !b: | |
97 errs = append(errs, errors.New("Unexpected bottleneck")) | |
98 } | |
99 | |
100 return errs | |
101 } |