Skip to content

Instantly share code, notes, and snippets.

@s1s1ty
Created July 18, 2018 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1s1ty/0dab5f457c3566e8cfe1fab05b68cfe2 to your computer and use it in GitHub Desktop.
Save s1s1ty/0dab5f457c3566e8cfe1fab05b68cfe2 to your computer and use it in GitHub Desktop.
5th part
// 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