Mercurial > gemma
annotate pkg/controllers/search.go @ 1199:181b8a947ecd
fixed property of null bug
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Mon, 19 Nov 2018 08:51:44 +0100 |
parents | 74a75a5ce770 |
children | 5443f5c9154c 53057ab27ff2 |
rev | line source |
---|---|
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
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:
995
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:
995
diff
changeset
|
3 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
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:
995
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:
995
diff
changeset
|
6 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
7 // Copyright (C) 2018 by via donau |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
9 // Software engineering by Intevation GmbH |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
10 // |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
11 // Author(s): |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
12 // * Sascha Wilde <sascha.wilde@intevation.de> |
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
13 |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
14 package controllers |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
15 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
16 import ( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
17 "database/sql" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 "net/http" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
19 "regexp" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 "strconv" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 "strings" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
22 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
23 "gemma.intevation.de/gemma/pkg/models" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
25 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
26 const ( |
749
802ecaae8410
Search back end: simply return emty array for empty result sets.
Sascha Wilde <wilde@intevation.de>
parents:
748
diff
changeset
|
27 searchHectometreSQL = `SELECT COALESCE(json_agg(r),'[]') |
784
5616c6bfb186
Search: show more human readable results for rkm.
Sascha Wilde <wilde@intevation.de>
parents:
751
diff
changeset
|
28 FROM (SELECT (location_code).hectometre || ' rhm' AS name, |
1024
96ea805d44b9
back end: Added new field "type" to search results.
Sascha Wilde <wilde@intevation.de>
parents:
1017
diff
changeset
|
29 ST_AsGeoJSON(geom)::json AS geom, 'rhm' AS type |
995
e03fab882b47
Use distance_marks_virtual table for search.
Sascha Wilde <wilde@intevation.de>
parents:
784
diff
changeset
|
30 FROM waterway.distance_marks_virtual |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
31 WHERE (location_code).hectometre = $1) r` |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
32 |
1109
74a75a5ce770
Added geonames data and extended search for villages/cities.
Sascha Wilde <wilde@intevation.de>
parents:
1050
diff
changeset
|
33 searchMostSQL = `SELECT search_most($1)::text` |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
34 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
35 listBottlenecksSQL = ` |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
36 SELECT COALESCE(json_agg(r),'[]') |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
37 FROM ( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
38 SELECT |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
39 objnam AS name, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
40 ST_AsGeoJSON(ST_Centroid(area))::json AS geom, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
41 'bottleneck' AS type |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
42 FROM waterway.bottlenecks |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
43 ORDER BY objnam) r |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
44 ` |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
45 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
46 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
47 var rkmRegex = regexp.MustCompile( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
48 "^[[:space:]]*([0-9]+)([,.]([0-9]))?[[:space:]]*$", |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
49 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
50 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 func searchFeature( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 input interface{}, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 req *http.Request, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 db *sql.Conn, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 ) (jr JSONResult, err error) { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 s := input.(*models.SearchRequest) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 if len(s.SearchString) == 0 { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 err = JSONError{http.StatusBadRequest, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 "error: empty search string"} |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
63 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
65 var result string |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
66 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 m := rkmRegex.FindStringSubmatch(s.SearchString) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 if len(m) != 0 { |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
69 // Handle search for river kilometre: |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
70 var hectometre int |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
71 if hectometre, err = strconv.Atoi(m[1]); err != nil { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
72 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
73 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
74 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
75 hectometre *= 10 |
750
cf37bf6c28c6
Search: Fixed parsing of numbers w/o decimal.
Sascha Wilde <wilde@intevation.de>
parents:
749
diff
changeset
|
76 if m[3] != "" { |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
77 var h int |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
78 if h, err = strconv.Atoi(m[3]); err != nil { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
79 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
80 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
81 hectometre += h |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
82 } |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
83 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
84 err = db.QueryRowContext( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
85 req.Context(), |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
86 searchHectometreSQL, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
87 hectometre, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
88 ).Scan(&result) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
89 } else { |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
90 // Hande search for bottlencks: |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
91 err = db.QueryRowContext( |
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
92 req.Context(), |
1109
74a75a5ce770
Added geonames data and extended search for villages/cities.
Sascha Wilde <wilde@intevation.de>
parents:
1050
diff
changeset
|
93 searchMostSQL, |
74a75a5ce770
Added geonames data and extended search for villages/cities.
Sascha Wilde <wilde@intevation.de>
parents:
1050
diff
changeset
|
94 s.SearchString, |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
95 ).Scan(&result) |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
96 } |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
97 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
98 if err != nil { |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
99 return |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
100 } |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
101 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
102 jr.Result = strings.NewReader(result) |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
103 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
104 } |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
105 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
106 func listBottlenecks( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
107 _ interface{}, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
108 req *http.Request, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
109 conn *sql.Conn, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
110 ) (jr JSONResult, err error) { |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
111 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
112 var result string |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
113 err = conn.QueryRowContext( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
114 req.Context(), listBottlenecksSQL).Scan(&result) |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
115 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
116 switch { |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
117 case err == sql.ErrNoRows: |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
118 err = JSONError{ |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
119 Code: http.StatusNotFound, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
120 Message: "Cannot find any bottleneck.", |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
121 } |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
122 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
123 case err != nil: |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
124 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
125 } |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
126 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
127 jr = JSONResult{Result: strings.NewReader(result)} |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
128 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
129 } |