implemented post creation, deletion and update api
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
|||||||
type CreatePostInput struct {
|
type CreatePostInput struct {
|
||||||
Title string `json:"title" binding:"required"`
|
Title string `json:"title" binding:"required"`
|
||||||
Content string `json:"content" binding:"required"`
|
Content string `json:"content" binding:"required"`
|
||||||
AuthorID uint `json:"authorID" binding:"required"`
|
Author string `json:"author" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) GetPosts(c *gin.Context) {
|
func (h *Handler) GetPosts(c *gin.Context) {
|
||||||
@@ -30,12 +30,30 @@ func (h *Handler) CreatePost(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create post
|
// Create post
|
||||||
post := models.Post{Title: input.Title, Content: input.Content, AuthorID: input.AuthorID}
|
post := models.Post{Title: input.Title, Content: input.Content, Author: input.Author}
|
||||||
h.DB.Create(&post)
|
h.DB.Create(&post)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"data": post})
|
c.JSON(http.StatusOK, gin.H{"data": post})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) UpdatePost(c *gin.Context) {
|
func (h *Handler) UpdatePost(c *gin.Context) {
|
||||||
|
id := c.Param("id")
|
||||||
|
var post models.Post
|
||||||
|
if err := h.DB.First(&post, id).Error; err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var input CreatePostInput
|
||||||
|
if err := c.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
post.Title = input.Title
|
||||||
|
post.Content = input.Content
|
||||||
|
post.Author = input.Author
|
||||||
|
h.DB.Save(&post)
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"data": post})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,9 +47,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
h := handlers.Handler{DB: db}
|
||||||
|
|
||||||
r.GET("/books", handlers.GetPosts)
|
r.GET("/posts", h.GetPosts)
|
||||||
r.POST("/books", handlers.CreatePost)
|
r.POST("/posts", h.CreatePost)
|
||||||
|
r.PUT("/posts/:id", h.UpdatePost)
|
||||||
|
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/", func(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"message": "Hello World"})
|
c.JSON(200, gin.H{"message": "Hello World"})
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ type Post struct {
|
|||||||
gorm.Model // includes ID, CreatedAt, UpdatedAt, DeletedAt
|
gorm.Model // includes ID, CreatedAt, UpdatedAt, DeletedAt
|
||||||
Title string `gorm:"not null"`
|
Title string `gorm:"not null"`
|
||||||
Content string `gorm:"type:text; not null"`
|
Content string `gorm:"type:text; not null"`
|
||||||
AuthorID uint // foreign key to User
|
Author string `gorm:"not null"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user