implemented post creation, deletion and update api
This commit is contained in:
@@ -8,9 +8,9 @@ import (
|
||||
)
|
||||
|
||||
type CreatePostInput struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
AuthorID uint `json:"authorID" binding:"required"`
|
||||
Title string `json:"title" binding:"required"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
Author string `json:"author" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *Handler) GetPosts(c *gin.Context) {
|
||||
@@ -30,12 +30,30 @@ func (h *Handler) CreatePost(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": post})
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user