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.
97 lines
1.3 KiB
97 lines
1.3 KiB
package controllers |
|
|
|
import ( |
|
"go-crud/initializers" |
|
"go-crud/models" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
func PostsCreate(c *gin.Context) { |
|
// Get data |
|
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 |
|
c.JSON(200, gin.H{ |
|
"post": post, |
|
}) |
|
} |
|
|
|
func PostsIndex(c *gin.Context) { |
|
// Get post |
|
var posts []models.Post |
|
initializers.DB.Find(&posts) |
|
|
|
// Response |
|
c.JSON(200, gin.H{ |
|
"posts": posts, |
|
}) |
|
} |
|
|
|
func PostsShow(c *gin.Context) { |
|
// Get id |
|
id := c.Param("id") |
|
|
|
// Get the post |
|
var post models.Post |
|
initializers.DB.First(&post, id) |
|
|
|
// Response |
|
c.JSON(200, gin.H{ |
|
"post": post, |
|
}) |
|
} |
|
|
|
func PostsUpdate(c *gin.Context) { |
|
// Get id |
|
id := c.Param("id") |
|
|
|
// Get data |
|
var body struct { |
|
Body string |
|
Title string |
|
} |
|
|
|
c.Bind(&body) |
|
|
|
// find |
|
var post models.Post |
|
initializers.DB.First(&post, id) |
|
|
|
//Update |
|
initializers.DB.Model(&post).Updates(models.Post{ |
|
Title: body.Title, |
|
Body: body.Body, |
|
}) |
|
|
|
// Response |
|
c.JSON(200, gin.H{ |
|
"post": post, |
|
}) |
|
} |
|
|
|
func PostDelete(c *gin.Context) { |
|
// Get id |
|
id := c.Param("id") |
|
|
|
// Delete |
|
initializers.DB.Delete(&models.Post{}, id) |
|
|
|
// Response |
|
c.Status(200) |
|
}
|
|
|