datamodel done without validators
This commit is contained in:
@@ -36,99 +36,99 @@ func DefaultCRUDController[T Model]() CRUDController {
|
||||
}
|
||||
}
|
||||
|
||||
// GetLogs handles GET /logs and returns all log records.
|
||||
// GetLogs handles GET /records and returns all record records.
|
||||
func GetAll[T Model](c *gin.Context) {
|
||||
var logs []T
|
||||
var records []T
|
||||
|
||||
if err := database.Preload().Find(&logs).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching logs: " + err.Error()})
|
||||
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, logs)
|
||||
c.JSON(http.StatusOK, records)
|
||||
}
|
||||
|
||||
// GetLog handles GET /logs/:id and returns a single log record.
|
||||
// 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 log ID"})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record 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"})
|
||||
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, log)
|
||||
c.JSON(http.StatusOK, record)
|
||||
}
|
||||
|
||||
// CreateLog handles POST /logs and creates a new log record.
|
||||
// CreateLog handles POST /records and creates a new record record.
|
||||
func Create[T Model](c *gin.Context) {
|
||||
var log T
|
||||
if err := c.ShouldBindJSON(&log); err != nil {
|
||||
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(&log).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create log: " + err.Error()})
|
||||
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(&log, log.GetID())
|
||||
c.JSON(http.StatusCreated, log)
|
||||
database.Preload().First(&record, record.GetID())
|
||||
c.JSON(http.StatusCreated, record)
|
||||
}
|
||||
|
||||
// UpdateLog handles PUT /logs/:id and updates an existing log 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 log ID"})
|
||||
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": "Log not found"})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var log T
|
||||
if err := c.ShouldBindJSON(&log); err != nil {
|
||||
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(log).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update log: " + err.Error()})
|
||||
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, log.GetID())
|
||||
database.Preload().First(&existing, record.GetID())
|
||||
c.JSON(http.StatusOK, existing)
|
||||
}
|
||||
|
||||
// DeleteLog handles DELETE /logs/:id and deletes a log record.
|
||||
// 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 log ID"})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record 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"})
|
||||
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(&log).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete log: " + err.Error()})
|
||||
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": "Log deleted successfully"})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Record deleted successfully"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user