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:
74
backend/internal/services/validation.go
Normal file
74
backend/internal/services/validation.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
|
||||
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
||||
)
|
||||
|
||||
// colorHexPattern validates hex color strings like #FF0000 or #ff0000.
|
||||
var colorHexPattern = regexp.MustCompile(`^#[0-9A-Fa-f]{6}$`)
|
||||
|
||||
// validateFilamentSpool performs validation on a FilamentSpool entity.
|
||||
// Returns a descriptive error on failure.
|
||||
func validateFilamentSpool(s *models.FilamentSpool) error {
|
||||
if s.Name == "" {
|
||||
return errors.New("name is required")
|
||||
}
|
||||
if s.MaterialBaseID <= 0 {
|
||||
return errors.New("material_base_id is required")
|
||||
}
|
||||
if s.MaterialFinishID <= 0 {
|
||||
return errors.New("material_finish_id is required")
|
||||
}
|
||||
if !colorHexPattern.MatchString(s.ColorHex) {
|
||||
return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
|
||||
}
|
||||
if s.InitialGrams <= 0 {
|
||||
return errors.New("initial_grams must be greater than 0")
|
||||
}
|
||||
if s.RemainingGrams < 0 {
|
||||
return errors.New("remaining_grams must be >= 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateCreateFilamentRequest validates a creation DTO.
|
||||
func ValidateCreateFilamentRequest(req dtos.CreateFilamentRequest) error {
|
||||
if req.Name == "" {
|
||||
return errors.New("name is required")
|
||||
}
|
||||
if req.MaterialBaseID <= 0 {
|
||||
return errors.New("material_base_id is required")
|
||||
}
|
||||
if req.MaterialFinishID <= 0 {
|
||||
return errors.New("material_finish_id is required")
|
||||
}
|
||||
if !colorHexPattern.MatchString(req.ColorHex) {
|
||||
return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
|
||||
}
|
||||
if req.InitialGrams <= 0 {
|
||||
return errors.New("initial_grams must be greater than 0")
|
||||
}
|
||||
if req.RemainingGrams < 0 {
|
||||
return errors.New("remaining_grams must be >= 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateUpdateFilamentRequest validates partial update fields.
|
||||
func ValidateUpdateFilamentRequest(req dtos.UpdateFilamentRequest) error {
|
||||
if req.ColorHex != nil && !colorHexPattern.MatchString(*req.ColorHex) {
|
||||
return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
|
||||
}
|
||||
if req.InitialGrams != nil && *req.InitialGrams <= 0 {
|
||||
return errors.New("initial_grams must be greater than 0")
|
||||
}
|
||||
if req.RemainingGrams != nil && *req.RemainingGrams < 0 {
|
||||
return errors.New("remaining_grams must be >= 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user