changeset 840:0f61bfc21041

Added end point to get style (colour) information for feature.
author Sascha Wilde <wilde@intevation.de>
date Fri, 28 Sep 2018 10:56:03 +0200
parents 157a3e42d4af
children 07be3e5f99a9
files pkg/controllers/routes.go pkg/controllers/system.go pkg/models/system.go schema/auth.sql
diffstat 4 files changed, 63 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Fri Sep 28 10:46:01 2018 +0200
+++ b/pkg/controllers/routes.go	Fri Sep 28 10:56:03 2018 +0200
@@ -54,6 +54,11 @@
 		NoConn: true,
 	})).Methods(http.MethodGet)
 
+	// System Settings
+	api.Handle("/system/style/{feature}/{attr}", any(&JSONHandler{
+		Handle: getFeatureStyle,
+	})).Methods(http.MethodGet)
+
 	// Password resets.
 	api.Handle("/users/passwordreset", &JSONHandler{
 		Input:  func() interface{} { return new(models.PWResetUser) },
--- a/pkg/controllers/system.go	Fri Sep 28 10:46:01 2018 +0200
+++ b/pkg/controllers/system.go	Fri Sep 28 10:56:03 2018 +0200
@@ -2,13 +2,23 @@
 
 import (
 	"database/sql"
+	"fmt"
 	"io/ioutil"
 	"net/http"
 	"strings"
 
+	"gemma.intevation.de/gemma/pkg/models"
 	"github.com/gorilla/mux"
 )
 
+const (
+	getFeatureColourSQL = `SELECT r,g,b,a
+FROM systemconf.feature_colours
+WHERE feature_name = $1 AND style_attr = $2`
+)
+
+// System status end points
+
 func showSystemLog(
 	_ interface{}, req *http.Request,
 	_ *sql.Conn,
@@ -51,3 +61,39 @@
 	}
 	return
 }
+
+// Map/Feature style end points
+
+func getFeatureStyle(
+	_ interface{},
+	req *http.Request,
+	db *sql.Conn,
+) (jr JSONResult, err error) {
+
+	feature := mux.Vars(req)["feature"]
+	attr := mux.Vars(req)["attr"]
+
+	c := models.Colour{}
+	err = db.QueryRowContext(
+		req.Context(),
+		getFeatureColourSQL,
+		feature, attr,
+	).Scan(&c.R, &c.G, &c.B, &c.A)
+
+	switch {
+	case err == sql.ErrNoRows:
+		err = JSONError{
+			Code:    http.StatusNotFound,
+			Message: "Requestes style not found.",
+		}
+		return
+	case err != nil:
+		return
+	}
+
+	jr.Result = &struct {
+		Colour models.Colour `json:"colour"`
+		Code   string        `json:"code"`
+	}{c, fmt.Sprintf("rgba(%d, %d, %d, %g)", c.R, c.G, c.B, c.A)}
+	return
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/system.go	Fri Sep 28 10:56:03 2018 +0200
@@ -0,0 +1,10 @@
+package models
+
+type (
+	Colour struct {
+		R int     `json:"r"`
+		G int     `json:"g"`
+		B int     `json:"b"`
+		A float32 `json:"a"`
+	}
+)
--- a/schema/auth.sql	Fri Sep 28 10:46:01 2018 +0200
+++ b/schema/auth.sql	Fri Sep 28 10:56:03 2018 +0200
@@ -10,8 +10,9 @@
 --
 -- Privileges for waterway_user
 --
-GRANT USAGE ON SCHEMA public, users, waterway TO waterway_user;
+GRANT USAGE ON SCHEMA public, users, waterway, systemconf TO waterway_user;
 GRANT SELECT ON ALL TABLES IN SCHEMA public, users, waterway TO waterway_user;
+GRANT SELECT ON systemconf.feature_colours TO waterway_user;
 GRANT UPDATE (pw, map_extent, email_address) ON users.list_users
     TO waterway_user;