From f774688228078494081ef1a709b1a32d76f48bd5 Mon Sep 17 00:00:00 2001 From: Adam French Date: Tue, 18 Nov 2025 15:51:47 +0000 Subject: [PATCH] implementing handlers --- backend/handlers/handle_post.go | 41 +++++++++++++++++++++++++++++++++ backend/handlers/handle_user.go | 1 + backend/handlers/handler.go | 9 ++++++++ 3 files changed, 51 insertions(+) create mode 100644 backend/handlers/handle_post.go create mode 100644 backend/handlers/handle_user.go create mode 100644 backend/handlers/handler.go diff --git a/backend/handlers/handle_post.go b/backend/handlers/handle_post.go new file mode 100644 index 0000000..d94b68d --- /dev/null +++ b/backend/handlers/handle_post.go @@ -0,0 +1,41 @@ +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"` + AuthorID uint `json:"authorID" binding:"required"` +} + +func (h *Handler) 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 *Handler) 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, AuthorID: input.AuthorID} + h.DB.Create(&post) + + c.JSON(http.StatusOK, gin.H{"data": post}) +} + +func (h *Handler) UpdatePost(c *gin.Context) { + +} diff --git a/backend/handlers/handle_user.go b/backend/handlers/handle_user.go new file mode 100644 index 0000000..5ac8282 --- /dev/null +++ b/backend/handlers/handle_user.go @@ -0,0 +1 @@ +package handlers diff --git a/backend/handlers/handler.go b/backend/handlers/handler.go new file mode 100644 index 0000000..13e07c2 --- /dev/null +++ b/backend/handlers/handler.go @@ -0,0 +1,9 @@ +package handlers + +import ( + "gorm.io/gorm" +) + +type Handler struct { + DB *gorm.DB +}