package handlers import ( "net/http" "adam-french.co.uk/backend/models" "github.com/gin-gonic/gin" ) type CreatePostInput struct { Title string `json:"title" binding:"required"` Content string `json:"content" binding:"required"` Author string `json:"author" binding:"required"` } func (h *Store) GetPosts(c *gin.Context) { var posts []models.Post if err := h.DB.Find(&posts).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"data": models.Post{}}) } func (h *Store) CreatePost(c *gin.Context) { var input CreatePostInput if err := c.ShouldBindBodyWithJSON(&input); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // Create post 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 *Store) 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}) }