Unknown type supprt
This commit is contained in:
@@ -2,8 +2,8 @@ from typing import List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from app.api.deps import get_db, get_current_active_user
|
||||
from app.models.ppr import Aircraft
|
||||
from app.schemas.ppr import Aircraft as AircraftSchema
|
||||
from app.models.ppr import Aircraft, UserAircraft
|
||||
from app.schemas.ppr import Aircraft as AircraftSchema, UserAircraftCreate
|
||||
from app.models.ppr import User
|
||||
|
||||
router = APIRouter()
|
||||
@@ -18,6 +18,7 @@ async def lookup_aircraft_by_registration(
|
||||
"""
|
||||
Lookup aircraft by registration (clean match).
|
||||
Removes non-alphanumeric characters from input for matching.
|
||||
Checks user_aircraft table first, then aircraft table.
|
||||
"""
|
||||
# Clean the input registration (remove non-alphanumeric characters)
|
||||
clean_input = ''.join(c for c in registration if c.isalnum()).upper()
|
||||
@@ -25,7 +26,29 @@ async def lookup_aircraft_by_registration(
|
||||
if len(clean_input) < 4:
|
||||
return []
|
||||
|
||||
# Query aircraft table using clean_reg column
|
||||
# First check user_aircraft table
|
||||
user_aircraft = db.query(UserAircraft).filter(
|
||||
UserAircraft.clean_reg.like(f"{clean_input}%")
|
||||
).limit(10).all()
|
||||
|
||||
if user_aircraft:
|
||||
# Convert UserAircraft to Aircraft-like objects
|
||||
result = []
|
||||
for ua in user_aircraft:
|
||||
# Create a mock Aircraft object with the user data
|
||||
result.append({
|
||||
'id': ua.id,
|
||||
'registration': ua.registration,
|
||||
'type_code': ua.type_code,
|
||||
'clean_reg': ua.clean_reg,
|
||||
'icao24': None,
|
||||
'manufacturer_icao': None,
|
||||
'manufacturer_name': None,
|
||||
'model': None
|
||||
})
|
||||
return result
|
||||
|
||||
# If no user aircraft found, check main aircraft table
|
||||
aircraft_list = db.query(Aircraft).filter(
|
||||
Aircraft.clean_reg.like(f"{clean_input}%")
|
||||
).limit(10).all()
|
||||
@@ -42,6 +65,7 @@ async def public_lookup_aircraft_by_registration(
|
||||
Public lookup aircraft by registration (clean match).
|
||||
Removes non-alphanumeric characters from input for matching.
|
||||
No authentication required.
|
||||
Checks user_aircraft table first, then aircraft table.
|
||||
"""
|
||||
# Clean the input registration (remove non-alphanumeric characters)
|
||||
clean_input = ''.join(c for c in registration if c.isalnum()).upper()
|
||||
@@ -49,7 +73,28 @@ async def public_lookup_aircraft_by_registration(
|
||||
if len(clean_input) < 4:
|
||||
return []
|
||||
|
||||
# Query aircraft table using clean_reg column
|
||||
# First check user_aircraft table
|
||||
user_aircraft = db.query(UserAircraft).filter(
|
||||
UserAircraft.clean_reg.like(f"{clean_input}%")
|
||||
).limit(10).all()
|
||||
|
||||
if user_aircraft:
|
||||
# Convert UserAircraft to Aircraft-like objects
|
||||
result = []
|
||||
for ua in user_aircraft:
|
||||
result.append({
|
||||
'id': ua.id,
|
||||
'registration': ua.registration,
|
||||
'type_code': ua.type_code,
|
||||
'clean_reg': ua.clean_reg,
|
||||
'icao24': None,
|
||||
'manufacturer_icao': None,
|
||||
'manufacturer_name': None,
|
||||
'model': None
|
||||
})
|
||||
return result
|
||||
|
||||
# If no user aircraft found, check main aircraft table
|
||||
aircraft_list = db.query(Aircraft).filter(
|
||||
Aircraft.clean_reg.like(f"{clean_input}%")
|
||||
).limit(10).all()
|
||||
@@ -81,4 +126,39 @@ async def search_aircraft(
|
||||
(Aircraft.model.like(f"%{q}%"))
|
||||
).limit(limit).all()
|
||||
|
||||
return aircraft_list
|
||||
return aircraft_list
|
||||
|
||||
|
||||
@router.post("/user-aircraft", response_model=dict)
|
||||
async def save_user_aircraft(
|
||||
aircraft: UserAircraftCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""
|
||||
Save a user-defined aircraft type for a registration.
|
||||
"""
|
||||
# Clean the registration
|
||||
clean_reg = ''.join(c for c in aircraft.registration if c.isalnum()).upper()
|
||||
|
||||
# Check if already exists
|
||||
existing = db.query(UserAircraft).filter(
|
||||
UserAircraft.registration == aircraft.registration.upper()
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
raise HTTPException(status_code=400, detail="Aircraft registration already exists in user database")
|
||||
|
||||
# Create new user aircraft
|
||||
user_aircraft = UserAircraft(
|
||||
registration=aircraft.registration.upper(),
|
||||
type_code=aircraft.type_code.upper(),
|
||||
clean_reg=clean_reg,
|
||||
created_by=current_user.username
|
||||
)
|
||||
|
||||
db.add(user_aircraft)
|
||||
db.commit()
|
||||
db.refresh(user_aircraft)
|
||||
|
||||
return {"message": "Aircraft saved successfully", "id": user_aircraft.id}
|
||||
@@ -88,4 +88,16 @@ class Aircraft(Base):
|
||||
model = Column(String(255), nullable=True)
|
||||
clean_reg = Column(String(25), nullable=True, index=True)
|
||||
created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp())
|
||||
updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
|
||||
|
||||
class UserAircraft(Base):
|
||||
__tablename__ = "user_aircraft"
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
registration = Column(String(25), nullable=False, unique=True, index=True)
|
||||
type_code = Column(String(30), nullable=False)
|
||||
clean_reg = Column(String(25), nullable=False, index=True)
|
||||
created_by = Column(String(16), nullable=False, index=True)
|
||||
created_at = Column(DateTime, nullable=False, server_default=func.current_timestamp())
|
||||
updated_at = Column(DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
@@ -214,4 +214,24 @@ class Aircraft(AircraftBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
from_attributes = True
|
||||
|
||||
|
||||
# User Aircraft schemas
|
||||
class UserAircraftBase(BaseModel):
|
||||
registration: str
|
||||
type_code: str
|
||||
clean_reg: str
|
||||
created_by: str
|
||||
|
||||
|
||||
class UserAircraft(UserAircraftBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class UserAircraftCreate(BaseModel):
|
||||
registration: str
|
||||
type_code: str
|
||||
Reference in New Issue
Block a user