preorder/controllers/format.go

75 lines
1.8 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 FindFormats(c *gin.Context) {
var Formats []models.Format
models.DB.Find(&Formats)
c.JSON(http.StatusOK, gin.H{"data": Formats})
}
func FindFormat(c *gin.Context) {
var Format models.Format
if err := models.DB.Where("id = ?", c.Param("id")).First(&Format).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
}
c.JSON(http.StatusOK, gin.H{"data": Format})
}
func CreateFormat(c *gin.Context) {
var input models.CreateFormatInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
2024-06-19 02:59:35 +00:00
format := models.NewFormat(input.ID, input.Format)
models.DB.Create(&format)
c.JSON(http.StatusOK, gin.H{"data": format})
2024-06-18 04:37:32 +00:00
}
func UpdateFormat(c *gin.Context) {
var Format models.Format
if err := models.DB.Where("id = ?", c.Param("id")).First(&Format).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
}
var input models.UpdateFormatInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
models.DB.Model(&Format).Updates(input)
c.JSON(http.StatusOK, gin.H{"data": Format})
}
func DeleteFormat(c *gin.Context) {
var Format models.Format
if err := models.DB.Where("id = ?", c.Param("id")).First(&Format).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
models.DB.Delete(&Format)
c.JSON(http.StatusOK, gin.H{"data": true})
}
func ApplyFormatRouter(router *gin.Engine) *gin.Engine {
router.GET("/formats", FindFormats)
router.POST("/formats", CreateFormat)
router.GET("/formats/:id", FindFormat)
router.PATCH("/formats/:id", UpdateFormat)
router.DELETE("/formats/:id", DeleteFormat)
return router
}