Files
ytdlp-site/handlers/set_timestamp.go
2025-05-01 05:50:53 -06:00

55 lines
1.3 KiB
Go

package handlers
import (
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"ytdlp-site/database"
"ytdlp-site/originals"
)
func SetTimestamp(c echo.Context) error {
// Parse the ID from the URL parameter
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid ID format"})
}
// Define a struct to receive the timestamp from JSON
type TimestampRequest struct {
Timestamp float64 `json:"timestamp"`
}
// Parse the request body
var req TimestampRequest
if err := c.Bind(&req); err != nil {
log.Errorln(err)
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
}
log.Debugln("set video", id, "timestamp:", req.Timestamp)
// Update the timestamp field with the value from the request
db := database.Get()
result := db.Model(&originals.Original{}).
Where("id = ?", id).
Update("timestamp", req.Timestamp)
// Handle database errors
if result.Error != nil {
log.Errorln(result.Error)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Database error"})
}
// Check if record was found
if result.RowsAffected == 0 {
log.Errorln(gorm.ErrRecordNotFound)
return c.JSON(http.StatusNotFound, map[string]string{"error": "Record not found"})
}
return c.NoContent(http.StatusOK)
}