changeset 1050:146245d2198f

List all bottlenecks via GET /api/bottlenecks
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 25 Oct 2018 11:30:14 +0200
parents 2cbc905ff0e9
children 137a8144e581
files pkg/controllers/routes.go pkg/controllers/search.go
diffstat 2 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Thu Oct 25 10:58:37 2018 +0200
+++ b/pkg/controllers/routes.go	Thu Oct 25 11:30:14 2018 +0200
@@ -133,11 +133,16 @@
 		NoConn: true,
 	})).Methods(http.MethodGet)
 
-	// Bottleneck selection
+	// Survey selection
 	api.Handle("/surveys/{bottleneck}", any(&JSONHandler{
 		Handle: listSurveys,
 	})).Methods(http.MethodGet)
 
+	// Bottlenecks
+	api.Handle("/bottlencks", any(&JSONHandler{
+		Handle: listBottlenecks,
+	})).Methods(http.MethodGet)
+
 	// Cross sections
 	api.Handle("/cross", any(&JSONHandler{
 		Input:  func() interface{} { return new(models.CrossSectionInput) },
--- a/pkg/controllers/search.go	Thu Oct 25 10:58:37 2018 +0200
+++ b/pkg/controllers/search.go	Thu Oct 25 11:30:14 2018 +0200
@@ -29,12 +29,24 @@
              ST_AsGeoJSON(geom)::json AS geom, 'rhm' AS type
       FROM waterway.distance_marks_virtual
       WHERE (location_code).hectometre = $1) r`
+
 	searchBottleneckSQL = `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 objnam ILIKE $1) r`
+
+	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
+ORDER BY objnam) r
+`
 )
 
 var rkmRegex = regexp.MustCompile(
@@ -93,6 +105,30 @@
 	}
 
 	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
+}