Mercurial > gemma
annotate pkg/controllers/search.go @ 4931:e41d42be0e13 fairway-marks-import
One more callback to avoid code duplication more consequently
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Fri, 14 Feb 2020 19:34:08 +0100 |
parents | e020e6e34ad7 |
children | 2dcfab23dc86 |
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 "strings" |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
22 "gemma.intevation.de/gemma/pkg/models" |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
23 |
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
24 mw "gemma.intevation.de/gemma/pkg/middleware" |
743
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 |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
27 const ( |
1109
74a75a5ce770
Added geonames data and extended search for villages/cities.
Sascha Wilde <wilde@intevation.de>
parents:
1050
diff
changeset
|
28 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
|
29 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
30 listBottlenecksSQL = ` |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
31 SELECT COALESCE(json_agg(r),'[]') |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
32 FROM ( |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
33 SELECT |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
34 objnam AS name, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
35 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
|
36 'bottleneck' AS type |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
37 FROM waterway.bottlenecks |
3666
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
38 WHERE validity @> current_timestamp |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
39 ORDER BY objnam) r |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
40 ` |
743
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 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
43 func searchFeature(req *http.Request) (jr mw.JSONResult, err error) { |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
44 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
45 s := mw.JSONInput(req).(*models.SearchRequest) |
743
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 if len(s.SearchString) == 0 { |
4506
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
48 err = mw.JSONError{ |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
49 Code: http.StatusBadRequest, |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
50 Message: "error: empty search string", |
e020e6e34ad7
Made 'go vet' happy again.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4244
diff
changeset
|
51 } |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 return |
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 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
55 var result string |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
56 err = mw.JSONConn(req).QueryRowContext( |
3734
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
57 req.Context(), |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
58 searchMostSQL, |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
59 s.SearchString, |
1504856c9378
Moved rhm search to db.
Sascha Wilde <wilde@intevation.de>
parents:
3666
diff
changeset
|
60 ).Scan(&result) |
748
3cb012d4d9ef
Added search for bottlenecks
Sascha Wilde <wilde@intevation.de>
parents:
744
diff
changeset
|
61 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
62 if err != nil { |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
63 return |
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
64 } |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
65 |
751
21dd10f26206
Some refactoring in search endpoint.
Sascha Wilde <wilde@intevation.de>
parents:
750
diff
changeset
|
66 jr.Result = strings.NewReader(result) |
743
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 return |
fdff2de616ad
Added search function for river kilometer.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 } |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
69 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
70 func listBottlenecks(req *http.Request) (jr mw.JSONResult, err error) { |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
71 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
72 var result string |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
73 err = mw.JSONConn(req).QueryRowContext( |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
74 req.Context(), listBottlenecksSQL).Scan(&result) |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
75 |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
76 switch { |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
77 case err == sql.ErrNoRows: |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
78 err = mw.JSONError{ |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
79 Code: http.StatusNotFound, |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
80 Message: "Cannot find any bottleneck.", |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
81 } |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
82 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
83 case err != nil: |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
84 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
85 } |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
86 |
4244
4394daeea96a
Moved JSONHandler into middleware package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4243
diff
changeset
|
87 jr = mw.JSONResult{Result: strings.NewReader(result)} |
1050
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
88 return |
146245d2198f
List all bottlenecks via GET /api/bottlenecks
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
1024
diff
changeset
|
89 } |