CUB-130: Build Add/Edit Filament form with backend endpoints for finishes and modifiers
All checks were successful
Dev Build / build-test (pull_request) Successful in 2m39s
All checks were successful
Dev Build / build-test (pull_request) Successful in 2m39s
This commit is contained in:
34
backend/internal/handlers/material_finish_handler.go
Normal file
34
backend/internal/handlers/material_finish_handler.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
|
||||
)
|
||||
|
||||
// MaterialFinishHandler handles requests for material finish lookup data.
|
||||
type MaterialFinishHandler struct {
|
||||
repo *repositories.MaterialFinishRepository
|
||||
}
|
||||
|
||||
// NewMaterialFinishHandler creates a MaterialFinishHandler with the given repository.
|
||||
func NewMaterialFinishHandler(repo *repositories.MaterialFinishRepository) *MaterialFinishHandler {
|
||||
return &MaterialFinishHandler{repo: repo}
|
||||
}
|
||||
|
||||
// List handles GET /api/finishes — returns all material finishes.
|
||||
func (h *MaterialFinishHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
finishes, err := h.repo.GetAll(r.Context())
|
||||
if err != nil {
|
||||
slog.Error("failed to list finishes", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
|
||||
Error: "internal server error",
|
||||
Code: http.StatusInternalServerError,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, dtos.SingleResponse{Data: finishes})
|
||||
}
|
||||
34
backend/internal/handlers/material_modifier_handler.go
Normal file
34
backend/internal/handlers/material_modifier_handler.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
|
||||
)
|
||||
|
||||
// MaterialModifierHandler handles requests for material modifier lookup data.
|
||||
type MaterialModifierHandler struct {
|
||||
repo *repositories.MaterialModifierRepository
|
||||
}
|
||||
|
||||
// NewMaterialModifierHandler creates a MaterialModifierHandler with the given repository.
|
||||
func NewMaterialModifierHandler(repo *repositories.MaterialModifierRepository) *MaterialModifierHandler {
|
||||
return &MaterialModifierHandler{repo: repo}
|
||||
}
|
||||
|
||||
// List handles GET /api/modifiers — returns all material modifiers.
|
||||
func (h *MaterialModifierHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
modifiers, err := h.repo.GetAll(r.Context())
|
||||
if err != nil {
|
||||
slog.Error("failed to list modifiers", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
|
||||
Error: "internal server error",
|
||||
Code: http.StatusInternalServerError,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, dtos.SingleResponse{Data: modifiers})
|
||||
}
|
||||
50
backend/internal/repositories/material_finish_repository.go
Normal file
50
backend/internal/repositories/material_finish_repository.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// MaterialFinishRepository handles database queries for material finishes.
|
||||
type MaterialFinishRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewMaterialFinishRepository creates a MaterialFinishRepository backed by the given pool.
|
||||
func NewMaterialFinishRepository(pool *pgxpool.Pool) *MaterialFinishRepository {
|
||||
return &MaterialFinishRepository{pool: pool}
|
||||
}
|
||||
|
||||
// GetAll returns all material finishes ordered by name.
|
||||
func (r *MaterialFinishRepository) GetAll(ctx context.Context) ([]models.MaterialFinish, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, name, description, created_at, updated_at
|
||||
FROM material_finishes
|
||||
ORDER BY name
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var finishes []models.MaterialFinish
|
||||
for rows.Next() {
|
||||
var f models.MaterialFinish
|
||||
if err := rows.Scan(
|
||||
&f.ID, &f.Name, &f.Description,
|
||||
&f.CreatedAt, &f.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finishes = append(finishes, f)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if finishes == nil {
|
||||
finishes = []models.MaterialFinish{}
|
||||
}
|
||||
return finishes, nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// MaterialModifierRepository handles database queries for material modifiers.
|
||||
type MaterialModifierRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewMaterialModifierRepository creates a MaterialModifierRepository backed by the given pool.
|
||||
func NewMaterialModifierRepository(pool *pgxpool.Pool) *MaterialModifierRepository {
|
||||
return &MaterialModifierRepository{pool: pool}
|
||||
}
|
||||
|
||||
// GetAll returns all material modifiers ordered by name.
|
||||
func (r *MaterialModifierRepository) GetAll(ctx context.Context) ([]models.MaterialModifier, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, name, description, created_at, updated_at
|
||||
FROM material_modifiers
|
||||
ORDER BY name
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var modifiers []models.MaterialModifier
|
||||
for rows.Next() {
|
||||
var m models.MaterialModifier
|
||||
if err := rows.Scan(
|
||||
&m.ID, &m.Name, &m.Description,
|
||||
&m.CreatedAt, &m.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
modifiers = append(modifiers, m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if modifiers == nil {
|
||||
modifiers = []models.MaterialModifier{}
|
||||
}
|
||||
return modifiers, nil
|
||||
}
|
||||
@@ -45,6 +45,8 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.R
|
||||
|
||||
// ── Repositories ──────────────────────────────────────────────────────
|
||||
materialRepo := repositories.NewMaterialRepository(dbPool)
|
||||
finishRepo := repositories.NewMaterialFinishRepository(dbPool)
|
||||
modifierRepo := repositories.NewMaterialModifierRepository(dbPool)
|
||||
filamentRepo := repositories.NewFilamentRepository(dbPool)
|
||||
printerRepo := repositories.NewPrinterRepository(dbPool)
|
||||
printJobRepo := repositories.NewPrintJobRepository(dbPool)
|
||||
@@ -57,6 +59,8 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.R
|
||||
|
||||
// ── Handlers ──────────────────────────────────────────────────────────
|
||||
materialHandler := handlers.NewMaterialHandler(materialRepo)
|
||||
finishHandler := handlers.NewMaterialFinishHandler(finishRepo)
|
||||
modifierHandler := handlers.NewMaterialModifierHandler(modifierRepo)
|
||||
filamentHandler := handlers.NewFilamentHandler(filamentService)
|
||||
printerHandler := handlers.NewPrinterHandler(printerService)
|
||||
printJobHandler := handlers.NewPrintJobHandler(printJobService)
|
||||
@@ -66,6 +70,8 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.R
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Use(middleware.Timeout(60 * time.Second))
|
||||
r.Get("/materials", materialHandler.List)
|
||||
r.Get("/finishes", finishHandler.List)
|
||||
r.Get("/modifiers", modifierHandler.List)
|
||||
|
||||
r.Route("/filaments", func(r chi.Router) {
|
||||
r.Get("/", filamentHandler.List)
|
||||
|
||||
Reference in New Issue
Block a user