Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Created June 21, 2016 04:34
Show Gist options
  • Select an option

  • Save narenaryan/c0d483893a653a189bab2bf31f99ce03 to your computer and use it in GitHub Desktop.

Select an option

Save narenaryan/c0d483893a653a189bab2bf31f99ce03 to your computer and use it in GitHub Desktop.
// Delete resources
router.DELETE("/person", func(c *gin.Context) {
id := c.Query("id")
stmt, err := db.Prepare("delete from person where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(id)
if err != nil {
fmt.Print(err.Error())
}
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully deleted user: %s", id),
})
})
// PUT - update a person details
router.PUT("/person", func(c *gin.Context) {
var buffer bytes.Buffer
id := c.Query("id")
first_name := c.PostForm("first_name")
last_name := c.PostForm("last_name")
stmt, err := db.Prepare("update person set first_name= ?, last_name= ? where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(first_name, last_name, id)
if err != nil {
fmt.Print(err.Error())
}
// Fastest way to append strings
buffer.WriteString(first_name)
buffer.WriteString(" ")
buffer.WriteString(last_name)
defer stmt.Close()
name := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully updated to %s", name),
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment