diff --git a/controllers/postController.go b/controllers/postController.go index 839b37b..120462f 100644 --- a/controllers/postController.go +++ b/controllers/postController.go @@ -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, + }) + +} diff --git a/main.go b/main.go index 1196c19..3c8e567 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,9 @@ func main() { r := gin.Default() r.POST("/posts", controllers.PostsCreate) + r.PUT("/posts/:id", controllers.PostsUpdate) 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") }