Mercurial > gemma
annotate pkg/controllers/surveys.go @ 3019:4b9e83cf82ea
client: removed unnecessary css class
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Thu, 11 Apr 2019 17:47:04 +0200 |
parents | 8a8680e70d2e |
children | ec6163c6687d |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
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:
825
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:
825
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
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:
825
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:
825
diff
changeset
|
6 // |
2129
8f6345ad5f13
Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents:
1572
diff
changeset
|
7 // Copyright (C) 2018, 2019 by via donau |
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
diff
changeset
|
12 // * Sascha Wilde <sascha.wilde@intevation.de> |
1317
5443f5c9154c
Added missing authors names in Go files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1017
diff
changeset
|
13 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
2129
8f6345ad5f13
Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents:
1572
diff
changeset
|
14 // * Bernhard Reiter <bernhard.reiter@intevation.de> |
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
825
diff
changeset
|
15 |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
16 package controllers |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
17 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 import ( |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
19 "database/sql" |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 "net/http" |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
22 "gemma.intevation.de/gemma/pkg/models" |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
23 "github.com/gorilla/mux" |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 ) |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
25 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
26 const ( |
1572
056a86b24be2
Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1332
diff
changeset
|
27 listSurveysSQL = ` |
056a86b24be2
Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1332
diff
changeset
|
28 SELECT |
056a86b24be2
Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1332
diff
changeset
|
29 s.bottleneck_id, |
2129
8f6345ad5f13
Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents:
1572
diff
changeset
|
30 s.date_info::text, |
2146
7267f8168176
Extending endpoint /surveys to return depth_reference
Bernhard Reiter <bernhard@intevation.de>
parents:
2129
diff
changeset
|
31 s.depth_reference, |
2149
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
32 bg.objname AS gauge_objname, |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
33 r.value AS waterlevel_value |
2129
8f6345ad5f13
Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents:
1572
diff
changeset
|
34 FROM |
2149
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
35 ( |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
36 ( SELECT * FROM waterway.bottlenecks AS b, waterway.gauges AS g |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
37 WHERE b.fk_g_fid = g.location |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
38 ) AS bg |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
39 JOIN waterway.sounding_results AS s |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
40 ON bg.id = s.bottleneck_id |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
41 ) |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
42 LEFT JOIN waterway.gauges_reference_water_levels AS r |
2317
8a8680e70d2e
Cleanup schema for reference water levels
Tom Gottfried <tom@intevation.de>
parents:
2152
diff
changeset
|
43 ON s.depth_reference = r.depth_reference AND bg.location = r.gauge_id |
2129
8f6345ad5f13
Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents:
1572
diff
changeset
|
44 WHERE bg.objnam=$1` |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
45 ) |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
46 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
47 func listSurveys( |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
48 _ interface{}, |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
49 req *http.Request, |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
50 db *sql.Conn, |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 ) (jr JSONResult, err error) { |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 bottleneckName := mux.Vars(req)["bottleneck"] |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 var rows *sql.Rows |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 rows, err = db.QueryContext(req.Context(), listSurveysSQL, bottleneckName) |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 if err != nil { |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 return |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 } |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 defer rows.Close() |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 |
2152
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
63 surveys := []*models.Survey{} |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 |
2149
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
65 // as we do not use the values here, we could simply the code here |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
66 // to work without an explicit mdels/surverys.go |
ed0ee54fc49e
Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents:
2146
diff
changeset
|
67 // (like done in controllers/search.go) |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 for rows.Next() { |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
69 var survey models.Survey |
2152
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
70 var level sql.NullInt64 |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
71 if err = rows.Scan( |
1332
447db2bce052
Renamed BottleneckId to BottleneckID to make golint happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1317
diff
changeset
|
72 &survey.BottleneckID, |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
73 &survey.DateInfo, |
2146
7267f8168176
Extending endpoint /surveys to return depth_reference
Bernhard Reiter <bernhard@intevation.de>
parents:
2129
diff
changeset
|
74 &survey.DepthReference, |
2129
8f6345ad5f13
Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents:
1572
diff
changeset
|
75 &survey.ReferenceGauge, |
2152
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
76 &level, |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
77 ); err != nil { |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
78 return |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
79 } |
2152
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
80 if level.Valid { |
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
81 survey.WaterLevelValue = &level.Int64 |
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
82 } |
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
83 surveys = append(surveys, &survey) |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
84 } |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
85 |
825
a5452a001b46
Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
812
diff
changeset
|
86 if err = rows.Err(); err != nil { |
a5452a001b46
Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
812
diff
changeset
|
87 return |
a5452a001b46
Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
812
diff
changeset
|
88 } |
a5452a001b46
Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
812
diff
changeset
|
89 |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
90 jr = JSONResult{ |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
91 Result: struct { |
2152
8132434f4093
Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2149
diff
changeset
|
92 Surveys []*models.Survey `json:"surveys"` |
812
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
93 }{surveys}, |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
94 } |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
95 return |
98a7776100fb
Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
96 } |