- FastAPI backend with JWT authentication - MySQL database with full schema - Docker Compose orchestration - CSV data import for 43,208 airports and 519,999 aircraft - Complete PPR management API - Modernized replacement for PHP-based system
162 lines
4.7 KiB
Python
162 lines
4.7 KiB
Python
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
|
from sqlalchemy.orm import Session
|
|
from datetime import date
|
|
from app.api.deps import get_db, get_current_active_user
|
|
from app.crud.crud_ppr import ppr as crud_ppr
|
|
from app.schemas.ppr import PPR, PPRCreate, PPRUpdate, PPRStatus, PPRStatusUpdate
|
|
from app.models.ppr import User
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[PPR])
|
|
async def get_pprs(
|
|
request: Request,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
status: Optional[PPRStatus] = None,
|
|
date_from: Optional[date] = None,
|
|
date_to: Optional[date] = None,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""Get PPR records with optional filtering"""
|
|
pprs = crud_ppr.get_multi(
|
|
db, skip=skip, limit=limit, status=status,
|
|
date_from=date_from, date_to=date_to
|
|
)
|
|
return pprs
|
|
|
|
|
|
@router.post("/", response_model=PPR)
|
|
async def create_ppr(
|
|
request: Request,
|
|
ppr_in: PPRCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""Create a new PPR record"""
|
|
ppr = crud_ppr.create(db, obj_in=ppr_in, created_by=current_user.username)
|
|
|
|
# Send real-time update via WebSocket
|
|
if hasattr(request.app.state, 'connection_manager'):
|
|
await request.app.state.connection_manager.broadcast({
|
|
"type": "ppr_created",
|
|
"data": {
|
|
"id": ppr.id,
|
|
"ac_reg": ppr.ac_reg,
|
|
"status": ppr.status.value
|
|
}
|
|
})
|
|
|
|
return ppr
|
|
|
|
|
|
@router.get("/{ppr_id}", response_model=PPR)
|
|
async def get_ppr(
|
|
ppr_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""Get a specific PPR record"""
|
|
ppr = crud_ppr.get(db, ppr_id=ppr_id)
|
|
if not ppr:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="PPR record not found"
|
|
)
|
|
return ppr
|
|
|
|
|
|
@router.put("/{ppr_id}", response_model=PPR)
|
|
async def update_ppr(
|
|
request: Request,
|
|
ppr_id: int,
|
|
ppr_in: PPRUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""Update a PPR record"""
|
|
db_ppr = crud_ppr.get(db, ppr_id=ppr_id)
|
|
if not db_ppr:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="PPR record not found"
|
|
)
|
|
|
|
ppr = crud_ppr.update(db, db_obj=db_ppr, obj_in=ppr_in)
|
|
|
|
# Send real-time update
|
|
if hasattr(request.app.state, 'connection_manager'):
|
|
await request.app.state.connection_manager.broadcast({
|
|
"type": "ppr_updated",
|
|
"data": {
|
|
"id": ppr.id,
|
|
"ac_reg": ppr.ac_reg,
|
|
"status": ppr.status.value
|
|
}
|
|
})
|
|
|
|
return ppr
|
|
|
|
|
|
@router.patch("/{ppr_id}/status", response_model=PPR)
|
|
async def update_ppr_status(
|
|
request: Request,
|
|
ppr_id: int,
|
|
status_update: PPRStatusUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""Update PPR status (LANDED, DEPARTED, etc.)"""
|
|
ppr = crud_ppr.update_status(db, ppr_id=ppr_id, status=status_update.status)
|
|
if not ppr:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="PPR record not found"
|
|
)
|
|
|
|
# Log the status change (you might want to create a journal entry here)
|
|
|
|
# Send real-time update
|
|
if hasattr(request.app.state, 'connection_manager'):
|
|
await request.app.state.connection_manager.broadcast({
|
|
"type": "status_update",
|
|
"data": {
|
|
"id": ppr.id,
|
|
"ac_reg": ppr.ac_reg,
|
|
"status": ppr.status.value,
|
|
"timestamp": ppr.landed_dt.isoformat() if ppr.landed_dt else None
|
|
}
|
|
})
|
|
|
|
return ppr
|
|
|
|
|
|
@router.delete("/{ppr_id}", response_model=PPR)
|
|
async def delete_ppr(
|
|
request: Request,
|
|
ppr_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
"""Delete (soft delete) a PPR record"""
|
|
ppr = crud_ppr.delete(db, ppr_id=ppr_id)
|
|
if not ppr:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="PPR record not found"
|
|
)
|
|
|
|
# Send real-time update
|
|
if hasattr(request.app.state, 'connection_manager'):
|
|
await request.app.state.connection_manager.broadcast({
|
|
"type": "ppr_deleted",
|
|
"data": {
|
|
"id": ppr.id,
|
|
"ac_reg": ppr.ac_reg
|
|
}
|
|
})
|
|
|
|
return ppr |