comparison pkg/controllers/printtemplates.go @ 3217:4c254651d80b

Added template types "map", "diagram", "report".
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 09 May 2019 12:08:02 +0200
parents 813309225e35
children 9e087a495f41
comparison
equal deleted inserted replaced
3216:ee6a4e8af766 3217:4c254651d80b
16 import ( 16 import (
17 "bytes" 17 "bytes"
18 "database/sql" 18 "database/sql"
19 "encoding/json" 19 "encoding/json"
20 "net/http" 20 "net/http"
21 "strings"
21 "time" 22 "time"
22 23
23 "github.com/gorilla/mux" 24 "github.com/gorilla/mux"
24 "github.com/jackc/pgx/pgtype" 25 "github.com/jackc/pgx/pgtype"
25 26
30 31
31 const ( 32 const (
32 listPrintTemplatesSQL = ` 33 listPrintTemplatesSQL = `
33 SELECT 34 SELECT
34 template_name, 35 template_name,
36 template_type::varchar,
35 date_info, 37 date_info,
36 country 38 country
37 FROM 39 FROM users.templates
38 users.templates 40 `
39 ORDER BY date_info DESC`
40 41
41 hasPrintTemplateSQL = ` 42 hasPrintTemplateSQL = `
42 SELECT true FROM users.templates WHERE template_name = $1` 43 SELECT true FROM users.templates
44 WHERE template_name = $1 AND template_type = $2::template_types`
43 45
44 deletePrintTemplateSQL = ` 46 deletePrintTemplateSQL = `
45 DELETE FROM users.templates WHERE template_name = $1` 47 DELETE FROM users.templates
48 WHERE template_name = $1 AND template_type = $2::template_types`
46 49
47 selectPrintTemplateSQL = ` 50 selectPrintTemplateSQL = `
48 SELECT template_data FROM users.templates WHERE template_name = $1` 51 SELECT template_data FROM users.templates
52 WHERE template_name = $1 AND template_type = $2::template_types`
49 53
50 insertPrintTemplateSQL = ` 54 insertPrintTemplateSQL = `
51 INSERT INTO users.templates (template_name, template_data, country) 55 INSERT INTO users.templates (template_name, template_type, template_data, country)
52 SELECT 56 SELECT
53 $1, 57 $1,
54 $2, 58 $2::template_types,
59 $3,
55 CASE WHEN pg_has_role('sys_admin', 'MEMBER') THEN NULL 60 CASE WHEN pg_has_role('sys_admin', 'MEMBER') THEN NULL
56 ELSE users.current_user_country() 61 ELSE users.current_user_country()
57 END` 62 END`
58 63
59 updatePrintTemplateSQL = ` 64 updatePrintTemplateSQL = `
60 UPDATE user.templates template_data = $2 WHERE template_name = $1` 65 UPDATE user.templates template_data = $2
66 WHERE template_name = $1 AND template_type = $2::template_types`
61 ) 67 )
68
69 var templateTypes = []string{"map", "diagram", "report"}
62 70
63 func listPrintTemplates( 71 func listPrintTemplates(
64 _ interface{}, 72 _ interface{},
65 req *http.Request, 73 req *http.Request,
66 conn *sql.Conn, 74 conn *sql.Conn,
67 ) (jr JSONResult, err error) { 75 ) (jr JSONResult, err error) {
68 76
77 ts := mux.Vars(req)["types"]
78
79 if ts == "" {
80 if ts = req.FormValue("types"); ts == "" {
81 ts = strings.Join(templateTypes, ",")
82 }
83 }
84
85 types := toTextArray(ts, templateTypes)
86 filter := buildFilterTerm("template_type = ANY($%d) ", types)
87
88 var stmt strings.Builder
89 var args []interface{}
90
91 stmt.WriteString(listPrintTemplatesSQL)
92 filter.serialize(&stmt, &args)
93 stmt.WriteString(" ORDER BY date_info DESC")
94
95 var rows *sql.Rows
96 if rows, err = conn.QueryContext(req.Context(), stmt.String(), args...); err != nil {
97 return
98 }
99 defer rows.Close()
100
69 type template struct { 101 type template struct {
70 Name string `json:"name"` 102 Name string `json:"name"`
103 Type string `json:"type"`
71 Time models.Time `json:"time"` 104 Time models.Time `json:"time"`
72 Country *string `json:"country,omitempty"` 105 Country *string `json:"country,omitempty"`
73 } 106 }
74
75 var rows *sql.Rows
76 if rows, err = conn.QueryContext(req.Context(), listPrintTemplatesSQL); err != nil {
77 return
78 }
79 defer rows.Close()
80 107
81 templates := []*template{} 108 templates := []*template{}
82 109
83 for rows.Next() { 110 for rows.Next() {
84 var tmpl template 111 var tmpl template
102 _ interface{}, 129 _ interface{},
103 req *http.Request, 130 req *http.Request,
104 conn *sql.Conn, 131 conn *sql.Conn,
105 ) (jr JSONResult, err error) { 132 ) (jr JSONResult, err error) {
106 133
134 vars := mux.Vars(req)
135 name, typ := vars["name"], vars["type"]
136
107 ctx := req.Context() 137 ctx := req.Context()
108 name := mux.Vars(req)["name"]
109
110 var data pgtype.Bytea 138 var data pgtype.Bytea
111 err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name).Scan(&data) 139 err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name, typ).Scan(&data)
112 140
113 switch { 141 switch {
114 case err == sql.ErrNoRows: 142 case err == sql.ErrNoRows:
115 err = JSONError{ 143 err = JSONError{
116 Code: http.StatusNotFound, 144 Code: http.StatusNotFound,
134 input interface{}, 162 input interface{},
135 req *http.Request, 163 req *http.Request,
136 conn *sql.Conn, 164 conn *sql.Conn,
137 ) (jr JSONResult, err error) { 165 ) (jr JSONResult, err error) {
138 166
167 vars := mux.Vars(req)
168 name, typ := vars["name"], vars["type"]
169
170 in := input.(*json.RawMessage)
171
172 if name == "" {
173 err = JSONError{
174 Code: http.StatusBadRequest,
175 Message: "Template must have a none empty name",
176 }
177 return
178 }
179 if len(*in) == 0 {
180 err = JSONError{
181 Code: http.StatusBadRequest,
182 Message: "Template must have a none empty template",
183 }
184 return
185 }
186
139 ctx := req.Context() 187 ctx := req.Context()
140 name := mux.Vars(req)["name"]
141 in := input.(*json.RawMessage)
142
143 if name == "" {
144 err = JSONError{
145 Code: http.StatusBadRequest,
146 Message: "Template must have a none empty name",
147 }
148 return
149 }
150 if len(*in) == 0 {
151 err = JSONError{
152 Code: http.StatusBadRequest,
153 Message: "Template must have a none empty template",
154 }
155 return
156 }
157 var tx *sql.Tx 188 var tx *sql.Tx
158 if tx, err = conn.BeginTx(ctx, nil); err != nil { 189 if tx, err = conn.BeginTx(ctx, nil); err != nil {
159 return 190 return
160 } 191 }
161 defer tx.Rollback() 192 defer tx.Rollback()
162 193
163 var dummy bool 194 var dummy bool
164 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) 195 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy)
165 196
166 switch { 197 switch {
167 case err == sql.ErrNoRows: 198 case err == sql.ErrNoRows:
168 // This is fine. 199 // This is fine.
169 case err != nil: 200 case err != nil:
175 } 206 }
176 return 207 return
177 } 208 }
178 data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} 209 data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present}
179 210
180 if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, &data); err != nil { 211 if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, typ, &data); err != nil {
181 return 212 return
182 } 213 }
183 214
184 if err = tx.Commit(); err != nil { 215 if err = tx.Commit(); err != nil {
185 return 216 return
197 _ interface{}, 228 _ interface{},
198 req *http.Request, 229 req *http.Request,
199 conn *sql.Conn, 230 conn *sql.Conn,
200 ) (jr JSONResult, err error) { 231 ) (jr JSONResult, err error) {
201 232
233 vars := mux.Vars(req)
234 name, typ := vars["name"], vars["type"]
235
202 ctx := req.Context() 236 ctx := req.Context()
203 name := mux.Vars(req)["name"]
204
205 var tx *sql.Tx 237 var tx *sql.Tx
206 if tx, err = conn.BeginTx(ctx, nil); err != nil { 238 if tx, err = conn.BeginTx(ctx, nil); err != nil {
207 return 239 return
208 } 240 }
209 defer tx.Rollback() 241 defer tx.Rollback()
210 242
211 var dummy bool 243 var dummy bool
212 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) 244 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy)
213 245
214 switch { 246 switch {
215 case err == sql.ErrNoRows: 247 case err == sql.ErrNoRows:
216 err = JSONError{ 248 err = JSONError{
217 Code: http.StatusNotFound, 249 Code: http.StatusNotFound,
226 Message: "Unexpected return value from database query", 258 Message: "Unexpected return value from database query",
227 } 259 }
228 return 260 return
229 } 261 }
230 262
231 if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name); err != nil { 263 if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name, typ); err != nil {
232 return 264 return
233 } 265 }
234 266
235 if err = tx.Commit(); err != nil { 267 if err = tx.Commit(); err != nil {
236 return 268 return
249 input interface{}, 281 input interface{},
250 req *http.Request, 282 req *http.Request,
251 conn *sql.Conn, 283 conn *sql.Conn,
252 ) (jr JSONResult, err error) { 284 ) (jr JSONResult, err error) {
253 285
286 vars := mux.Vars(req)
287 name, typ := vars["name"], vars["type"]
288
289 in := input.(*json.RawMessage)
290
291 if name == "" {
292 err = JSONError{
293 Code: http.StatusBadRequest,
294 Message: "Template must have a none empty name",
295 }
296 return
297 }
298 if len(*in) == 0 {
299 err = JSONError{
300 Code: http.StatusBadRequest,
301 Message: "Template must have a none empty template",
302 }
303 return
304 }
305
254 ctx := req.Context() 306 ctx := req.Context()
255 name := mux.Vars(req)["name"]
256 in := input.(*json.RawMessage)
257
258 if name == "" {
259 err = JSONError{
260 Code: http.StatusBadRequest,
261 Message: "Template must have a none empty name",
262 }
263 return
264 }
265 if len(*in) == 0 {
266 err = JSONError{
267 Code: http.StatusBadRequest,
268 Message: "Template must have a none empty template",
269 }
270 return
271 }
272 var tx *sql.Tx 307 var tx *sql.Tx
273 if tx, err = conn.BeginTx(ctx, nil); err != nil { 308 if tx, err = conn.BeginTx(ctx, nil); err != nil {
274 return 309 return
275 } 310 }
276 defer tx.Rollback() 311 defer tx.Rollback()
277 312
278 var dummy bool 313 var dummy bool
279 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) 314 err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy)
280 315
281 switch { 316 switch {
282 case err == sql.ErrNoRows: 317 case err == sql.ErrNoRows:
283 err = JSONError{ 318 err = JSONError{
284 Code: http.StatusNotFound, 319 Code: http.StatusNotFound,
294 } 329 }
295 return 330 return
296 } 331 }
297 data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} 332 data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present}
298 333
299 if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, &data); err != nil { 334 if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, typ, &data); err != nil {
300 return 335 return
301 } 336 }
302 337
303 if err = tx.Commit(); err != nil { 338 if err = tx.Commit(); err != nil {
304 return 339 return