feat(svg): add GET endpoint for all svg templates
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"tomatentum.net/svg-templater/internal/database"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
svgs, err := database.GetSVG()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Cannot fetch svgs from database", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
data, err := json.Marshal(svgs)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "An Error occurred while encoding json.", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Could not write data.", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
|
||||||
|
for _, svg := range svgs {
|
||||||
|
enc.Encode(svg)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,11 +15,15 @@ func PrepareHTTP() {
|
|||||||
fmt.Fprintln(w, "You are authorized!")
|
fmt.Fprintln(w, "You are authorized!")
|
||||||
})
|
})
|
||||||
registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "POST" {
|
switch r.Method {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
case "POST":
|
||||||
|
routes.CreateSVG(w, r)
|
||||||
|
return
|
||||||
|
case "GET":
|
||||||
|
routes.Get(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
routes.CreateSVG(w, r)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
})
|
})
|
||||||
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
|
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
|
|||||||
Reference in New Issue
Block a user