Added a way to delete a specific subcategory.
This commit is contained in:
+15
-12
@@ -132,6 +132,21 @@ func deleteCategory(id int64) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func deleteSubcategory(id int64) bool {
|
||||||
|
db := getSQLConnection()
|
||||||
|
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
_, err := db.Exec("DELETE FROM subcategory WHERE subcategory_id = $1;", id)
|
||||||
|
//TODO: log this if an error happened
|
||||||
|
if err != nil {
|
||||||
|
println(err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func getCategoryByID(id int64) (category Category, ok bool) {
|
func getCategoryByID(id int64) (category Category, ok bool) {
|
||||||
db := getSQLConnection()
|
db := getSQLConnection()
|
||||||
|
|
||||||
@@ -153,18 +168,6 @@ func getCategoryByID(id int64) (category Category, ok bool) {
|
|||||||
return category, true
|
return category, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// func getSubcategoryByID(id int64) (subcategory Subcategory, ok bool) {
|
|
||||||
// db := getSQLConnection()
|
|
||||||
|
|
||||||
// defer db.Close()
|
|
||||||
|
|
||||||
// if err := db.QueryRow("SELECT * FROM subcategory WHERE subcategory_id = $1;", id).Scan(&subcategory.ID, &subcategory.Name, &subcategory.CategoryId); err != nil {
|
|
||||||
// return subcategory, false
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return subcategory, true
|
|
||||||
// }
|
|
||||||
|
|
||||||
func getSubcategories(categoryID int64) (subcategories []Subcategory, ok bool) {
|
func getSubcategories(categoryID int64) (subcategories []Subcategory, ok bool) {
|
||||||
db := getSQLConnection()
|
db := getSQLConnection()
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ func main() {
|
|||||||
//Note: seems adding a trailing slash allows routing URLs like /category/ID#
|
//Note: seems adding a trailing slash allows routing URLs like /category/ID#
|
||||||
//No need for query parameters.
|
//No need for query parameters.
|
||||||
http.HandleFunc("/category/", category)
|
http.HandleFunc("/category/", category)
|
||||||
|
http.HandleFunc("/subcategory/", subcategory)
|
||||||
|
|
||||||
err := http.ListenAndServe(":8088", nil)
|
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) {
|
func getCategoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
id := strings.TrimPrefix(r.URL.Path, "/category/")
|
id := strings.TrimPrefix(r.URL.Path, "/category/")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user