Mercurial > gemma
annotate pkg/controllers/search.go @ 3830:464a6a6e05e6 sld-colors
Clean color handling a bit.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 04 Jul 2019 11:03:36 +0200 |
parents | 1504856c9378 |
children | f68529ff47a4 |
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> |
1337
53057ab27ff2
add headers for licensing to some middleware files
Fadi Abbud <fadi.abbud@intevation.de>
parents:
1109
diff
changeset
|
13 // * Sascha Teichmann <sascha.teichmann@intevation.de> |
1017
a244b18cb916
Added GNU Affero General Public License.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
995
diff
changeset
|
14 |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
15 package controllers |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
16 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
17 import ( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
19 "net/http" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 "regexp" |
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 ( |
1109
74a75a5ce770
Added geonames data and extended search for villages/cities.
Sascha Wilde <wilde@intevation.de>
parents:
1050
diff
changeset
|
27 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
|
28 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
29 listBottlenecksSQL = ` |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
30 SELECT COALESCE(json_agg(r),'[]') |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
31 FROM ( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
32 SELECT |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
33 objnam AS name, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
34 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
|
35 'bottleneck' AS type |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
36 FROM waterway.bottlenecks |
3666
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
37 WHERE validity @> current_timestamp |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
38 ORDER BY objnam) r |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
39 ` |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
40 ) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
41 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
42 var rkmRegex = regexp.MustCompile( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
43 "^[[:space:]]*([0-9]+)([,.]([0-9]))?[[:space:]]*$", |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
44 ) |
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 func searchFeature( |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
47 input interface{}, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
48 req *http.Request, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
49 db *sql.Conn, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
50 ) (jr JSONResult, err error) { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 s := input.(*models.SearchRequest) |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 if len(s.SearchString) == 0 { |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 err = JSONError{http.StatusBadRequest, |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 "error: empty search string"} |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 return |
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 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
60 var result string |
3734
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
61 err = db.QueryRowContext( |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
62 req.Context(), |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
63 searchMostSQL, |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
64 s.SearchString, |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
65 ).Scan(&result) |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
66 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
67 if err != nil { |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
68 return |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
69 } |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
70 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
71 jr.Result = strings.NewReader(result) |
743
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 } |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
74 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
75 func listBottlenecks( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
76 _ interface{}, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
77 req *http.Request, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
78 conn *sql.Conn, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
79 ) (jr JSONResult, err error) { |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
80 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
81 var result string |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
82 err = conn.QueryRowContext( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
83 req.Context(), listBottlenecksSQL).Scan(&result) |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
84 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
85 switch { |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
86 case err == sql.ErrNoRows: |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
87 err = JSONError{ |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
88 Code: http.StatusNotFound, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
89 Message: "Cannot find any bottleneck.", |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
90 } |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
91 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
92 case err != nil: |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
93 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
94 } |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
95 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
96 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
|
97 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
98 } |