annotate pkg/controllers/printtemplates.go @ 2158:7bd485bf85e8

Print templates: Implemented /api/templates/print POST to create new templates.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 12:50:40 +0100
parents 51c5aead78c0
children 7444b75d5497
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
17 "bytes"
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "database/sql"
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "net/http"
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
20 "time"
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
21
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
22 "github.com/gorilla/mux"
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
23 "github.com/jackc/pgx/pgtype"
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
24
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
25 "gemma.intevation.de/gemma/pkg/models"
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
26 )
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
27
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
28 const maxPrintTemplateSize = 5 * 1024 * 1024
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
29
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
30 const (
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
31 listPrintTemplatesSQL = `
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
32 SELECT
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
33 template_name,
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
34 date_info
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
35 FROM
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
36 users.templates
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
37 ORDER BY date_info DESC`
2153
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
38
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
39 hasPrintTemplateSQL = `
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
40 SELECT true FROM users.templates WHERE name = $1`
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
41
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
42 deletePrintTemplateSQL = `
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
43 DELETE FROM users.templates WHERE name = $1`
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
44
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
45 selectPrintTemplateSQL = `
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
46 SELECT template_data FROM users.templates WHERE name = $1`
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
47
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
48 insertPrintTemplateSQL = `
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
49 INSERT INTO users.templates (template_name, template_data)
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
50 VALUES ($1, $2)`
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 )
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 func listPrintTemplates(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 ) (jr JSONResult, err error) {
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
58
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
59 type template struct {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
60 Name string `json:"name"`
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
61 Time models.Time `json:"time"`
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
62 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
63
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
64 var rows *sql.Rows
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
65 if rows, err = conn.QueryContext(req.Context(), listPrintTemplatesSQL); err != nil {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
66 return
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
67 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
68 defer rows.Close()
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
69
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
70 templates := []*template{}
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
71
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
72 for rows.Next() {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
73 var tmpl template
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
74 var w time.Time
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
75 if err = rows.Scan(&tmpl.Name, &w); err != nil {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
76 return
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
77 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
78 tmpl.Time = models.Time{w}
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
79 templates = append(templates, &tmpl)
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
80 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
81
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
82 jr = JSONResult{Result: templates}
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 func fetchPrintTemplate(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 ) (jr JSONResult, err error) {
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
91
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
92 ctx := req.Context()
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
93 name := mux.Vars(req)["name"]
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
94
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
95 var data pgtype.Bytea
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
96 err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name).Scan(&data)
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
97
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
98 switch {
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
99 case err == sql.ErrNoRows:
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
100 err = JSONError{
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
101 Code: http.StatusNotFound,
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
102 Message: "No such template found",
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
103 }
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
104 return
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
105 case err != nil:
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
106 return
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
107 case data.Status != pgtype.Present:
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
108 err = JSONError{
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
109 Code: http.StatusInternalServerError,
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
110 Message: "Unexpected return value from database query",
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
111 }
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
112 return
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
113 }
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
114 jr = JSONResult{Result: bytes.NewReader(data.Bytes)}
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 }
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 func createPrintTemplate(
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
119 input interface{},
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 ) (jr JSONResult, err error) {
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
123
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
124 ctx := req.Context()
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
125 in := input.(*models.PrintTemplateIn)
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
126
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
127 if in.Name == "" {
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
128 err = JSONError{
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
129 Code: http.StatusBadRequest,
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
130 Message: "Template must have a none empty name",
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
131 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
132 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
133 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
134 if len(in.Template) == 0 {
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
135 err = JSONError{
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
136 Code: http.StatusBadRequest,
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
137 Message: "Template must have a none empty template",
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
138 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
139 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
140 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
141 var tx *sql.Tx
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
142 if tx, err = conn.BeginTx(ctx, nil); err != nil {
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
143 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
144 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
145 defer tx.Rollback()
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
146
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
147 var dummy bool
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
148 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, in.Name).Scan(&dummy)
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
149
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
150 switch {
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
151 case err == sql.ErrNoRows:
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
152 // This is fine.
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
153 case err != nil:
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
154 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
155 default:
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
156 err = JSONError{
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
157 Code: http.StatusBadRequest,
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
158 Message: "A template with this name already exists",
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
159 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
160 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
161 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
162 data := pgtype.Bytea{Bytes: in.Template, Status: pgtype.Present}
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
163
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
164 if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, in.Name, &data); err != nil {
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
165 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
166 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
167
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
168 if err = tx.Commit(); err != nil {
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
169 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
170 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
171 jr = JSONResult{
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
172 Code: http.StatusCreated,
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
173 Result: map[string]string{
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
174 "created": in.Name,
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
175 },
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
176 }
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 }
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 func deletePrintTemplate(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 ) (jr JSONResult, err error) {
2153
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
185
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
186 ctx := req.Context()
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
187 name := mux.Vars(req)["name"]
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
188
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
189 var tx *sql.Tx
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
190 if tx, err = conn.BeginTx(ctx, nil); err != nil {
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
191 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
192 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
193 defer tx.Rollback()
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
194
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
195 var dummy bool
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
196 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy)
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
197
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
198 switch {
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
199 case err == sql.ErrNoRows:
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
200 err = JSONError{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
201 Code: http.StatusNotFound,
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
202 Message: "No such template found",
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
203 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
204 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
205 case err != nil:
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
206 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
207 case !dummy:
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
208 err = JSONError{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
209 Code: http.StatusInternalServerError,
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
210 Message: "Unexpected return value from database query",
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
211 }
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
212 return
2153
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
213 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
214
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
215 if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name); err != nil {
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
216 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
217 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
218
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
219 if err = tx.Commit(); err != nil {
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
220 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
221 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
222
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
223 jr = JSONResult{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
224 Result: map[string]string{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
225 "deleted": name,
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
226 },
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
227 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
228
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
229 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
230 }
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
231
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
232 func updatePrintTemplate(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
235 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
236 ) (jr JSONResult, err error) {
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237 // TODO: Implement me!
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
238 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
239 }