trying around with different routing and controller structures
This commit is contained in:
136
wagfarm-api/controllers/logs/seedinglog.go
Normal file
136
wagfarm-api/controllers/logs/seedinglog.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SeedingLogController struct {
|
||||
GenericLogController
|
||||
}
|
||||
|
||||
// GetLogs handles GET /logs and returns all log records.
|
||||
func (l *SeedingLogController) GetAll(c *gin.Context) {
|
||||
var logs []models.SeedingLog
|
||||
// Preload the Visibility association
|
||||
if err := database.DB.Preload("Log").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 *SeedingLogController) 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 *SeedingLogController) 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 *SeedingLogController) 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 *SeedingLogController) 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"})
|
||||
}
|
||||
Reference in New Issue
Block a user