""" Tenants API routes """ from fastapi import APIRouter, Depends, HTTPException, status from typing import List from pydantic import BaseModel from sqlalchemy.orm import Session from ...database import SessionLocal from ...models import Tenant from ...config.settings import settings router = APIRouter() # Pydantic models for tenants class TenantCreate(BaseModel): name: str subdomain: str class Config: json_schema_extra = { "example": { "name": "Acme Corporation", "subdomain": "acme" } } class TenantResponse(BaseModel): id: int name: str subdomain: str is_active: bool class Config: from_attributes = True json_schema_extra = { "example": { "id": 1, "name": "Acme Corporation", "subdomain": "acme", "is_active": True } } @router.get("/", response_model=List[TenantResponse]) async def get_tenants(skip: int = 0, limit: int = 100, db: Session = Depends(SessionLocal)): """Get all tenants""" tenants = db.query(Tenant).offset(skip).limit(limit).all() return tenants @router.get("/{tenant_id}", response_model=TenantResponse) async def get_tenant(tenant_id: int, db: Session = Depends(SessionLocal)): """Get a specific tenant""" tenant = db.query(Tenant).filter(Tenant.id == tenant_id).first() if not tenant: raise HTTPException(status_code=404, detail="Tenant not found") return tenant @router.post("/", response_model=TenantResponse) async def create_tenant(tenant: TenantCreate, db: Session = Depends(SessionLocal)): """Create a new tenant""" if not settings.MULTI_TENANT_ENABLED: raise HTTPException(status_code=400, detail="Multi-tenant is not enabled") db_tenant = Tenant(**tenant.model_dump()) db.add(db_tenant) db.commit() db.refresh(db_tenant) return db_tenant