view pkg/controllers/surveys.go @ 2129:8f6345ad5f13

Extend /surveys/ endpoint to include reference gauge * Add a subselection to the sql query in models/surveys.go which similiar to the `bottleneck_geoserver` view assumes that the reference gauge is the same for each survey result. * Maintain rights infos.
author Bernhard Reiter <bernhard@intevation.de>
date Wed, 06 Feb 2019 15:44:55 +0100
parents 056a86b24be2
children 7267f8168176
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, 2019 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha Wilde <sascha.wilde@intevation.de>
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
//  * Bernhard Reiter <bernhard.reiter@intevation.de>

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,
  bg.objname AS gauge_objname
FROM
  ( SELECT * FROM waterway.bottlenecks AS b, waterway.gauges AS g
    WHERE b.fk_g_fid = g.location
  ) AS bg
  JOIN waterway.sounding_results AS s
ON bg.id = s.bottleneck_id
WHERE bg.objnam=$1`
)

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,
			&survey.ReferenceGauge,
		); err != nil {
			return
		}
		surveys = append(surveys, survey)
	}

	if err = rows.Err(); err != nil {
		return
	}

	jr = JSONResult{
		Result: struct {
			Surveys []models.Survey `json:"surveys"`
		}{surveys},
	}
	return
}