Getting there
This commit is contained in:
167
backend/app/api/endpoints/arrivals.py
Normal file
167
backend/app/api/endpoints/arrivals.py
Normal file
@@ -0,0 +1,167 @@
|
||||
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_read_user, get_current_operator_user
|
||||
from app.crud.crud_arrival import arrival as crud_arrival
|
||||
from app.schemas.arrival import Arrival, ArrivalCreate, ArrivalUpdate, ArrivalStatus, ArrivalStatusUpdate
|
||||
from app.models.ppr import User
|
||||
from app.core.utils import get_client_ip
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Arrival])
|
||||
async def get_arrivals(
|
||||
request: Request,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
status: Optional[ArrivalStatus] = None,
|
||||
date_from: Optional[date] = None,
|
||||
date_to: Optional[date] = None,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_read_user)
|
||||
):
|
||||
"""Get arrival records with optional filtering"""
|
||||
arrivals = crud_arrival.get_multi(
|
||||
db, skip=skip, limit=limit, status=status,
|
||||
date_from=date_from, date_to=date_to
|
||||
)
|
||||
return arrivals
|
||||
|
||||
|
||||
@router.post("/", response_model=Arrival)
|
||||
async def create_arrival(
|
||||
request: Request,
|
||||
arrival_in: ArrivalCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Create a new arrival record"""
|
||||
arrival = crud_arrival.create(db, obj_in=arrival_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": "arrival_booked_in",
|
||||
"data": {
|
||||
"id": arrival.id,
|
||||
"registration": arrival.registration,
|
||||
"in_from": arrival.in_from,
|
||||
"status": arrival.status.value
|
||||
}
|
||||
})
|
||||
|
||||
return arrival
|
||||
|
||||
|
||||
@router.get("/{arrival_id}", response_model=Arrival)
|
||||
async def get_arrival(
|
||||
arrival_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_read_user)
|
||||
):
|
||||
"""Get a specific arrival record"""
|
||||
arrival = crud_arrival.get(db, arrival_id=arrival_id)
|
||||
if not arrival:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Arrival record not found"
|
||||
)
|
||||
return arrival
|
||||
|
||||
|
||||
@router.put("/{arrival_id}", response_model=Arrival)
|
||||
async def update_arrival(
|
||||
request: Request,
|
||||
arrival_id: int,
|
||||
arrival_in: ArrivalUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Update an arrival record"""
|
||||
db_arrival = crud_arrival.get(db, arrival_id=arrival_id)
|
||||
if not db_arrival:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Arrival record not found"
|
||||
)
|
||||
|
||||
arrival = crud_arrival.update(db, db_obj=db_arrival, obj_in=arrival_in)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "arrival_updated",
|
||||
"data": {
|
||||
"id": arrival.id,
|
||||
"registration": arrival.registration,
|
||||
"status": arrival.status.value
|
||||
}
|
||||
})
|
||||
|
||||
return arrival
|
||||
|
||||
|
||||
@router.patch("/{arrival_id}/status", response_model=Arrival)
|
||||
async def update_arrival_status(
|
||||
request: Request,
|
||||
arrival_id: int,
|
||||
status_update: ArrivalStatusUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Update arrival status"""
|
||||
arrival = crud_arrival.update_status(
|
||||
db,
|
||||
arrival_id=arrival_id,
|
||||
status=status_update.status,
|
||||
timestamp=status_update.timestamp
|
||||
)
|
||||
if not arrival:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Arrival record not found"
|
||||
)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "arrival_status_update",
|
||||
"data": {
|
||||
"id": arrival.id,
|
||||
"registration": arrival.registration,
|
||||
"status": arrival.status.value,
|
||||
"landed_dt": arrival.landed_dt.isoformat() if arrival.landed_dt else None
|
||||
}
|
||||
})
|
||||
|
||||
return arrival
|
||||
|
||||
|
||||
@router.delete("/{arrival_id}", response_model=Arrival)
|
||||
async def cancel_arrival(
|
||||
request: Request,
|
||||
arrival_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Cancel an arrival record"""
|
||||
arrival = crud_arrival.cancel(db, arrival_id=arrival_id)
|
||||
if not arrival:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Arrival record not found"
|
||||
)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "arrival_cancelled",
|
||||
"data": {
|
||||
"id": arrival.id,
|
||||
"registration": arrival.registration
|
||||
}
|
||||
})
|
||||
|
||||
return arrival
|
||||
167
backend/app/api/endpoints/departures.py
Normal file
167
backend/app/api/endpoints/departures.py
Normal file
@@ -0,0 +1,167 @@
|
||||
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_read_user, get_current_operator_user
|
||||
from app.crud.crud_departure import departure as crud_departure
|
||||
from app.schemas.departure import Departure, DepartureCreate, DepartureUpdate, DepartureStatus, DepartureStatusUpdate
|
||||
from app.models.ppr import User
|
||||
from app.core.utils import get_client_ip
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Departure])
|
||||
async def get_departures(
|
||||
request: Request,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
status: Optional[DepartureStatus] = None,
|
||||
date_from: Optional[date] = None,
|
||||
date_to: Optional[date] = None,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_read_user)
|
||||
):
|
||||
"""Get departure records with optional filtering"""
|
||||
departures = crud_departure.get_multi(
|
||||
db, skip=skip, limit=limit, status=status,
|
||||
date_from=date_from, date_to=date_to
|
||||
)
|
||||
return departures
|
||||
|
||||
|
||||
@router.post("/", response_model=Departure)
|
||||
async def create_departure(
|
||||
request: Request,
|
||||
departure_in: DepartureCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Create a new departure record"""
|
||||
departure = crud_departure.create(db, obj_in=departure_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": "departure_booked_out",
|
||||
"data": {
|
||||
"id": departure.id,
|
||||
"registration": departure.registration,
|
||||
"out_to": departure.out_to,
|
||||
"status": departure.status.value
|
||||
}
|
||||
})
|
||||
|
||||
return departure
|
||||
|
||||
|
||||
@router.get("/{departure_id}", response_model=Departure)
|
||||
async def get_departure(
|
||||
departure_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_read_user)
|
||||
):
|
||||
"""Get a specific departure record"""
|
||||
departure = crud_departure.get(db, departure_id=departure_id)
|
||||
if not departure:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Departure record not found"
|
||||
)
|
||||
return departure
|
||||
|
||||
|
||||
@router.put("/{departure_id}", response_model=Departure)
|
||||
async def update_departure(
|
||||
request: Request,
|
||||
departure_id: int,
|
||||
departure_in: DepartureUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Update a departure record"""
|
||||
db_departure = crud_departure.get(db, departure_id=departure_id)
|
||||
if not db_departure:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Departure record not found"
|
||||
)
|
||||
|
||||
departure = crud_departure.update(db, db_obj=db_departure, obj_in=departure_in)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "departure_updated",
|
||||
"data": {
|
||||
"id": departure.id,
|
||||
"registration": departure.registration,
|
||||
"status": departure.status.value
|
||||
}
|
||||
})
|
||||
|
||||
return departure
|
||||
|
||||
|
||||
@router.patch("/{departure_id}/status", response_model=Departure)
|
||||
async def update_departure_status(
|
||||
request: Request,
|
||||
departure_id: int,
|
||||
status_update: DepartureStatusUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Update departure status"""
|
||||
departure = crud_departure.update_status(
|
||||
db,
|
||||
departure_id=departure_id,
|
||||
status=status_update.status,
|
||||
timestamp=status_update.timestamp
|
||||
)
|
||||
if not departure:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Departure record not found"
|
||||
)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "departure_status_update",
|
||||
"data": {
|
||||
"id": departure.id,
|
||||
"registration": departure.registration,
|
||||
"status": departure.status.value,
|
||||
"departure_dt": departure.departure_dt.isoformat() if departure.departure_dt else None
|
||||
}
|
||||
})
|
||||
|
||||
return departure
|
||||
|
||||
|
||||
@router.delete("/{departure_id}", response_model=Departure)
|
||||
async def cancel_departure(
|
||||
request: Request,
|
||||
departure_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_operator_user)
|
||||
):
|
||||
"""Cancel a departure record"""
|
||||
departure = crud_departure.cancel(db, departure_id=departure_id)
|
||||
if not departure:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Departure record not found"
|
||||
)
|
||||
|
||||
# Send real-time update
|
||||
if hasattr(request.app.state, 'connection_manager'):
|
||||
await request.app.state.connection_manager.broadcast({
|
||||
"type": "departure_cancelled",
|
||||
"data": {
|
||||
"id": departure.id,
|
||||
"registration": departure.registration
|
||||
}
|
||||
})
|
||||
|
||||
return departure
|
||||
@@ -4,8 +4,12 @@ from sqlalchemy.orm import Session
|
||||
from app.api.deps import get_db
|
||||
from app.crud.crud_ppr import ppr as crud_ppr
|
||||
from app.crud.crud_local_flight import local_flight as crud_local_flight
|
||||
from app.crud.crud_departure import departure as crud_departure
|
||||
from app.crud.crud_arrival import arrival as crud_arrival
|
||||
from app.schemas.ppr import PPRPublic
|
||||
from app.models.local_flight import LocalFlightStatus
|
||||
from app.models.departure import DepartureStatus
|
||||
from app.models.arrival import ArrivalStatus
|
||||
from datetime import date
|
||||
|
||||
router = APIRouter()
|
||||
@@ -56,7 +60,7 @@ async def get_public_arrivals(db: Session = Depends(get_db)):
|
||||
|
||||
@router.get("/departures")
|
||||
async def get_public_departures(db: Session = Depends(get_db)):
|
||||
"""Get today's departures for public display (PPR and local flights)"""
|
||||
"""Get today's departures for public display (PPR, local flights, and departures to other airports)"""
|
||||
departures = crud_ppr.get_departures_today(db)
|
||||
|
||||
# Convert PPR departures to dictionaries
|
||||
@@ -70,7 +74,8 @@ async def get_public_departures(db: Session = Depends(get_db)):
|
||||
'etd': departure.etd,
|
||||
'departed_dt': departure.departed_dt,
|
||||
'status': departure.status.value,
|
||||
'isLocalFlight': False
|
||||
'isLocalFlight': False,
|
||||
'isDeparture': False
|
||||
})
|
||||
|
||||
# Add local flights with BOOKED_OUT status
|
||||
@@ -91,7 +96,29 @@ async def get_public_departures(db: Session = Depends(get_db)):
|
||||
'departed_dt': None,
|
||||
'status': 'BOOKED_OUT',
|
||||
'isLocalFlight': True,
|
||||
'flight_type': flight.flight_type.value
|
||||
'flight_type': flight.flight_type.value,
|
||||
'isDeparture': False
|
||||
})
|
||||
|
||||
# Add departures to other airports with BOOKED_OUT status
|
||||
departures_to_airports = crud_departure.get_multi(
|
||||
db,
|
||||
status=DepartureStatus.BOOKED_OUT,
|
||||
limit=1000
|
||||
)
|
||||
|
||||
# Convert departures to match the format for display
|
||||
for dep in departures_to_airports:
|
||||
departures_list.append({
|
||||
'ac_call': dep.callsign or dep.registration,
|
||||
'ac_reg': dep.registration,
|
||||
'ac_type': dep.type,
|
||||
'out_to': dep.out_to,
|
||||
'etd': dep.booked_out_dt,
|
||||
'departed_dt': None,
|
||||
'status': 'BOOKED_OUT',
|
||||
'isLocalFlight': False,
|
||||
'isDeparture': True
|
||||
})
|
||||
|
||||
return departures_list
|
||||
Reference in New Issue
Block a user