Files
wagfarm/wagfarm-api/controllers/logs/logs.go

143 lines
3.8 KiB
Go

package logs
import (
"net/http"
"strconv"
"time"
"wagfarm-api/database"
"wagfarm-api/models"
"github.com/gin-gonic/gin"
)
type LogController interface {
GetAll(c *gin.Context)
GetOne(c *gin.Context)
Create(c *gin.Context)
Update(c *gin.Context)
Delete(c *gin.Context)
}
type GenericLogController struct{}
// GetLogs handles GET /logs and returns all log records.
func (l *GenericLogController) GetAll(c *gin.Context) {
var logs []models.Log
// Preload the Visibility association
if err := database.DB.Preload("Visibility").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 (l *GenericLogController) GetOne(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 logEntry models.Log
if err := database.DB.Preload("Visibility").First(&logEntry, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Log not found"})
return
}
c.JSON(http.StatusOK, logEntry)
}
// CreateLog handles POST /logs and creates a new log record.
func (l *GenericLogController) Create(c *gin.Context) {
var input models.Log
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// For safety, you might want to assign defaults (for example, date) if not provided.
if input.Date.IsZero() {
input.Date = time.Now()
}
newLog := models.Log{
Name: input.Name,
Notes: input.Notes,
LogType: input.LogType,
Date: input.Date,
Duration: input.Duration,
VisibiltyID: input.VisibiltyID,
}
if err := database.DB.Create(&newLog).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create log: " + err.Error()})
return
}
// Optionally preload visibility before returning.
database.DB.Preload("Visibility").First(&newLog, newLog.ID)
c.JSON(http.StatusCreated, newLog)
}
// UpdateLog handles PUT /logs/:id and updates an existing log record.
func (l *GenericLogController) Update(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 models.Log
if err := database.DB.First(&existing, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Log not found"})
return
}
var input models.Log
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
updated := models.Log{
Name: input.Name,
Notes: input.Notes,
LogType: input.LogType,
Date: input.Date,
Duration: input.Duration,
VisibiltyID: input.VisibiltyID,
}
if err := database.DB.Model(&existing).Updates(updated).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update log: " + err.Error()})
return
}
database.DB.Preload("Visibility").First(&existing, id)
c.JSON(http.StatusOK, existing)
}
// DeleteLog handles DELETE /logs/:id and deletes a log record.
func (l *GenericLogController) Delete(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 logEntry models.Log
if err := database.DB.First(&logEntry, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Log not found"})
return
}
if err := database.DB.Delete(&logEntry).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"})
}