Files
bookhoard/internal/handlers/jobs.go
T
john-okeefe 1e05470fbb refactor(handlers): update all handlers for Echo v5 compatibility
Update all handler functions to use *echo.Context (pointer) instead of echo.Context (value) as required by Echo v5.

Changes across all handler files:
- analytics.go: Update handler signatures
- auth.go: Update authentication handler signatures
- book_matching.go: Update matching handler signatures
- collections.go: Update collection handler signatures
- collections_preview_test.go: Update test signatures
- commonhandlers.go: Update common handler signatures
- conflicts.go: Update conflict handler signatures
- context.go: Update context handler signatures
- dashboard.go: Update dashboard handler signatures
- devices.go: Update device handler signatures
- jobs.go: Update job handler signatures
- kobo.go: Update Kobo handler signatures
- koreader.go: Update Koreader handler signatures
- library.go: Update library handler signatures
- matching.go: Update matching handler signatures
- media.go: Update media handler signatures
- opds.go: Update OPDS handler signatures
- progress.go: Update progress handler signatures
- queue.go: Update queue handler signatures
- refresh_token.go: Update token handler signatures
- scanner.go: Update scanner handler signatures
- sidecar.go: Update sidecar handler signatures
- sync.go: Update sync handler signatures
- system_settings.go: Update settings handler signatures
- websocket.go: Update WebSocket handler signatures

All handlers now properly implement Echo v5's pointer-based context pattern.
This change is necessary for type safety and compatibility with Echo v5's
improved context handling and WebSocket support.
2026-03-06 14:00:28 -05:00

88 lines
1.9 KiB
Go

package handlers
import (
"net/http"
"github.com/google/uuid"
"github.com/labstack/echo/v5"
"bookhoard/internal/database"
"bookhoard/internal/services"
)
type JobsHandler struct {
db *database.Queries
worker *services.Worker
}
func NewJobsHandler(db *database.Queries, worker *services.Worker) *JobsHandler {
return &JobsHandler{
db: db,
worker: worker,
}
}
// CreateJob creates a new job based on type
func (h *JobsHandler) CreateJob(c *echo.Context) error {
var req struct {
Type string `json:"type"`
Params map[string]interface{} `json:"params"`
}
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"error": "Invalid request body",
})
}
// Validate job type
var jobType services.JobType
switch req.Type {
case "import", "convert", "thumbnails", "backup", "analytics":
jobType = services.JobType(req.Type)
default:
return c.JSON(http.StatusBadRequest, map[string]string{
"error": "Invalid job type",
})
}
// Add database to params
req.Params["db"] = h.db
// Create job
job := &services.Job{
ID: uuid.New().String(),
Type: jobType,
Params: req.Params,
Status: services.JobStatusPending,
}
// Enqueue job
if err := h.worker.EnqueueJob(job); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": "Failed to enqueue job",
})
}
return c.JSON(http.StatusAccepted, map[string]interface{}{
"message": "Job created",
"job_id": job.ID,
"type": req.Type,
"status": "pending",
})
}
// GetJobStatus returns the status of a specific job
func (h *JobsHandler) GetJobStatus(c *echo.Context) error {
jobID := c.Param("jobId")
result, exists := h.worker.GetJobStatus(jobID)
if !exists {
return c.JSON(http.StatusNotFound, map[string]string{
"error": "Job not found",
})
}
return c.JSON(http.StatusOK, result)
}