Mercurial > gemma
changeset 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 | 2bf92b876984 |
children | 0627565fb4d2 |
files | pkg/controllers/printtemplates.go pkg/controllers/routes.go pkg/models/printemplates.go |
diffstat | 3 files changed, 10 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/printtemplates.go Fri Feb 08 12:51:20 2019 +0100 +++ b/pkg/controllers/printtemplates.go Fri Feb 08 12:56:33 2019 +0100 @@ -16,6 +16,7 @@ import ( "bytes" "database/sql" + "encoding/json" "net/http" "time" @@ -122,16 +123,17 @@ ) (jr JSONResult, err error) { ctx := req.Context() - in := input.(*models.PrintTemplateIn) + name := mux.Vars(req)["name"] + in := input.(*json.RawMessage) - if in.Name == "" { + if name == "" { err = JSONError{ Code: http.StatusBadRequest, Message: "Template must have a none empty name", } return } - if len(in.Template) == 0 { + if len(*in) == 0 { err = JSONError{ Code: http.StatusBadRequest, Message: "Template must have a none empty template", @@ -145,7 +147,7 @@ defer tx.Rollback() var dummy bool - err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, in.Name).Scan(&dummy) + err = tx.QueryRowContext(ctx, hasPrintTemplateSQL, name).Scan(&dummy) switch { case err == sql.ErrNoRows: @@ -159,9 +161,9 @@ } return } - data := pgtype.Bytea{Bytes: in.Template, Status: pgtype.Present} + data := pgtype.Bytea{Bytes: *in, Status: pgtype.Present} - if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, in.Name, &data); err != nil { + if _, err = tx.ExecContext(ctx, insertPrintTemplateSQL, name, &data); err != nil { return } @@ -171,7 +173,7 @@ jr = JSONResult{ Code: http.StatusCreated, Result: map[string]string{ - "created": in.Name, + "created": name, }, } return
--- a/pkg/controllers/routes.go Fri Feb 08 12:51:20 2019 +0100 +++ b/pkg/controllers/routes.go Fri Feb 08 12:56:33 2019 +0100 @@ -105,7 +105,7 @@ })).Methods(http.MethodGet) api.Handle("/templates/print/{name}", waterwayAdmin(&JSONHandler{ - Input: func(*http.Request) interface{} { return new(models.PrintTemplateIn) }, + Input: func(*http.Request) interface{} { return &json.RawMessage{} }, Handle: createPrintTemplate, Limit: maxPrintTemplateSize, })).Methods(http.MethodPost)
--- a/pkg/models/printemplates.go Fri Feb 08 12:51:20 2019 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ -// This is Free Software under GNU Affero General Public License v >= 3.0 -// without warranty, see README.md and license for details. -// -// SPDX-License-Identifier: AGPL-3.0-or-later -// License-Filename: LICENSES/AGPL-3.0.txt -// -// Copyright (C) 2018 by via donau -// – Österreichische Wasserstraßen-Gesellschaft mbH -// Software engineering by Intevation GmbH -// -// Author(s): -// * Sascha L. Teichmann <sascha.teichmann@intevation.de> - -package models - -import "encoding/json" - -type PrintTemplateIn struct { - Name string `json:"name"` - Template json.RawMessage `json:"template"` -}