Files
Extrudex/backend/internal/handlers/material_handler.go
Joshua fca2ef5b84
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m4s
CUB-113: implement core CRUD API endpoints
- 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
2026-05-06 14:24:58 -04:00

35 lines
1013 B
Go

package handlers
import (
"log/slog"
"net/http"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
)
// MaterialHandler handles requests for material lookup data.
type MaterialHandler struct {
repo *repositories.MaterialRepository
}
// NewMaterialHandler creates a MaterialHandler with the given repository.
func NewMaterialHandler(repo *repositories.MaterialRepository) *MaterialHandler {
return &MaterialHandler{repo: repo}
}
// List handles GET /api/materials — returns all material bases.
func (h *MaterialHandler) List(w http.ResponseWriter, r *http.Request) {
materials, err := h.repo.GetAll(r.Context())
if err != nil {
slog.Error("failed to list materials", "error", err)
writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
Error: "internal server error",
Code: http.StatusInternalServerError,
})
return
}
writeJSON(w, http.StatusOK, dtos.SingleResponse{Data: materials})
}