# HG changeset patch # User Sascha Wilde # Date 1538061903 -7200 # Node ID 98a7776100fb90d0040681552830ab59991403fc # Parent 9b982e50e938a145de97be87f785daac48e1b3bd Added end point lo list available surveys for a given bottleneck. diff -r 9b982e50e938 -r 98a7776100fb pkg/controllers/routes.go --- 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{ diff -r 9b982e50e938 -r 98a7776100fb pkg/controllers/surveys.go --- /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 +} diff -r 9b982e50e938 -r 98a7776100fb pkg/models/surveys.go --- /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"` + } +)