changeset 2150:2c67c51d57ad

Print templates: Implemented GET of all templates.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 11:31:57 +0100
parents 4057a1f15174
children d639b721c7be
files pkg/common/attributes.go pkg/common/time.go pkg/controllers/printtemplates.go pkg/models/common.go
diffstat 4 files changed, 80 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/common/attributes.go	Fri Feb 08 11:03:12 2019 +0100
+++ b/pkg/common/attributes.go	Fri Feb 08 11:31:57 2019 +0100
@@ -20,13 +20,7 @@
 	"time"
 )
 
-const (
-	TimeFormat = "2006-01-02T15:04:05"
-	DateFormat = "2006-01-02"
-)
-
 type (
-
 	// Attributes is a map of optional key/value attributes
 	// of a configuration.
 	Attributes map[string]string
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/common/time.go	Fri Feb 08 11:31:57 2019 +0100
@@ -0,0 +1,19 @@
+// 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 L. Teichmann <sascha.teichmann@intevation.de>
+
+package common
+
+const (
+	TimeFormat = "2006-01-02T15:04:05"
+	DateFormat = "2006-01-02"
+)
--- a/pkg/controllers/printtemplates.go	Fri Feb 08 11:03:12 2019 +0100
+++ b/pkg/controllers/printtemplates.go	Fri Feb 08 11:31:57 2019 +0100
@@ -16,6 +16,19 @@
 import (
 	"database/sql"
 	"net/http"
+	"time"
+
+	"gemma.intevation.de/gemma/pkg/models"
+)
+
+const (
+	listPrintTemplatesSQL = `
+SELECT
+  template_name,
+  date_info
+FROM
+  users.templates
+ORDER BY date_info DESC`
 )
 
 func listPrintTemplates(
@@ -23,7 +36,31 @@
 	req *http.Request,
 	conn *sql.Conn,
 ) (jr JSONResult, err error) {
-	// TODO: Implement me!
+
+	type template struct {
+		Name string      `json:"name"`
+		Time models.Time `json:"time"`
+	}
+
+	var rows *sql.Rows
+	if rows, err = conn.QueryContext(req.Context(), listPrintTemplatesSQL); err != nil {
+		return
+	}
+	defer rows.Close()
+
+	templates := []*template{}
+
+	for rows.Next() {
+		var tmpl template
+		var w time.Time
+		if err = rows.Scan(&tmpl.Name, &w); err != nil {
+			return
+		}
+		tmpl.Time = models.Time{w}
+		templates = append(templates, &tmpl)
+	}
+
+	jr = JSONResult{Result: templates}
 	return
 }
 
--- a/pkg/models/common.go	Fri Feb 08 11:03:12 2019 +0100
+++ b/pkg/models/common.go	Fri Feb 08 11:31:57 2019 +0100
@@ -34,24 +34,42 @@
 
 type (
 	Date struct{ time.Time }
+	Time struct{ time.Time }
+
 	// Country is a valid country 2 letter code.
 	Country string
 	// UniqueCountries is a list of unique countries.
 	UniqueCountries []Country
 )
 
-func (srd Date) MarshalJSON() ([]byte, error) {
-	return json.Marshal(srd.Format(common.DateFormat))
+func (d Date) MarshalJSON() ([]byte, error) {
+	return json.Marshal(d.Format(common.DateFormat))
 }
 
-func (srd *Date) UnmarshalJSON(data []byte) error {
+func (d *Date) UnmarshalJSON(data []byte) error {
 	var s string
 	if err := json.Unmarshal(data, &s); err != nil {
 		return err
 	}
-	d, err := time.Parse(common.DateFormat, s)
+	d2, err := time.Parse(common.DateFormat, s)
 	if err == nil {
-		*srd = Date{d}
+		*d = Date{d2}
+	}
+	return err
+}
+
+func (t Time) MarshalJSON() ([]byte, error) {
+	return json.Marshal(t.Format(common.TimeFormat))
+}
+
+func (t *Time) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+	t2, err := time.Parse(common.TimeFormat, s)
+	if err == nil {
+		*t = Time{t2}
 	}
 	return err
 }