PostsUpdate done

This commit is contained in:
2024-02-21 20:08:02 -06:00
parent 455de5a771
commit 860b1f75fa
2 changed files with 46 additions and 0 deletions

View File

@@ -41,3 +41,47 @@ func PostsIndex(c *gin.Context) {
})
}
func PostsShow(c *gin.Context) {
// Get if off url
id := c.Param("id")
// Get the post
var post models.Post
initializers.DB.First(&post, id)
// Respond with it
c.JSON(200, gin.H{
"post": post,
})
}
func PostsUpdate(c *gin.Context) {
// Get the id off the url
id := c.Param("id")
// Get the data off req body
var body struct {
Body string
Title string
}
c.Bind(&body)
// Find the post were updating
var post models.Post
initializers.DB.First(&post, id)
// Update it
initializers.DB.Model(&post).Updates(models.Post{
Title: body.Title,
Body: body.Body,
})
// Respond with it
c.JSON(200, gin.H{
"post": post,
})
}