summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-11-10 17:45:03 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-11-10 17:45:03 -0500
commit77e855276e3672875d1d0612fe8a72be1b49b642 (patch)
treee651ecdd2b063551d6bdb434efb2af3d8f92fe95
parent898a9ed40e4422d9062b58efaf070cf31a7ef168 (diff)
Add delete functionality for recipes
-rw-r--r--backend/main.go29
1 files changed, 28 insertions, 1 deletions
diff --git a/backend/main.go b/backend/main.go
index c39f123..fc7317f 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -163,7 +163,34 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) {
} else if r.Method == "PUT" {
fmt.Printf("Update recipe \"%d\"...\n", recipe_id)
} else if r.Method == "DELETE" {
- fmt.Printf("Delete recipe \"%d\"...\n", recipe_id)
+ res, err := db.Exec(`DELETE FROM recipes where id = $1`,
+ recipe_id)
+ if err != nil {
+ panic(err)
+ }
+
+ var status int
+ var msg string
+ if res.RowsAffected() == 0 {
+ status = http.StatusNotFound
+ msg = "Recipe Not found"
+ } else {
+ status = http.StatusOK
+ msg = "Recipe Deleted Successfully"
+ }
+
+ resp := APIResponseItem{
+ Status: APIError{Code: status, Msg: msg},
+ Data: make([]APIDataRecipe, 0),
+ }
+
+ w.Header().Set("Content-Type",
+ "application/json; charset=UTF-8")
+ w.WriteHeader(http.StatusOK)
+ if err := json.NewEncoder(w).Encode(resp); err != nil {
+ panic(err)
+ }
+
}
}