annotate pkg/controllers/surveys.go @ 2152:8132434f4093

Surveys controller: Fixed nullable water level.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 11:47:31 +0100
parents ed0ee54fc49e
children 8a8680e70d2e
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: 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 (
2149
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
27 // Use the same logic as in schema/gemma.sql to find out
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
28 // which is the reference gauge and which from its values is meant.
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
29 // The LEFT for the JOIN is necessary because we still want to list
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
30 // the surveys even if the corresponding value cannot be found because the
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
31 // depth_reference and reference_water_level strings do not match.
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
32 //
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
33 // As this information could be vary from survey entry to survey,
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
34 // in theory even for the same bottleneck
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
35 // it seems to be the right place to return it from the /surveys endpoint.
1572
056a86b24be2 Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1332
diff changeset
36 listSurveysSQL = `
056a86b24be2 Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1332
diff changeset
37 SELECT
056a86b24be2 Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1332
diff changeset
38 s.bottleneck_id,
2129
8f6345ad5f13 Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents: 1572
diff changeset
39 s.date_info::text,
2146
7267f8168176 Extending endpoint /surveys to return depth_reference
Bernhard Reiter <bernhard@intevation.de>
parents: 2129
diff changeset
40 s.depth_reference,
2149
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
41 bg.objname AS gauge_objname,
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
42 r.value AS waterlevel_value
2129
8f6345ad5f13 Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents: 1572
diff changeset
43 FROM
2149
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
44 (
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
45 ( 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
46 WHERE b.fk_g_fid = g.location
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
47 ) AS bg
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
48 JOIN waterway.sounding_results AS s
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
49 ON bg.id = s.bottleneck_id
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
50 )
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
51 LEFT JOIN waterway.gauges_reference_water_levels AS r
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
52 ON s.depth_reference = r.reference_water_level AND bg.location = r.gauge_id
2129
8f6345ad5f13 Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents: 1572
diff changeset
53 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
54 )
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
55
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
56 func listSurveys(
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
57 _ interface{},
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
58 req *http.Request,
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
59 db *sql.Conn,
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
60 ) (jr JSONResult, err error) {
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
61
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
62 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
63
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
64 var rows *sql.Rows
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
65
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
66 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
67 if err != nil {
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
68 return
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
69 }
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
70 defer rows.Close()
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
71
2152
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
72 surveys := []*models.Survey{}
812
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
73
2149
ed0ee54fc49e Extend /surveys endpoint to include waterlevel_value
Bernhard Reiter <bernhard@intevation.de>
parents: 2146
diff changeset
74 // 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
75 // 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
76 // (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
77 for rows.Next() {
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
78 var survey models.Survey
2152
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
79 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
80 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
81 &survey.BottleneckID,
812
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
82 &survey.DateInfo,
2146
7267f8168176 Extending endpoint /surveys to return depth_reference
Bernhard Reiter <bernhard@intevation.de>
parents: 2129
diff changeset
83 &survey.DepthReference,
2129
8f6345ad5f13 Extend /surveys/ endpoint to include reference gauge
Bernhard Reiter <bernhard@intevation.de>
parents: 1572
diff changeset
84 &survey.ReferenceGauge,
2152
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
85 &level,
812
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
86 ); err != nil {
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
87 return
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
88 }
2152
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
89 if level.Valid {
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
90 survey.WaterLevelValue = &level.Int64
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
91 }
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
92 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
93 }
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
94
825
a5452a001b46 Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 812
diff changeset
95 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
96 return
a5452a001b46 Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 812
diff changeset
97 }
a5452a001b46 Added missing error check in surveys controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 812
diff changeset
98
812
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
99 jr = JSONResult{
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
100 Result: struct {
2152
8132434f4093 Surveys controller: Fixed nullable water level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2149
diff changeset
101 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
102 }{surveys},
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
103 }
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
104 return
98a7776100fb Added end point lo list available surveys for a given bottleneck.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
105 }