diff --git a/.gitignore b/.gitignore index 0dc62a2..6e55330 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ # 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 # Manual -go-crud +*go-crud .env # # Binaries for programs and plugins diff --git a/controllers/postController.go b/controllers/postController.go new file mode 100644 index 0000000..5a03866 --- /dev/null +++ b/controllers/postController.go @@ -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, + }) +} diff --git a/go-crud b/go-crud index 655d84d..8389744 100755 Binary files a/go-crud and b/go-crud differ diff --git a/main.go b/main.go index 1827b09..940d37b 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,7 @@ package main import ( - "net/http" - + "gitea.sitodosi.com/betology/go-crud/controllers" "gitea.sitodosi.com/betology/go-crud/initializers" "github.com/gin-gonic/gin" ) @@ -15,11 +14,7 @@ func init() { func main() { r := gin.Default() - r.GET("/", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "pong", - }) - }) + r.POST("/posts", controllers.PostsCreate) r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") }