change log api to reflect data split into base and extension
This commit is contained in:
112
wagfarm-api/logs/generic/controller.go
Normal file
112
wagfarm-api/logs/generic/controller.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package generic
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/generic"
|
||||
"wagfarm-api/logs/base"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func LogCRUDController[T ExtendedLog]() generic.CRUDController {
|
||||
return generic.CRUDController{
|
||||
RController: generic.DefaultRController[T](),
|
||||
Create: Create[T],
|
||||
Update: Update[T],
|
||||
Delete: Delete[T],
|
||||
}
|
||||
}
|
||||
|
||||
// CreateLog handles POST /records and creates a new record record.
|
||||
func Create[T ExtendedLog](c *gin.Context) {
|
||||
var base base.BaseLog
|
||||
var record T
|
||||
if err := c.ShouldBindJSON(&base); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := database.DB.Create(&base).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create record: " + err.Error()})
|
||||
return
|
||||
}
|
||||
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 ExtendedLog](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 base base.BaseLog
|
||||
if err := c.ShouldBindJSON(&base); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.DB.Model(&existing).Updates(base).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update record: " + err.Error()})
|
||||
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 ExtendedLog](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
|
||||
}
|
||||
|
||||
base := record.GetBaseLog()
|
||||
if err := database.DB.Delete(&base).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete record: " + err.Error()})
|
||||
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"})
|
||||
}
|
||||
Reference in New Issue
Block a user