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 /logs and returns all log records. func GetAll[T Model](c *gin.Context) { var logs []T if err := database.Preload().Find(&logs).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching logs: " + err.Error()}) return } c.JSON(http.StatusOK, logs) } // GetLog handles GET /logs/:id and returns a single log 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 log ID"}) return } var log T if err := database.Preload().First(&log, id).Error; err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "Log not found"}) return } c.JSON(http.StatusOK, log) } // CreateLog handles POST /logs and creates a new log record. func Create[T Model](c *gin.Context) { var log T if err := c.ShouldBindJSON(&log); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := database.DB.Create(&log).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create log: " + err.Error()}) return } // Optionally preload visibility before returning. database.Preload().First(&log, log.GetID()) c.JSON(http.StatusCreated, log) } // UpdateLog handles PUT /logs/:id and updates an existing log 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 log ID"}) return } var existing T if err := database.DB.First(&existing, id).Error; err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "Log not found"}) return } var log T if err := c.ShouldBindJSON(&log); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := database.DB.Model(&existing).Updates(log).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update log: " + err.Error()}) return } database.Preload().First(&existing, log.GetID()) c.JSON(http.StatusOK, existing) } // DeleteLog handles DELETE /logs/:id and deletes a log 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 log ID"}) return } var log T if err := database.DB.First(&log, id).Error; err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "Log not found"}) return } if err := database.DB.Delete(&log).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete log: " + err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "Log deleted successfully"}) }