preorder/controllers/author.go

78 lines
1.9 KiB
Go
Raw Normal View History

2024-06-18 04:37:32 +00:00
package controllers
import (
"net/http"
"preorder/models"
"github.com/gin-gonic/gin"
)
func FindAuthors(c *gin.Context) {
var Authors []models.Author
models.DB.Find(&Authors)
c.JSON(http.StatusOK, gin.H{"data": Authors})
}
func FindAuthor(c *gin.Context) {
var Author models.Author
if err := models.DB.Where("id = ?", c.Param("id")).First(&Author).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
}
c.JSON(http.StatusOK, gin.H{"data": Author})
}
func CreateAuthor(c *gin.Context) {
var input models.CreateAuthorInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
Author := models.Author{
ID: input.ID,
FullName: input.FullName,
}
models.DB.Create(&Author)
c.JSON(http.StatusOK, gin.H{"data": Author})
}
func UpdateAuthor(c *gin.Context) {
var Author models.Author
if err := models.DB.Where("id = ?", c.Param("id")).First(&Author).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
}
var input models.UpdateAuthorInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
models.DB.Model(&Author).Updates(input)
c.JSON(http.StatusOK, gin.H{"data": Author})
}
func DeleteAuthor(c *gin.Context) {
var Author models.Author
if err := models.DB.Where("id = ?", c.Param("id")).First(&Author).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
models.DB.Delete(&Author)
c.JSON(http.StatusOK, gin.H{"data": true})
}
func ApplyAuthorRouter(router *gin.Engine) *gin.Engine {
router.GET("/authors", FindAuthors)
router.POST("/authors", CreateAuthor)
router.GET("/authors/:id", FindAuthor)
router.PATCH("/authors/:id", UpdateAuthor)
router.DELETE("/authors/:id", DeleteAuthor)
return router
}