List and edit user aircraft
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from app.api.deps import get_db, get_current_active_user
|
||||
from app.models.ppr import Aircraft, UserAircraft
|
||||
from app.schemas.ppr import Aircraft as AircraftSchema, UserAircraftCreate
|
||||
from app.schemas.ppr import Aircraft as AircraftSchema, UserAircraftCreate, UserAircraft as UserAircraftSchema
|
||||
from app.models.ppr import User
|
||||
|
||||
router = APIRouter()
|
||||
@@ -161,4 +162,66 @@ async def save_user_aircraft(
|
||||
db.commit()
|
||||
db.refresh(user_aircraft)
|
||||
|
||||
return {"message": "Aircraft saved successfully", "id": user_aircraft.id}
|
||||
return {"message": "Aircraft saved successfully", "id": user_aircraft.id}
|
||||
|
||||
|
||||
@router.get("/user-aircraft", response_model=List[UserAircraftSchema])
|
||||
async def get_user_aircraft(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""
|
||||
Get all user-defined aircraft types.
|
||||
"""
|
||||
user_aircraft = db.query(UserAircraft).order_by(UserAircraft.created_at.desc()).all()
|
||||
return user_aircraft
|
||||
|
||||
|
||||
@router.put("/user-aircraft/{registration}", response_model=dict)
|
||||
async def update_user_aircraft(
|
||||
registration: str,
|
||||
aircraft: UserAircraftCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""
|
||||
Update a user-defined aircraft type.
|
||||
"""
|
||||
# Find the existing user aircraft
|
||||
existing = db.query(UserAircraft).filter(
|
||||
UserAircraft.registration == registration.upper()
|
||||
).first()
|
||||
|
||||
if not existing:
|
||||
raise HTTPException(status_code=404, detail="User aircraft not found")
|
||||
|
||||
# Update the type
|
||||
existing.type_code = aircraft.type_code.upper()
|
||||
existing.updated_at = func.current_timestamp()
|
||||
|
||||
db.commit()
|
||||
|
||||
return {"message": "Aircraft updated successfully"}
|
||||
|
||||
|
||||
@router.delete("/user-aircraft/{registration}", response_model=dict)
|
||||
async def delete_user_aircraft(
|
||||
registration: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""
|
||||
Delete a user-defined aircraft type.
|
||||
"""
|
||||
# Find the existing user aircraft
|
||||
existing = db.query(UserAircraft).filter(
|
||||
UserAircraft.registration == registration.upper()
|
||||
).first()
|
||||
|
||||
if not existing:
|
||||
raise HTTPException(status_code=404, detail="User aircraft not found")
|
||||
|
||||
db.delete(existing)
|
||||
db.commit()
|
||||
|
||||
return {"message": "Aircraft deleted successfully"}
|
||||
Reference in New Issue
Block a user