PostCreate done
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,7 +2,7 @@
|
|||||||
# If you prefer the allow list template instead of the deny list, see community template:
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
# Manual
|
# Manual
|
||||||
go-crud
|
*go-crud
|
||||||
.env
|
.env
|
||||||
#
|
#
|
||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
|
|||||||
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
9
main.go
9
main.go
@@ -1,8 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"gitea.sitodosi.com/betology/go-crud/controllers"
|
||||||
|
|
||||||
"gitea.sitodosi.com/betology/go-crud/initializers"
|
"gitea.sitodosi.com/betology/go-crud/initializers"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -15,11 +14,7 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.POST("/posts", controllers.PostsCreate)
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"message": "pong",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
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