PostCreate done
This commit is contained in:
32
controllers/postController.go
Normal file
32
controllers/postController.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"gitea.sitodosi.com/betology/go-crud/initializers"
|
||||
"gitea.sitodosi.com/betology/go-crud/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PostsCreate(c *gin.Context) {
|
||||
// Get data off rew 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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user