Mercurial > gemma
view pkg/controllers/search.go @ 3681:c9e1848a516a
Handle violation of responsibility areas graceful for BN import.
As many others this is an error, which should only affect the specific
bottleneck failing and not the whole import.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Tue, 18 Jun 2019 12:43:01 +0200 |
parents | db87f34805fb |
children | 1504856c9378 |
line wrap: on
line source
// This is Free Software under GNU Affero General Public License v >= 3.0 // without warranty, see README.md and license for details. // // SPDX-License-Identifier: AGPL-3.0-or-later // License-Filename: LICENSES/AGPL-3.0.txt // // Copyright (C) 2018 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // // Author(s): // * Sascha Wilde <sascha.wilde@intevation.de> // * Sascha Teichmann <sascha.teichmann@intevation.de> package controllers import ( "database/sql" "net/http" "regexp" "strconv" "strings" "gemma.intevation.de/gemma/pkg/models" ) const ( searchHectometreSQL = `SELECT COALESCE(json_agg(r),'[]') FROM (SELECT (location_code).hectometre || ' rhm' AS name, replace(concat(location_code), ',','') AS locationcode, ST_AsGeoJSON(geom)::json AS geom, 'rhm' AS type FROM waterway.distance_marks_virtual WHERE (location_code).hectometre = $1) r` searchMostSQL = `SELECT search_most($1)::text` listBottlenecksSQL = ` SELECT COALESCE(json_agg(r),'[]') FROM ( SELECT objnam AS name, ST_AsGeoJSON(ST_Centroid(area))::json AS geom, 'bottleneck' AS type FROM waterway.bottlenecks WHERE validity @> current_timestamp ORDER BY objnam) r ` ) var rkmRegex = regexp.MustCompile( "^[[:space:]]*([0-9]+)([,.]([0-9]))?[[:space:]]*$", ) func searchFeature( input interface{}, req *http.Request, db *sql.Conn, ) (jr JSONResult, err error) { s := input.(*models.SearchRequest) if len(s.SearchString) == 0 { err = JSONError{http.StatusBadRequest, "error: empty search string"} return } var result string m := rkmRegex.FindStringSubmatch(s.SearchString) if len(m) != 0 { // Handle search for river kilometre: var hectometre int if hectometre, err = strconv.Atoi(m[1]); err != nil { return } hectometre *= 10 if m[3] != "" { var h int if h, err = strconv.Atoi(m[3]); err != nil { return } hectometre += h } err = db.QueryRowContext( req.Context(), searchHectometreSQL, hectometre, ).Scan(&result) } else { // Hande search for bottlencks: err = db.QueryRowContext( req.Context(), searchMostSQL, s.SearchString, ).Scan(&result) } if err != nil { return } jr.Result = strings.NewReader(result) return } func listBottlenecks( _ interface{}, req *http.Request, conn *sql.Conn, ) (jr JSONResult, err error) { var result string err = conn.QueryRowContext( req.Context(), listBottlenecksSQL).Scan(&result) switch { case err == sql.ErrNoRows: err = JSONError{ Code: http.StatusNotFound, Message: "Cannot find any bottleneck.", } return case err != nil: return } jr = JSONResult{Result: strings.NewReader(result)} return }