annotate pkg/controllers/printtemplates.go @ 2161:7444b75d5497

Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 12:56:33 +0100
parents 7bd485bf85e8
children 0627565fb4d2
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"
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
19 "encoding/json"
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "net/http"
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
21 "time"
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
22
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
23 "github.com/gorilla/mux"
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
24 "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
25
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
26 "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
27 )
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
28
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
29 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
30
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
31 const (
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
32 listPrintTemplatesSQL = `
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
33 SELECT
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
34 template_name,
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
35 date_info
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
36 FROM
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
37 users.templates
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
38 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
39
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
40 hasPrintTemplateSQL = `
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
41 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
42
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
43 deletePrintTemplateSQL = `
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
44 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
45
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
46 selectPrintTemplateSQL = `
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
47 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
48
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
49 insertPrintTemplateSQL = `
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
50 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
51 VALUES ($1, $2)`
2148
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
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 func listPrintTemplates(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 ) (jr JSONResult, err error) {
2150
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
59
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
60 type template struct {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
61 Name string `json:"name"`
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
62 Time models.Time `json:"time"`
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
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
65 var rows *sql.Rows
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
66 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
67 return
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
68 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
69 defer rows.Close()
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
70
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
71 templates := []*template{}
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
72
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
73 for rows.Next() {
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
74 var tmpl template
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
75 var w time.Time
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
76 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
77 return
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
78 }
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
79 tmpl.Time = models.Time{w}
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
80 templates = append(templates, &tmpl)
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
2c67c51d57ad Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2148
diff changeset
83 jr = JSONResult{Result: templates}
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 return
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
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 func fetchPrintTemplate(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 ) (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
92
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
93 ctx := req.Context()
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
94 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
95
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
96 var data pgtype.Bytea
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
97 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
98
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
99 switch {
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
100 case err == sql.ErrNoRows:
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
101 err = JSONError{
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
102 Code: http.StatusNotFound,
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
103 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
104 }
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
105 return
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
106 case err != nil:
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
107 return
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
108 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
109 err = JSONError{
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
110 Code: http.StatusInternalServerError,
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
111 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
112 }
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
113 return
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
114 }
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
115 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
116 return
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
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 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
120 input interface{},
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 ) (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
124
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
125 ctx := req.Context()
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
126 name := mux.Vars(req)["name"]
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
127 in := input.(*json.RawMessage)
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
128
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
129 if name == "" {
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
130 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
131 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
132 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
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 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
135 }
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
136 if len(*in) == 0 {
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
137 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
138 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
139 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
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 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
142 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
143 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
144 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
145 return
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 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
148
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
149 var dummy bool
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
150 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy)
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
151
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
152 switch {
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 == 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
154 // 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
155 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
156 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
157 default:
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
158 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
159 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
160 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
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 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
163 }
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
164 data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present}
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
165
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
166 if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, &data); err != nil {
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
167 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
168 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
169
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
170 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
171 return
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
172 }
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
173 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
174 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
175 Result: map[string]string{
2161
7444b75d5497 Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2158
diff changeset
176 "created": name,
2158
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
177 },
7bd485bf85e8 Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2155
diff changeset
178 }
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 }
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 func deletePrintTemplate(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 ) (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
187
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
188 ctx := req.Context()
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
189 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
190
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
191 var tx *sql.Tx
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
192 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
193 return
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 defer tx.Rollback()
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
196
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
197 var dummy bool
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
198 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
199
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
200 switch {
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
201 case err == sql.ErrNoRows:
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
202 err = JSONError{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
203 Code: http.StatusNotFound,
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
204 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
205 }
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 err != nil:
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
208 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
209 case !dummy:
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
210 err = JSONError{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
211 Code: http.StatusInternalServerError,
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
212 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
213 }
2155
51c5aead78c0 Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2153
diff changeset
214 return
2153
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
215 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
216
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
217 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
218 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
219 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
220
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
221 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
222 return
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
223 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
224
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
225 jr = JSONResult{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
226 Result: map[string]string{
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
227 "deleted": name,
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
228 },
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
229 }
cdd68f555ad7 Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2150
diff changeset
230
2148
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
231 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
232 }
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 func updatePrintTemplate(
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
235 _ interface{},
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
236 req *http.Request,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237 conn *sql.Conn,
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
238 ) (jr JSONResult, err error) {
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
239 // TODO: Implement me!
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
240 return
4057a1f15174 Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
241 }