PostsUpdate done
This commit is contained in:
@@ -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,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -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")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user