105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
import uuid
|
|
|
|
from bd import make_engine
|
|
|
|
from bd.tables.organization import Organization
|
|
from route.auth_utils import require_admin_key
|
|
|
|
router = APIRouter(tags=["organizations"], prefix="/organizations")
|
|
|
|
|
|
class OrganizationCreate(BaseModel):
|
|
name_organization: str
|
|
|
|
class OrganizationUpdate(BaseModel):
|
|
name_organization: Optional[str] = None
|
|
|
|
|
|
|
|
def get_session():
|
|
from sqlalchemy.orm import sessionmaker
|
|
return sessionmaker(bind=make_engine(), future=True)
|
|
|
|
|
|
def _org_dict(org: Organization) -> dict:
|
|
return {
|
|
"id": str(org.id),
|
|
"name_organization": org.name_organization,
|
|
}
|
|
|
|
|
|
@router.post("/", status_code=201, dependencies=[Depends(require_admin_key)])
|
|
def create_organization(payload: OrganizationCreate):
|
|
Session = get_session()
|
|
with Session() as session:
|
|
existing = session.query(Organization).filter(
|
|
Organization.name_organization == payload.name_organization
|
|
).first()
|
|
if existing:
|
|
raise HTTPException(status_code=400, detail="Organization name already exists")
|
|
org = Organization(name_organization=payload.name_organization)
|
|
session.add(org)
|
|
session.commit()
|
|
session.refresh(org)
|
|
return _org_dict(org)
|
|
|
|
|
|
@router.get("/")
|
|
def list_organizations():
|
|
Session = get_session()
|
|
with Session() as session:
|
|
rows = session.query(Organization).all()
|
|
return [_org_dict(o) for o in rows]
|
|
|
|
|
|
@router.get("/{org_id}")
|
|
def get_organization(org_id: str):
|
|
Session = get_session()
|
|
try:
|
|
oid = uuid.UUID(org_id)
|
|
except Exception:
|
|
raise HTTPException(status_code=400, detail="Invalid UUID")
|
|
with Session() as session:
|
|
org = session.get(Organization, oid)
|
|
if not org:
|
|
raise HTTPException(status_code=404, detail="Organization not found")
|
|
return _org_dict(org)
|
|
|
|
|
|
@router.put("/{org_id}", dependencies=[Depends(require_admin_key)])
|
|
def update_organization(org_id: str, payload: OrganizationUpdate):
|
|
Session = get_session()
|
|
try:
|
|
oid = uuid.UUID(org_id)
|
|
except Exception:
|
|
raise HTTPException(status_code=400, detail="Invalid UUID")
|
|
with Session() as session:
|
|
org = session.get(Organization, oid)
|
|
if not org:
|
|
raise HTTPException(status_code=404, detail="Organization not found")
|
|
if payload.name_organization is not None:
|
|
org.name_organization = payload.name_organization
|
|
session.add(org)
|
|
session.commit()
|
|
session.refresh(org)
|
|
return _org_dict(org)
|
|
|
|
|
|
@router.delete("/{org_id}", status_code=204, dependencies=[Depends(require_admin_key)])
|
|
def delete_organization(org_id: str):
|
|
Session = get_session()
|
|
try:
|
|
oid = uuid.UUID(org_id)
|
|
except Exception:
|
|
raise HTTPException(status_code=400, detail="Invalid UUID")
|
|
with Session() as session:
|
|
org = session.get(Organization, oid)
|
|
if not org:
|
|
raise HTTPException(status_code=404, detail="Organization not found")
|
|
session.delete(org)
|
|
session.commit()
|
|
return {}
|