Initial commit: NextGen PPR System
- 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
This commit is contained in:
1
backend/app/crud/__init__.py
Normal file
1
backend/app/crud/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Empty __init__.py files to make Python packages
|
||||
120
backend/app/crud/crud_ppr.py
Normal file
120
backend/app/crud/crud_ppr.py
Normal file
@@ -0,0 +1,120 @@
|
||||
from typing import List, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import and_, or_, func, desc
|
||||
from datetime import date, datetime
|
||||
from app.models.ppr import PPRRecord, PPRStatus
|
||||
from app.schemas.ppr import PPRCreate, PPRUpdate
|
||||
|
||||
|
||||
class CRUDPPR:
|
||||
def get(self, db: Session, ppr_id: int) -> Optional[PPRRecord]:
|
||||
return db.query(PPRRecord).filter(PPRRecord.id == ppr_id).first()
|
||||
|
||||
def get_multi(
|
||||
self,
|
||||
db: Session,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
status: Optional[PPRStatus] = None,
|
||||
date_from: Optional[date] = None,
|
||||
date_to: Optional[date] = None
|
||||
) -> List[PPRRecord]:
|
||||
query = db.query(PPRRecord)
|
||||
|
||||
if status:
|
||||
query = query.filter(PPRRecord.status == status)
|
||||
|
||||
if date_from:
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.date(PPRRecord.eta) >= date_from,
|
||||
func.date(PPRRecord.etd) >= date_from
|
||||
)
|
||||
)
|
||||
|
||||
if date_to:
|
||||
query = query.filter(
|
||||
or_(
|
||||
func.date(PPRRecord.eta) <= date_to,
|
||||
func.date(PPRRecord.etd) <= date_to
|
||||
)
|
||||
)
|
||||
|
||||
return query.order_by(desc(PPRRecord.submitted_dt)).offset(skip).limit(limit).all()
|
||||
|
||||
def get_arrivals_today(self, db: Session) -> List[PPRRecord]:
|
||||
"""Get today's arrivals"""
|
||||
today = date.today()
|
||||
return db.query(PPRRecord).filter(
|
||||
and_(
|
||||
func.date(PPRRecord.eta) == today,
|
||||
PPRRecord.status == PPRStatus.NEW
|
||||
)
|
||||
).order_by(PPRRecord.eta).all()
|
||||
|
||||
def get_departures_today(self, db: Session) -> List[PPRRecord]:
|
||||
"""Get today's departures"""
|
||||
today = date.today()
|
||||
return db.query(PPRRecord).filter(
|
||||
and_(
|
||||
func.date(PPRRecord.etd) == today,
|
||||
PPRRecord.status == PPRStatus.LANDED
|
||||
)
|
||||
).order_by(PPRRecord.etd).all()
|
||||
|
||||
def create(self, db: Session, obj_in: PPRCreate, created_by: str) -> PPRRecord:
|
||||
db_obj = PPRRecord(
|
||||
**obj_in.dict(),
|
||||
created_by=created_by,
|
||||
status=PPRStatus.NEW
|
||||
)
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update(self, db: Session, db_obj: PPRRecord, obj_in: PPRUpdate) -> PPRRecord:
|
||||
update_data = obj_in.dict(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
setattr(db_obj, field, value)
|
||||
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update_status(
|
||||
self,
|
||||
db: Session,
|
||||
ppr_id: int,
|
||||
status: PPRStatus
|
||||
) -> Optional[PPRRecord]:
|
||||
db_obj = self.get(db, ppr_id)
|
||||
if not db_obj:
|
||||
return None
|
||||
|
||||
db_obj.status = status
|
||||
|
||||
# Set timestamps based on status
|
||||
if status == PPRStatus.LANDED:
|
||||
db_obj.landed_dt = datetime.utcnow()
|
||||
elif status == PPRStatus.DEPARTED:
|
||||
db_obj.departed_dt = datetime.utcnow()
|
||||
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def delete(self, db: Session, ppr_id: int) -> Optional[PPRRecord]:
|
||||
db_obj = self.get(db, ppr_id)
|
||||
if db_obj:
|
||||
# Soft delete by setting status
|
||||
db_obj.status = PPRStatus.DELETED
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
|
||||
ppr = CRUDPPR()
|
||||
39
backend/app/crud/crud_user.py
Normal file
39
backend/app/crud/crud_user.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from typing import Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from app.models.ppr import User
|
||||
from app.schemas.ppr import UserCreate
|
||||
from app.core.security import get_password_hash, verify_password
|
||||
|
||||
|
||||
class CRUDUser:
|
||||
def get(self, db: Session, user_id: int) -> Optional[User]:
|
||||
return db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
def get_by_username(self, db: Session, username: str) -> Optional[User]:
|
||||
return db.query(User).filter(User.username == username).first()
|
||||
|
||||
def create(self, db: Session, obj_in: UserCreate) -> User:
|
||||
hashed_password = get_password_hash(obj_in.password)
|
||||
db_obj = User(
|
||||
username=obj_in.username,
|
||||
password=hashed_password
|
||||
)
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def authenticate(self, db: Session, username: str, password: str) -> Optional[User]:
|
||||
user = self.get_by_username(db, username=username)
|
||||
if not user:
|
||||
return None
|
||||
if not verify_password(password, user.password):
|
||||
return None
|
||||
return user
|
||||
|
||||
def is_active(self, user: User) -> bool:
|
||||
# For future use if we add user status
|
||||
return True
|
||||
|
||||
|
||||
user = CRUDUser()
|
||||
Reference in New Issue
Block a user