# Backend Development Guide The backend is a FastAPI application providing a RESTful API and WebSocket support for the Radionica 3D platform. ## Key Technologies - **FastAPI**: High-performance Python framework. - **SQLite**: Local database, chosen for simplicity and zero-latency access on small/medium traffic. - **Pydantic**: Data validation and serialization. - **JWT**: Token-based authentication. - **WebSockets**: Real-time communication for the order chat. ## API Structure (`backend/routers/`) - `auth.py`: Login, Registration, Password Reset, and Token management. - `orders.py`: Order creation, tracking, and management. - `admin.py`: High-privileged endpoints for managing users, reviews, and site settings. - `files.py`: Handling uploads and serving protected files (STL models, invoice PDFs). - `warehouse.py`: Inventory management for materials and filaments. ## Business Logic (`backend/services/`) We avoid putting heavy logic in routers. Use services instead: - **`AuditService`**: Logs every state change (e.g., when an admin changes an order price). - **`FiscalService`**: Implements the Montenegrin Electronic Fiscalization (EFI). Generates JIKR/IKOF codes and signed XMLs. - **`PricingService`**: Calculates the quote based on print time, material usage, and fixed costs. ## Database Management The project uses raw SQL for schema definitions (`schema.sql`) and migrations. - **Initialization**: `init_db.py` (archived in `legacy/`) was used initially. - **Migrations**: New changes are added via SQL files in `backend/migrations/` and executed via `run_migrations.py`. ## Authentication We use a custom dependency `get_current_user` in `backend/auth_utils.py` to protect routes. Example usage: ```python @router.get("/my-orders") async def get_orders(current_user: User = Depends(get_current_user)): return await order_service.get_for_user(current_user.id) ``` ## Running the Backend 1. Ensure the virtual environment is activated: ```bash # Windows backend\.venv\Scripts\activate ``` 2. Install dependencies: `pip install -r backend/requirements.txt` 3. Run with Uvicorn: ```bash python -m uvicorn main:app --app-dir backend --port 8000 --reload ``` ## Adding a New Feature 1. **Define Schema**: Add a Pydantic model in `backend/schemas.py`. 2. **Define Logic**: Create a new service or add a method to an existing one in `backend/services/`. 3. **Define Endpoint**: Create a route in the appropriate router file. 4. **Register Router**: Ensure the router is included in `backend/main.py`.