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,
})
}

View File

@@ -15,7 +15,9 @@ func main() {
r := gin.Default() r := gin.Default()
r.POST("/posts", controllers.PostsCreate) r.POST("/posts", controllers.PostsCreate)
r.PUT("/posts/:id", controllers.PostsUpdate)
r.GET("/posts", controllers.PostsIndex) r.GET("/posts", controllers.PostsIndex)
r.GET("/posts/:id", controllers.PostsShow)
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
} }