Added a way to delete a specific subcategory.

This commit is contained in:
2021-09-09 16:07:49 -05:00
parent 5feb32a122
commit f1272b86e8
2 changed files with 46 additions and 12 deletions
+31
View File
@@ -30,6 +30,7 @@ func main() {
//Note: seems adding a trailing slash allows routing URLs like /category/ID#
//No need for query parameters.
http.HandleFunc("/category/", category)
http.HandleFunc("/subcategory/", subcategory)
err := http.ListenAndServe(":8088", nil)
@@ -63,6 +64,36 @@ func category(w http.ResponseWriter, r *http.Request) {
}
}
func subcategory(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "DELETE":
deleteSubcategoryHandler(w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
}
}
func deleteSubcategoryHandler(w http.ResponseWriter, r *http.Request) {
id := strings.TrimPrefix(r.URL.Path, "/subcategory/")
value, err := strconv.ParseInt(id, 0, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if !deleteSubcategory(value) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Failed to delete %d", value)))
return
}
w.WriteHeader(http.StatusOK)
}
func getCategoryHandler(w http.ResponseWriter, r *http.Request) {
id := strings.TrimPrefix(r.URL.Path, "/category/")