From 633859cabe295579eac0757c173567fe9f5c1ad3 Mon Sep 17 00:00:00 2001 From: Tueem Date: Tue, 26 May 2026 14:47:03 +0200 Subject: [PATCH] feat(template): add name to the template definition --- .vscode/launch.json | 16 ++++++++++++++++ internal/database/svgdb.go | 32 ++++++++++++++++++++++--------- internal/routes/rename.go | 37 ++++++++++++++++++++++++++++++++++++ internal/routes/upload.go | 4 +++- internal/server/http.go | 3 +++ pkg/svg/actions/renamesvg.go | 17 +++++++++++++++++ pkg/svg/actions/upload.go | 4 ++-- pkg/svg/templates.go | 1 + 8 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 internal/routes/rename.go create mode 100644 pkg/svg/actions/renamesvg.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5ac2342 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/cmd/svg-templater", + "args": ["-data", "${workspaceFolder}/data"] + } + ] +} \ No newline at end of file diff --git a/internal/database/svgdb.go b/internal/database/svgdb.go index 9993dfa..d2dcaaf 100644 --- a/internal/database/svgdb.go +++ b/internal/database/svgdb.go @@ -2,20 +2,23 @@ package database import ( "encoding/json" + "log" "tomatentum.net/svg-templater/pkg/svg" ) const SVGTABLECREATE string = ` CREATE TABLE IF NOT EXISTS svg ( - name varchar(16) PRIMARY KEY NOT NULL, + id varchar(16) PRIMARY KEY NOT NULL, + name varchar(32), templatekeys longtext NOT NULL );` -const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);" -const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE name = ?;" +const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?, ?);" +const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE id = ?;" const GETSVGSQL string = "SELECT * FROM svg;" -const DELETESVGSQL string = "DELETE FROM svg WHERE name = ?;" +const DELETESVGSQL string = "DELETE FROM svg WHERE id = ?;" +const RENAMESVGSQL string = "UPDATE svg SET name = ? WHERE id = ?;" func InsertSVG(data *svg.TemplateData) error { json, err := json.Marshal(data.TemplateKeys) @@ -23,7 +26,7 @@ func InsertSVG(data *svg.TemplateData) error { return err } - if _, err := database.Exec(INSERTSVGSQL, data.Id, string(json)); err != nil { + if _, err := database.Exec(INSERTSVGSQL, data.Id, data.Name, string(json)); err != nil { return err } @@ -40,16 +43,17 @@ func GetSVG() ([]svg.TemplateData, error) { for result.Next() { var ( id string + name string keysjson []byte keys []string ) - if err := result.Scan(&id, &keysjson); err != nil { + if err := result.Scan(&id, &name, &keysjson); err != nil { return nil, err } if err := json.Unmarshal(keysjson, &keys); err != nil { return nil, err } - templates = append(templates, svg.TemplateData{Id: id, TemplateKeys: keys}) + templates = append(templates, svg.TemplateData{Id: id, Name: name, TemplateKeys: keys}) } if err := result.Err(); err != nil { @@ -62,16 +66,17 @@ func GetSpecificSVG(id string) (svg.TemplateData, error) { result := database.QueryRow(GETSPECIFICSVGSQL, id) var ( + name string keysjson []byte keys []string ) - if err := result.Scan(&id, &keysjson); err != nil { + if err := result.Scan(&id, &name, &keysjson); err != nil { return svg.TemplateData{}, err } if err := json.Unmarshal(keysjson, &keys); err != nil { return svg.TemplateData{}, err } - return svg.TemplateData{Id: id, TemplateKeys: keys}, nil + return svg.TemplateData{Id: id, Name: name, TemplateKeys: keys}, nil } func DeleteSvg(id string) (bool, error) { @@ -89,3 +94,12 @@ func DeleteSvg(id string) (bool, error) { } return true, nil } + +func RenameSvg(id string, name string) error { + res, err := database.Exec(RENAMESVGSQL, name, id) + log.Println(id, name, res) + if err != nil { + return err + } + return nil +} diff --git a/internal/routes/rename.go b/internal/routes/rename.go new file mode 100644 index 0000000..026c301 --- /dev/null +++ b/internal/routes/rename.go @@ -0,0 +1,37 @@ +package routes + +import ( + "encoding/json" + "net/http" + + "tomatentum.net/svg-templater/pkg/svg/actions" +) + +type renameRequest struct { + Name string +} + +func RenameSvg(w http.ResponseWriter, r *http.Request) { + contentType := r.Header.Get("Content-Type") + if contentType != "application/json" { + http.Error(w, "Incorrect Content-Type. Needs application/json.", http.StatusUnsupportedMediaType) + return + } + + var request renameRequest + err := json.NewDecoder(r.Body).Decode(&request) + + if err != nil { + http.Error(w, "Could not decode body.", http.StatusBadRequest) + return + } + + id := r.PathValue("id") + + err = actions.RenameSvg(id, request.Name) + + if err != nil { + http.Error(w, "Error while renaming", http.StatusInternalServerError) + return + } +} diff --git a/internal/routes/upload.go b/internal/routes/upload.go index f7ff97c..0674310 100644 --- a/internal/routes/upload.go +++ b/internal/routes/upload.go @@ -31,7 +31,9 @@ func CreateSVG(writer http.ResponseWriter, r *http.Request) { return } - data, err := actions.Create(readsvg) + name := r.URL.Query().Get("name") + + data, err := actions.Create(readsvg, name) if err != nil { http.Error(writer, err.Error(), http.StatusInternalServerError) diff --git a/internal/server/http.go b/internal/server/http.go index 66ae58f..4ef5bc6 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -33,6 +33,9 @@ func PrepareHTTP() { case "GET", "POST": routes.DownloadSVG(w, r) return + case "PATCH": + routes.RenameSvg(w, r) + return case "DELETE": routes.DeleteSvg(w, r) return diff --git a/pkg/svg/actions/renamesvg.go b/pkg/svg/actions/renamesvg.go new file mode 100644 index 0000000..6599875 --- /dev/null +++ b/pkg/svg/actions/renamesvg.go @@ -0,0 +1,17 @@ +package actions + +import ( + "log" + + "tomatentum.net/svg-templater/internal/database" +) + +func RenameSvg(id string, name string) error { + err := database.RenameSvg(id, name) + + if err != nil { + return err + } + log.Printf("Changed template %s name to %s.\n", id, name) + return nil +} diff --git a/pkg/svg/actions/upload.go b/pkg/svg/actions/upload.go index bc4bd47..d7ed00e 100644 --- a/pkg/svg/actions/upload.go +++ b/pkg/svg/actions/upload.go @@ -11,8 +11,8 @@ import ( "tomatentum.net/svg-templater/pkg/svg" ) -func Create(svgbuf []byte) (svg.TemplateData, error) { - data := svg.TemplateData{Id: generateId(), TemplateKeys: nil} +func Create(svgbuf []byte, name string) (svg.TemplateData, error) { + data := svg.TemplateData{Id: generateId(), Name: name, TemplateKeys: nil} populateKeys(&data, svgbuf) _, err := svg.Storage.Create(data.Id, bytes.NewReader(svgbuf)) diff --git a/pkg/svg/templates.go b/pkg/svg/templates.go index 6061f01..998c143 100644 --- a/pkg/svg/templates.go +++ b/pkg/svg/templates.go @@ -4,6 +4,7 @@ const KeyRegex string = `\{\{\s*(.*?)\s*\}\}` type TemplateData struct { Id string + Name string TemplateKeys []string }