feat(page): add Page action endpoints
This commit is contained in:
@@ -0,0 +1,80 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg"
|
||||||
|
"tomatentum.net/svg-templater/pkg/svg/actions"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddPage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := r.ParseMultipartForm(128000000); err != nil {
|
||||||
|
http.Error(w, "Couldn't parse form data.", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileheaders := r.MultipartForm.File["files"]
|
||||||
|
|
||||||
|
id := r.PathValue("id")
|
||||||
|
|
||||||
|
pages := make([]svg.TemplatePage, len(fileheaders))
|
||||||
|
|
||||||
|
for i, fileh := range fileheaders {
|
||||||
|
file, err := fileh.Open()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, err := io.ReadAll(file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
page, err := actions.AddPage(id, buf)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pages[i] = page
|
||||||
|
}
|
||||||
|
|
||||||
|
err := json.NewEncoder(w).Encode(pages)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
id, pagestring := r.PathValue("id"), r.PathValue("page")
|
||||||
|
|
||||||
|
page, err := strconv.Atoi(pagestring)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Page must be a number", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ok, err := actions.DeletePage(id, page)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
http.Error(w, "Could not delete page", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,6 +43,22 @@ func PrepareHTTP() {
|
|||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
registerAuthorizedFunc("/svg/{id}/page/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
routes.AddPage(w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
registerAuthorizedFunc("/svg/{id}/page/{page}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "DELETE" {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
routes.DeletePage(w, r)
|
||||||
|
})
|
||||||
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
|
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
|
|||||||
Reference in New Issue
Block a user