implementing handlers
This commit is contained in:
41
backend/handlers/handle_post.go
Normal file
41
backend/handlers/handle_post.go
Normal file
@@ -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) {
|
||||
|
||||
}
|
||||
1
backend/handlers/handle_user.go
Normal file
1
backend/handlers/handle_user.go
Normal file
@@ -0,0 +1 @@
|
||||
package handlers
|
||||
9
backend/handlers/handler.go
Normal file
9
backend/handlers/handler.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
Reference in New Issue
Block a user