Overview
The Automatización Backend follows a layered architecture pattern that separates concerns and promotes maintainability. The system is built with FastAPI and implements dependency injection for loose coupling between components.Architecture Layers
API Layer
FastAPI endpoints that handle HTTP requests and responses
Service Layer
Orchestrates business logic and coordinates between handlers
Core Layer
Implements restriction handlers and schedule building patterns
Data Layer
JSON-based data storage for entities (aulas, asignaturas, etc.)
Layer 1: API Layer (FastAPI)
The API layer is defined insrc/api.py and provides RESTful endpoints for the scheduling system.
FastAPI Application Structure
Dependency Injection Pattern
The API layer uses FastAPI’s dependency injection system to provide service instances:Key API Endpoints
- Available Classrooms
- Reserve Classroom
- Schedule Generation
POST
/api/v1/aulas-disponiblesFinds available classrooms for a subject based on:- Capacity constraints
- Compatibility requirements
- Occupancy status
Layer 2: Service Layer
Services orchestrate business logic and coordinate between the API and core restriction handlers.AulaDisponibleService
The primary service that determines classroom availability:Why this order?
Why this order?
The chain is ordered by computational efficiency:
- Capacity - Quick numeric comparison
- Compatibility - Simple type matching
- Occupancy - Requires checking all existing schedules (most expensive)
Service Responsibilities
Data Loading
Load JSON data files (aulas, asignaturas, programaciones)
Chain Orchestration
Set up and execute the restriction handler chain
Result Aggregation
Collect and format results from all handlers
Error Handling
Transform handler errors into user-friendly messages
Layer 3: Core Layer
The core layer contains business logic implemented through design patterns:Restriction Handlers (Chain of Responsibility)
See Constraint System for detailed coverage.Schedule Builders (Builder Pattern)
See Design Patterns for detailed coverage.Facade Pattern
TheHorarioFacade provides a unified interface to the complex schedule system:
Layer 4: Data Layer
Data is stored in JSON files for simplicity and easy inspection:Data Flow Example
Here’s how a request flows through all layers:Configuration and CORS
The application is configured with CORS middleware for frontend integration:Security Note: In production, replace
allow_origins=["*"] with specific allowed origins.Benefits of This Architecture
Separation of Concerns
Separation of Concerns
Each layer has a clear, single responsibility:
- API handles HTTP
- Services orchestrate logic
- Core implements business rules
- Data manages persistence
Testability
Testability
Each layer can be tested independently:
- API: Test with mock services
- Services: Test with mock handlers
- Core: Unit test business logic
- Data: Test with fixture files
Maintainability
Maintainability
Changes are localized:
- Add new endpoint → Modify API layer
- Change validation rules → Modify handlers
- Add new data source → Modify data layer
Extensibility
Extensibility
Easy to extend:
- Add new restriction handlers to chain
- Add new builder types
- Add new API endpoints
- Switch data storage backend
Next Steps
Design Patterns
Learn about Chain of Responsibility, Builder, and Facade patterns
Constraint System
Understand the restriction validation system
Data Model
Explore the entity structure and relationships