Added a way to delete a specific subcategory.
This commit is contained in:
@@ -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/")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user