view pkg/controllers/search.go @ 4723:baabc2b2f094

Avoid creating user profiles without matching role The INSTEAD OF triggers on users.list_users did that already, but profile data coming e.g. via restoring a dump had been added also if there was no matching database role in the cluster. This also unifies the errors occuring on creation of users with existing role names that differed between roles with and without profile before. Note this is no referential integrity. A dropped role still leaves an orphaned profile behind.
author Tom Gottfried <tom@intevation.de>
date Thu, 17 Oct 2019 18:56:59 +0200
parents e020e6e34ad7
children 2dcfab23dc86
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 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha Wilde <sascha.wilde@intevation.de>
//  * Sascha Teichmann <sascha.teichmann@intevation.de>

package controllers

import (
	"database/sql"
	"net/http"
	"strings"

	"gemma.intevation.de/gemma/pkg/models"

	mw "gemma.intevation.de/gemma/pkg/middleware"
)

const (
	searchMostSQL = `SELECT search_most($1)::text`

	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
  WHERE validity @> current_timestamp
ORDER BY objnam) r
`
)

func searchFeature(req *http.Request) (jr mw.JSONResult, err error) {

	s := mw.JSONInput(req).(*models.SearchRequest)

	if len(s.SearchString) == 0 {
		err = mw.JSONError{
			Code:    http.StatusBadRequest,
			Message: "error: empty search string",
		}
		return
	}

	var result string
	err = mw.JSONConn(req).QueryRowContext(
		req.Context(),
		searchMostSQL,
		s.SearchString,
	).Scan(&result)

	if err != nil {
		return
	}

	jr.Result = strings.NewReader(result)
	return
}

func listBottlenecks(req *http.Request) (jr mw.JSONResult, err error) {

	var result string
	err = mw.JSONConn(req).QueryRowContext(
		req.Context(), listBottlenecksSQL).Scan(&result)

	switch {
	case err == sql.ErrNoRows:
		err = mw.JSONError{
			Code:    http.StatusNotFound,
			Message: "Cannot find any bottleneck.",
		}
		return
	case err != nil:
		return
	}

	jr = mw.JSONResult{Result: strings.NewReader(result)}
	return
}