Created
July 18, 2018 17:08
-
-
Save s1s1ty/0dab5f457c3566e8cfe1fab05b68cfe2 to your computer and use it in GitHub Desktop.
5th part
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UpdatePost update a spesific post | |
func UpdatePost(w http.ResponseWriter, r *http.Request) { | |
var post Post | |
id := chi.URLParam(r, "id") | |
json.NewDecoder(r.Body).Decode(&post) | |
query, err := db.Prepare("Update posts set title=?, content=? where id=?") | |
catch(err) | |
_, er := query.Exec(post.Title, post.Content, id) | |
catch(er) | |
defer query.Close() | |
respondwithJSON(w, http.StatusOK, map[string]string{"message": "update successfully"}) | |
} | |
// DeletePost remove a spesific post | |
func DeletePost(w http.ResponseWriter, r *http.Request) { | |
id := chi.URLParam(r, "id") | |
query, err := db.Prepare("delete from posts where id=?") | |
catch(err) | |
_, er := query.Exec(id) | |
catch(er) | |
query.Close() | |
respondwithJSON(w, http.StatusOK, map[string]string{"message": "successfully deleted"}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment