changeset 812:98a7776100fb

Added end point lo list available surveys for a given bottleneck.
author Sascha Wilde <wilde@intevation.de>
date Thu, 27 Sep 2018 17:25:03 +0200
parents 9b982e50e938
children 1a808929c2c5
files pkg/controllers/routes.go pkg/controllers/surveys.go pkg/models/surveys.go
diffstat 3 files changed, 66 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Thu Sep 27 17:08:26 2018 +0200
+++ b/pkg/controllers/routes.go	Thu Sep 27 17:25:03 2018 +0200
@@ -103,6 +103,11 @@
 		NoConn: true,
 	})).Methods(http.MethodGet)
 
+	// Bottleneck selection
+	api.Handle("/surveys/{bottleneck}", any(&JSONHandler{
+		Handle: listSurveys,
+	})).Methods(http.MethodGet)
+
 	// Cross sections
 
 	api.Handle("/cross", any(&JSONHandler{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/surveys.go	Thu Sep 27 17:25:03 2018 +0200
@@ -0,0 +1,53 @@
+package controllers
+
+import (
+	"database/sql"
+	"net/http"
+
+	"gemma.intevation.de/gemma/pkg/models"
+	"github.com/gorilla/mux"
+)
+
+const (
+	listSurveysSQL = `SELECT s.bottleneck_id,
+                                 s.date_info::text
+FROM waterway.bottlenecks b, waterway.sounding_results s
+WHERE b.objnam=$1 AND s.bottleneck_id = b.bottleneck_id;`
+)
+
+func listSurveys(
+	_ interface{},
+	req *http.Request,
+	db *sql.Conn,
+) (jr JSONResult, err error) {
+
+	bottleneckName := mux.Vars(req)["bottleneck"]
+
+	var rows *sql.Rows
+
+	rows, err = db.QueryContext(req.Context(), listSurveysSQL, bottleneckName)
+	if err != nil {
+		return
+	}
+	defer rows.Close()
+
+	surveys := []models.Survey{}
+
+	for rows.Next() {
+		var survey models.Survey
+		if err = rows.Scan(
+			&survey.BottleneckId,
+			&survey.DateInfo,
+		); err != nil {
+			return
+		}
+		surveys = append(surveys, survey)
+	}
+
+	jr = JSONResult{
+		Result: struct {
+			Surveys []models.Survey `json:"surveys"`
+		}{surveys},
+	}
+	return
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/surveys.go	Thu Sep 27 17:25:03 2018 +0200
@@ -0,0 +1,8 @@
+package models
+
+type (
+	Survey struct {
+		BottleneckId string `json:"bottleneck_id"`
+		DateInfo     string `json:"date_info"`
+	}
+)