rename context
This commit is contained in:
@@ -13,47 +13,47 @@ type CreatePostInput struct {
|
|||||||
Author string `json:"author" binding:"required"`
|
Author string `json:"author" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Store) GetPosts(c *gin.Context) {
|
func (store *Store) GetPosts(ctx *gin.Context) {
|
||||||
var posts []models.Post
|
var posts []models.Post
|
||||||
if err := h.DB.Find(&posts).Error; err != nil {
|
if err := store.DB.Find(&posts).Error; err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{"data": models.Post{}})
|
ctx.JSON(http.StatusOK, gin.H{"data": models.Post{}})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Store) CreatePost(c *gin.Context) {
|
func (store *Store) CreatePost(ctx *gin.Context) {
|
||||||
var input CreatePostInput
|
var input CreatePostInput
|
||||||
if err := c.ShouldBindBodyWithJSON(&input); err != nil {
|
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create post
|
// Create post
|
||||||
post := models.Post{Title: input.Title, Content: input.Content, Author: input.Author}
|
post := models.Post{Title: input.Title, Content: input.Content, Author: input.Author}
|
||||||
h.DB.Create(&post)
|
store.DB.Create(&post)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"data": post})
|
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Store) UpdatePost(c *gin.Context) {
|
func (store *Store) UpdatePost(ctx *gin.Context) {
|
||||||
id := c.Param("id")
|
id := ctx.Param("id")
|
||||||
var post models.Post
|
var post models.Post
|
||||||
if err := h.DB.First(&post, id).Error; err != nil {
|
if err := store.DB.First(&post, id).Error; err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var input CreatePostInput
|
var input CreatePostInput
|
||||||
if err := c.ShouldBindBodyWithJSON(&input); err != nil {
|
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
post.Title = input.Title
|
post.Title = input.Title
|
||||||
post.Content = input.Content
|
post.Content = input.Content
|
||||||
post.Author = input.Author
|
post.Author = input.Author
|
||||||
h.DB.Save(&post)
|
store.DB.Save(&post)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{"data": post})
|
ctx.JSON(http.StatusOK, gin.H{"data": post})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user