Skip to content

Instantly share code, notes, and snippets.

@s1s1ty s1s1ty/go-sql-5.go
Created Jul 18, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.