Created
June 21, 2016 04:34
-
-
Save narenaryan/c0d483893a653a189bab2bf31f99ce03 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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), | |
| }) | |
| }) |
This file contains hidden or 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
| // 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