From 77e855276e3672875d1d0612fe8a72be1b49b642 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 10 Nov 2019 17:45:03 -0500 Subject: Add delete functionality for recipes --- backend/main.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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) + } + } } -- cgit v1.1