You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.4 KiB
93 lines
1.4 KiB
package controllers |
|
|
|
import ( |
|
"go-crud/initializers" |
|
"go-crud/models" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
func PostsCreate(c *gin.Context) { |
|
// Get data off req body |
|
var body struct { |
|
Body string |
|
Title string |
|
} |
|
c.Bind(&body) |
|
|
|
// Create a post |
|
post := models.Post{Title: body.Title, Body: body.Body} |
|
result := initializers.DB.Create(&post) |
|
|
|
if result.Error != nil { |
|
c.Status(400) |
|
return |
|
} |
|
// Return it |
|
c.JSON(200, gin.H{ |
|
"post": post, |
|
}) |
|
} |
|
|
|
func PostsIndex(c *gin.Context) { |
|
// Get the posts |
|
var posts []models.Post |
|
initializers.DB.Find(&posts) |
|
|
|
// Respond with them |
|
c.JSON(200, gin.H{ |
|
"post": posts, |
|
}) |
|
} |
|
|
|
func PostsShow(c *gin.Context) { |
|
// Get id off URL |
|
id := c.Param("id") |
|
|
|
// Get the posts |
|
var post models.Post |
|
initializers.DB.First(&post, id) |
|
|
|
// Respond with them |
|
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, |
|
}) |
|
} |
|
|
|
func PostsDelete(c *gin.Context) { |
|
// Get the id off the URL |
|
id := c.Param("id") |
|
|
|
// Delete the post |
|
initializers.DB.Delete(&models.Post{}, id) |
|
|
|
// Respond |
|
c.Status(200) |
|
}
|
|
|