Files
wagfarm/wagfarm-api/generic/controller.go

135 lines
3.4 KiB
Go

package generic
import (
"net/http"
"strconv"
"wagfarm-api/database"
"github.com/gin-gonic/gin"
)
type RController struct {
GetAll func(*gin.Context)
GetOne func(*gin.Context)
}
func DefaultRController[T Model]() RController {
return RController{
GetAll: GetAll[T],
GetOne: GetOne[T],
}
}
type CRUDController struct {
RController
Create func(*gin.Context)
Update func(*gin.Context)
Delete func(*gin.Context)
}
func DefaultCRUDController[T Model]() CRUDController {
return CRUDController{
RController: DefaultRController[T](),
Create: Create[T],
Update: Update[T],
Delete: Delete[T],
}
}
// GetLogs handles GET /records and returns all record records.
func GetAll[T Model](c *gin.Context) {
var records []T
if err := database.Preload().Find(&records).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching records: " + err.Error()})
return
}
c.JSON(http.StatusOK, records)
}
// GetLog handles GET /records/:id and returns a single record record.
func GetOne[T Model](c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record ID"})
return
}
var record T
if err := database.Preload().First(&record, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
return
}
c.JSON(http.StatusOK, record)
}
// CreateLog handles POST /records and creates a new record record.
func Create[T Model](c *gin.Context) {
var record T
if err := c.ShouldBindJSON(&record); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := database.DB.Create(&record).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create record: " + err.Error()})
return
}
// Optionally preload visibility before returning.
database.Preload().First(&record, record.GetID())
c.JSON(http.StatusCreated, record)
}
// UpdateLog handles PUT /records/:id and updates an existing record record.
func Update[T Model](c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record ID"})
return
}
var existing T
if err := database.DB.First(&existing, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
return
}
var record T
if err := c.ShouldBindJSON(&record); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := database.DB.Model(&existing).Updates(record).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update record: " + err.Error()})
return
}
database.Preload().First(&existing, record.GetID())
c.JSON(http.StatusOK, existing)
}
// DeleteLog handles DELETE /records/:id and deletes a record record.
func Delete[T Model](c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record ID"})
return
}
var record T
if err := database.DB.First(&record, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
return
}
if err := database.DB.Delete(&record).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete record: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Record deleted successfully"})
}