Mercurial > gemma
annotate pkg/controllers/printtemplates.go @ 2188:005da2c21b11
client: Zoom to users responisbility area on startup.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Tue, 12 Feb 2019 09:23:53 +0100 |
parents | 0627565fb4d2 |
children | e6fba449aa3c |
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)` |
2162
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
52 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
53 updatePrintTemplateSQL = ` |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
54 UPDATE user.templates template_data = $2 WHERE template_name = $1` |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 ) |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 func listPrintTemplates( |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 _ interface{}, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 req *http.Request, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 conn *sql.Conn, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 ) (jr JSONResult, err error) { |
2150
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 type template struct { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
64 Name string `json:"name"` |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
65 Time models.Time `json:"time"` |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
66 } |
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 var rows *sql.Rows |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
69 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
|
70 return |
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 defer rows.Close() |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
73 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
74 templates := []*template{} |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
75 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
76 for rows.Next() { |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
77 var tmpl template |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
78 var w time.Time |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
79 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
|
80 return |
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 tmpl.Time = models.Time{w} |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
83 templates = append(templates, &tmpl) |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
84 } |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
85 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2148
diff
changeset
|
86 jr = JSONResult{Result: templates} |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 return |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 } |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 func fetchPrintTemplate( |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 _ interface{}, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 req *http.Request, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 conn *sql.Conn, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 ) (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
|
95 |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
96 ctx := req.Context() |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
97 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
|
98 |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
99 var data pgtype.Bytea |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
100 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
|
101 |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
102 switch { |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
103 case err == sql.ErrNoRows: |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
104 err = JSONError{ |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
105 Code: http.StatusNotFound, |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
106 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
|
107 } |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
108 return |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
109 case err != nil: |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
110 return |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
111 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
|
112 err = JSONError{ |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
113 Code: http.StatusInternalServerError, |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
114 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
|
115 } |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
116 return |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
117 } |
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
118 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
|
119 return |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 } |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 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
|
123 input interface{}, |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 req *http.Request, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 conn *sql.Conn, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 ) (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
|
127 |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 |
2161
7444b75d5497
Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2158
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 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
|
136 } |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
137 return |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
138 } |
2161
7444b75d5497
Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2158
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 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
|
143 } |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
144 return |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
145 } |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
146 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
|
147 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
|
148 return |
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 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
|
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 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
|
153 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
|
154 |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
155 switch { |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
156 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
|
157 // 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
|
158 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
|
159 return |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
160 default: |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
161 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
|
162 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
|
163 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
|
164 } |
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 } |
2161
7444b75d5497
Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2158
diff
changeset
|
167 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
|
168 |
2161
7444b75d5497
Print templates endpoints: Get rid of useless models.PrintTemplateIn model.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2158
diff
changeset
|
169 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
|
170 return |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
171 } |
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 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
|
174 return |
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 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
|
177 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
|
178 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
|
179 "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
|
180 }, |
7bd485bf85e8
Print templates: Implemented /api/templates/print POST to create new templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2155
diff
changeset
|
181 } |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
182 return |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
183 } |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
184 |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
185 func deletePrintTemplate( |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
186 _ interface{}, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
187 req *http.Request, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
188 conn *sql.Conn, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
189 ) (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
|
190 |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
191 ctx := req.Context() |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
192 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
|
193 |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
194 var tx *sql.Tx |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
195 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
|
196 return |
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 defer tx.Rollback() |
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 var dummy bool |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
201 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
|
202 |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
203 switch { |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
204 case err == sql.ErrNoRows: |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
205 err = JSONError{ |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
206 Code: http.StatusNotFound, |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
207 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
|
208 } |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
209 return |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
210 case err != nil: |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
211 return |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
212 case !dummy: |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
213 err = JSONError{ |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
214 Code: http.StatusInternalServerError, |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
215 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
|
216 } |
2155
51c5aead78c0
Print templates: Implemented /api/templates/print/{name} GET.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2153
diff
changeset
|
217 return |
2153
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 |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
220 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
|
221 return |
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 |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
224 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
|
225 return |
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 jr = JSONResult{ |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
229 Result: map[string]string{ |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
230 "deleted": name, |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
231 }, |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
232 } |
cdd68f555ad7
Print templates: Implemented /api/templates/print/{name} DELETE.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2150
diff
changeset
|
233 |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
234 return |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
235 } |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
236 |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
237 func updatePrintTemplate( |
2162
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
238 input interface{}, |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 req *http.Request, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
240 conn *sql.Conn, |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
241 ) (jr JSONResult, err error) { |
2162
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
242 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
243 ctx := req.Context() |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
244 name := mux.Vars(req)["name"] |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
245 in := input.(*json.RawMessage) |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
246 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
247 if name == "" { |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
248 err = JSONError{ |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
249 Code: http.StatusBadRequest, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
250 Message: "Template must have a none empty name", |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
251 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
252 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
253 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
254 if len(*in) == 0 { |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
255 err = JSONError{ |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
256 Code: http.StatusBadRequest, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
257 Message: "Template must have a none empty template", |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
258 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
259 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
260 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
261 var tx *sql.Tx |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
262 if tx, err = conn.BeginTx(ctx, nil); err != nil { |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
263 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
264 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
265 defer tx.Rollback() |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
266 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
267 var dummy bool |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
268 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
269 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
270 switch { |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
271 case err == sql.ErrNoRows: |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
272 err = JSONError{ |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
273 Code: http.StatusNotFound, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
274 Message: "No such template found", |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
275 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
276 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
277 case err != nil: |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
278 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
279 case !dummy: |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
280 err = JSONError{ |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
281 Code: http.StatusInternalServerError, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
282 Message: "Unexpected return value from database query", |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
283 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
284 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
285 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
286 data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
287 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
288 if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, &data); err != nil { |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
289 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
290 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
291 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
292 if err = tx.Commit(); err != nil { |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
293 return |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
294 } |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
295 |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
296 jr = JSONResult{ |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
297 Code: http.StatusOK, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
298 Result: map[string]string{ |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
299 "updated": name, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
300 }, |
0627565fb4d2
Print templates: Implemented /api/templates/print/{name} PATCH to update print templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2161
diff
changeset
|
301 } |
2148
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
302 return |
4057a1f15174
Print templates: Forget to add controller file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
303 } |