CUB-113: implement core CRUD API endpoints
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m4s
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m4s
- Add dtos package with request/response structs
- Add repositories: Material, Filament, Printer, PrintJob, UsageLog
- Add services: FilamentService, PrinterService, PrintJobService
- Add handlers for all 5 resources with consistent error responses
- Wire all endpoints into Chi router under /api
- Validation on POST/PUT filament endpoints
- Filter/pagination support on list endpoints
- Soft-delete for filaments (DELETE /api/filaments/{id})
- go build ./... && go vet ./... → PASS
This commit is contained in:
60
backend/internal/handlers/print_job_handler.go
Normal file
60
backend/internal/handlers/print_job_handler.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/services"
|
||||
)
|
||||
|
||||
// PrintJobHandler handles HTTP requests for print job operations.
|
||||
type PrintJobHandler struct {
|
||||
service *services.PrintJobService
|
||||
}
|
||||
|
||||
// NewPrintJobHandler creates a PrintJobHandler with the given service.
|
||||
func NewPrintJobHandler(service *services.PrintJobService) *PrintJobHandler {
|
||||
return &PrintJobHandler{service: service}
|
||||
}
|
||||
|
||||
// List handles GET /api/print-jobs — returns paginated, filtered print jobs.
|
||||
func (h *PrintJobHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
limit, offset := parsePagination(r)
|
||||
filter := repositories.PrintJobFilter{
|
||||
Status: r.URL.Query().Get("status"),
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}
|
||||
|
||||
if pidStr := r.URL.Query().Get("printer_id"); pidStr != "" {
|
||||
pid, err := strconv.Atoi(pidStr)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, dtos.ErrorResponse{
|
||||
Error: "invalid printer_id",
|
||||
Code: http.StatusBadRequest,
|
||||
})
|
||||
return
|
||||
}
|
||||
filter.PrinterID = &pid
|
||||
}
|
||||
|
||||
jobs, total, err := h.service.List(r.Context(), filter)
|
||||
if err != nil {
|
||||
slog.Error("failed to list print jobs", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
|
||||
Error: "internal server error",
|
||||
Code: http.StatusInternalServerError,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, dtos.ListResponse{
|
||||
Data: jobs,
|
||||
Total: total,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user