feat(svg): added svg template delete endpoint
build / Go-Build (push) Successful in 1m23s
build / Go-Build (pull_request) Successful in 1m23s

This commit is contained in:
2026-05-15 17:24:45 +02:00
parent 372bf4f97f
commit e39821a3e2
8 changed files with 82 additions and 4 deletions
+3
View File
@@ -32,5 +32,8 @@ func InitDB() {
} }
func Close() error { func Close() error {
if database == nil {
return nil
}
return database.Close() return database.Close()
} }
+17
View File
@@ -15,6 +15,7 @@ const SVGTABLECREATE string = `
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);" const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);"
const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE name = ?;" const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE name = ?;"
const GETSVGSQL string = "SELECT * FROM svg;" const GETSVGSQL string = "SELECT * FROM svg;"
const DELETESVGSQL string = "DELETE FROM svg WHERE name = ?;"
func InsertSVG(data *svg.TemplateData) error { func InsertSVG(data *svg.TemplateData) error {
json, err := json.Marshal(data.TemplateKeys) json, err := json.Marshal(data.TemplateKeys)
@@ -72,3 +73,19 @@ func GetSpecificSVG(id string) (svg.TemplateData, error) {
} }
return svg.TemplateData{Id: id, TemplateKeys: keys}, nil return svg.TemplateData{Id: id, TemplateKeys: keys}, nil
} }
func DeleteSvg(id string) (bool, error) {
res, err := database.Exec(DELETESVGSQL, id)
if err != nil {
return false, err
}
num, err := res.RowsAffected()
if err != nil {
return false, err
}
if num == 0 {
return false, nil
}
return true, nil
}
+26
View File
@@ -0,0 +1,26 @@
package routes
import (
"net/http"
"tomatentum.net/svg-templater/pkg/svg/actions"
)
func DeleteSvg(writer http.ResponseWriter, reader *http.Request) {
id := reader.PathValue("id")
if id == "" {
http.Error(writer, "No ID provided", http.StatusBadRequest)
return
}
success, err := actions.Delete(id)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
if !success {
http.Error(writer, "No template to delete.", http.StatusBadRequest)
return
}
}
-1
View File
@@ -41,7 +41,6 @@ func CreateSVG(writer http.ResponseWriter, r *http.Request) {
writer.Header().Add("Content-Type", "application/json") writer.Header().Add("Content-Type", "application/json")
json.NewEncoder(writer).Encode(data) json.NewEncoder(writer).Encode(data)
log.Println("Created SVG Template " + data.Id)
} }
func validateSVG(svgbuf []byte) (bool, error) { func validateSVG(svgbuf []byte) (bool, error) {
+8 -3
View File
@@ -22,11 +22,16 @@ func PrepareHTTP() {
routes.CreateSVG(w, r) routes.CreateSVG(w, r)
}) })
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) { registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" { switch r.Method {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) case "GET":
routes.DownloadSVG(w, r)
return
case "DELETE":
routes.DeleteSvg(w, r)
return return
} }
routes.DownloadSVG(w, r) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}) })
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) { registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
+20
View File
@@ -0,0 +1,20 @@
package actions
import (
"log"
"tomatentum.net/svg-templater/internal/database"
"tomatentum.net/svg-templater/pkg/svg"
)
func Delete(id string) (bool, error) {
if success, err := database.DeleteSvg(id); err != nil || !success {
return success, err
}
if err := svg.Storage.Delete(id); err != nil {
return false, err
}
log.Println("Deleted SVG Template " + id)
return true, nil
}
+1
View File
@@ -24,6 +24,7 @@ func Create(svgbuf []byte) (svg.TemplateData, error) {
return svg.TemplateData{}, err return svg.TemplateData{}, err
} }
log.Println("Created SVG Template " + data.Id)
return data, nil return data, nil
} }
+7
View File
@@ -15,6 +15,7 @@ import (
type SvgStorage interface { type SvgStorage interface {
Create(id string, svg io.Reader) (string, error) Create(id string, svg io.Reader) (string, error)
Get(id string) (io.ReadCloser, error) Get(id string) (io.ReadCloser, error)
Delete(id string) error
AddFont(reader io.Reader, format string) error AddFont(reader io.Reader, format string) error
GetFonts() ([]string, error) GetFonts() ([]string, error)
GetFontsDir() (string, error) GetFontsDir() (string, error)
@@ -68,6 +69,12 @@ func (f FileSvgStorage) Get(id string) (io.ReadCloser, error) {
return file, nil return file, nil
} }
func (f FileSvgStorage) Delete(id string) error {
path := filepath.Join(f.basepath, id+".svg")
defer log.Println("Deleted File: " + path)
return os.Remove(path)
}
func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) { func (f FileSvgStorage) CreatePublic(data io.Reader, filetype string) (string, error) {
path := filepath.Join(f.basepath, f.publicSubPath) path := filepath.Join(f.basepath, f.publicSubPath)
if err := os.MkdirAll(path, 0755); err != nil { if err := os.MkdirAll(path, 0755); err != nil {