Mercurial > gemma
view pkg/controllers/search.go @ 748:3cb012d4d9ef
Added search for bottlenecks
As we only implement search for rkm an and bottlenecks for now, this
is the default if the search string is not a (potential) rkm number.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Mon, 24 Sep 2018 16:19:31 +0200 |
parents | 6fe6839f5ce6 |
children | 802ecaae8410 |
line wrap: on
line source
package controllers import ( "database/sql" "fmt" "net/http" "regexp" "strconv" "strings" "gemma.intevation.de/gemma/pkg/models" ) const ( searchHectometreSQL = `SELECT json_agg(r) FROM (SELECT location_code::text AS name, ST_AsGeoJSON(geom)::json AS geom FROM waterway.distance_marks WHERE (location_code).hectometre = $1) r` searchBottleneckSQL = `SELECT json_agg(r) FROM (SELECT objnam AS name, ST_AsGeoJSON(ST_Centroid(area))::json AS geom FROM waterway.bottlenecks WHERE objnam ILIKE $1) 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 } // Handle search for river kilometre: m := rkmRegex.FindStringSubmatch(s.SearchString) if len(m) != 0 { var hectometre int if hectometre, err = strconv.Atoi(m[1]); err != nil { return } hectometre *= 10 if len(m) == 4 { var h int if h, err = strconv.Atoi(m[3]); err != nil { return } hectometre += h } var result string err = db.QueryRowContext( req.Context(), searchHectometreSQL, hectometre, ).Scan(&result) switch { case err == sql.ErrNoRows: err = JSONError{ Code: http.StatusNotFound, Message: fmt.Sprintf("Cannot find river hectometre %d.", hectometre), } return case err != nil: return } jr.Result = strings.NewReader(result) } else { var result string err = db.QueryRowContext( req.Context(), searchBottleneckSQL, "%"+s.SearchString+"%", ).Scan(&result) switch { case err == sql.ErrNoRows: err = JSONError{ Code: http.StatusNotFound, Message: fmt.Sprintf("No Results for %s.", s.SearchString), } return case err != nil: return } jr.Result = strings.NewReader(result) } return }