feat(template): add name to the template definition
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user