Mercurial > gemma
diff pkg/controllers/printtemplates.go @ 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 | cdd68f555ad7 |
line wrap: on
line diff
--- 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 }