Mercurial > gemma
diff 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 |
line wrap: on
line diff
--- a/pkg/controllers/printtemplates.go Thu May 09 11:56:11 2019 +0200 +++ b/pkg/controllers/printtemplates.go Thu May 09 12:08:02 2019 +0200 @@ -18,6 +18,7 @@ "database/sql" "encoding/json" "net/http" + "strings" "time" "github.com/gorilla/mux" @@ -32,52 +33,78 @@ listPrintTemplatesSQL = ` SELECT template_name, + template_type::varchar, date_info, country -FROM - users.templates -ORDER BY date_info DESC` +FROM users.templates +` hasPrintTemplateSQL = ` -SELECT true FROM users.templates WHERE template_name = $1` +SELECT true FROM users.templates +WHERE template_name = $1 AND template_type = $2::template_types` deletePrintTemplateSQL = ` -DELETE FROM users.templates WHERE template_name = $1` +DELETE FROM users.templates +WHERE template_name = $1 AND template_type = $2::template_types` selectPrintTemplateSQL = ` -SELECT template_data FROM users.templates WHERE template_name = $1` +SELECT template_data FROM users.templates +WHERE template_name = $1 AND template_type = $2::template_types` insertPrintTemplateSQL = ` -INSERT INTO users.templates (template_name, template_data, country) +INSERT INTO users.templates (template_name, template_type, template_data, country) SELECT $1, - $2, + $2::template_types, + $3, CASE WHEN pg_has_role('sys_admin', 'MEMBER') THEN NULL ELSE users.current_user_country() END` updatePrintTemplateSQL = ` -UPDATE user.templates template_data = $2 WHERE template_name = $1` +UPDATE user.templates template_data = $2 +WHERE template_name = $1 AND template_type = $2::template_types` ) +var templateTypes = []string{"map", "diagram", "report"} + func listPrintTemplates( _ interface{}, req *http.Request, conn *sql.Conn, ) (jr JSONResult, err error) { + ts := mux.Vars(req)["types"] + + if ts == "" { + if ts = req.FormValue("types"); ts == "" { + ts = strings.Join(templateTypes, ",") + } + } + + types := toTextArray(ts, templateTypes) + filter := buildFilterTerm("template_type = ANY($%d) ", types) + + var stmt strings.Builder + var args []interface{} + + stmt.WriteString(listPrintTemplatesSQL) + filter.serialize(&stmt, &args) + stmt.WriteString(" ORDER BY date_info DESC") + + var rows *sql.Rows + if rows, err = conn.QueryContext(req.Context(), stmt.String(), args...); err != nil { + return + } + defer rows.Close() + type template struct { Name string `json:"name"` + Type string `json:"type"` Time models.Time `json:"time"` Country *string `json:"country,omitempty"` } - var rows *sql.Rows - if rows, err = conn.QueryContext(req.Context(), listPrintTemplatesSQL); err != nil { - return - } - defer rows.Close() - templates := []*template{} for rows.Next() { @@ -104,11 +131,12 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - ctx := req.Context() - name := mux.Vars(req)["name"] + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + ctx := req.Context() var data pgtype.Bytea - err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name).Scan(&data) + err = conn.QueryRowContext(ctx, selectPrintTemplateSQL, name, typ).Scan(&data) switch { case err == sql.ErrNoRows: @@ -136,8 +164,9 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - ctx := req.Context() - name := mux.Vars(req)["name"] + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + in := input.(*json.RawMessage) if name == "" { @@ -154,6 +183,8 @@ } return } + + ctx := req.Context() var tx *sql.Tx if tx, err = conn.BeginTx(ctx, nil); err != nil { return @@ -161,7 +192,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -177,7 +208,7 @@ } data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} - if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, &data); err != nil { + if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, typ, &data); err != nil { return } @@ -199,9 +230,10 @@ conn *sql.Conn, ) (jr JSONResult, err error) { + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + ctx := req.Context() - name := mux.Vars(req)["name"] - var tx *sql.Tx if tx, err = conn.BeginTx(ctx, nil); err != nil { return @@ -209,7 +241,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -228,7 +260,7 @@ return } - if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name); err != nil { + if _, err = tx.ExecContext(ctx, deletePrintTemplateSQL, name, typ); err != nil { return } @@ -251,8 +283,9 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - ctx := req.Context() - name := mux.Vars(req)["name"] + vars := mux.Vars(req) + name, typ := vars["name"], vars["type"] + in := input.(*json.RawMessage) if name == "" { @@ -269,6 +302,8 @@ } return } + + ctx := req.Context() var tx *sql.Tx if tx, err = conn.BeginTx(ctx, nil); err != nil { return @@ -276,7 +311,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name, typ).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -296,7 +331,7 @@ } data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} - if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, &data); err != nil { + if _, err = tx.ExecContext(ctx, updatePrintTemplateSQL, name, typ, &data); err != nil { return }