feat(svg): added svg template delete endpoint
This commit is contained in:
@@ -32,5 +32,8 @@ func InitDB() {
|
||||
}
|
||||
|
||||
func Close() error {
|
||||
if database == nil {
|
||||
return nil
|
||||
}
|
||||
return database.Close()
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ const SVGTABLECREATE string = `
|
||||
const INSERTSVGSQL string = "INSERT INTO svg VALUES (?, ?);"
|
||||
const GETSPECIFICSVGSQL string = "SELECT * FROM svg WHERE name = ?;"
|
||||
const GETSVGSQL string = "SELECT * FROM svg;"
|
||||
const DELETESVGSQL string = "DELETE FROM svg WHERE name = ?;"
|
||||
|
||||
func InsertSVG(data *svg.TemplateData) error {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,6 @@ func CreateSVG(writer http.ResponseWriter, r *http.Request) {
|
||||
|
||||
writer.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(writer).Encode(data)
|
||||
log.Println("Created SVG Template " + data.Id)
|
||||
}
|
||||
|
||||
func validateSVG(svgbuf []byte) (bool, error) {
|
||||
|
||||
@@ -22,11 +22,16 @@ func PrepareHTTP() {
|
||||
routes.CreateSVG(w, r)
|
||||
})
|
||||
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
routes.DownloadSVG(w, r)
|
||||
return
|
||||
case "DELETE":
|
||||
routes.DeleteSvg(w, r)
|
||||
return
|
||||
}
|
||||
routes.DownloadSVG(w, r)
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
})
|
||||
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
|
||||
Reference in New Issue
Block a user