117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import desc
|
|
from datetime import datetime
|
|
from app.models.circuit import Circuit
|
|
from app.schemas.circuit import CircuitCreate, CircuitUpdate
|
|
from app.models.journal import EntityType
|
|
from app.crud.crud_journal import journal
|
|
|
|
|
|
class CRUDCircuit:
|
|
def get(self, db: Session, circuit_id: int) -> Optional[Circuit]:
|
|
return db.query(Circuit).filter(Circuit.id == circuit_id).first()
|
|
|
|
def get_by_local_flight(self, db: Session, local_flight_id: int) -> List[Circuit]:
|
|
"""Get all circuits for a specific local flight"""
|
|
return db.query(Circuit).filter(
|
|
Circuit.local_flight_id == local_flight_id
|
|
).order_by(Circuit.circuit_timestamp).all()
|
|
|
|
def get_by_arrival(self, db: Session, arrival_id: int) -> List[Circuit]:
|
|
"""Get all circuits for a specific arrival"""
|
|
return db.query(Circuit).filter(
|
|
Circuit.arrival_id == arrival_id
|
|
).order_by(Circuit.circuit_timestamp).all()
|
|
|
|
def get_multi(
|
|
self,
|
|
db: Session,
|
|
skip: int = 0,
|
|
limit: int = 100
|
|
) -> List[Circuit]:
|
|
return db.query(Circuit).order_by(desc(Circuit.created_at)).offset(skip).limit(limit).all()
|
|
|
|
def create(self, db: Session, obj_in: CircuitCreate, user: str = "system", user_ip: Optional[str] = None) -> Circuit:
|
|
db_obj = Circuit(
|
|
local_flight_id=obj_in.local_flight_id,
|
|
arrival_id=obj_in.arrival_id,
|
|
circuit_timestamp=obj_in.circuit_timestamp
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
|
|
# Log circuit creation in journal
|
|
# Use LOCAL_FLIGHT entity type if local_flight_id exists, otherwise ARRIVAL
|
|
entity_type = EntityType.LOCAL_FLIGHT if obj_in.local_flight_id else EntityType.ARRIVAL
|
|
entity_id = obj_in.local_flight_id if obj_in.local_flight_id else obj_in.arrival_id
|
|
|
|
journal.log_change(
|
|
db,
|
|
entity_type,
|
|
entity_id,
|
|
f"Circuit recorded at {obj_in.circuit_timestamp.isoformat()}",
|
|
user,
|
|
user_ip
|
|
)
|
|
|
|
return db_obj
|
|
|
|
def update(self, db: Session, db_obj: Circuit, obj_in: CircuitUpdate, user: str = "system", user_ip: Optional[str] = None) -> Circuit:
|
|
obj_data = obj_in.dict(exclude_unset=True)
|
|
changes = []
|
|
|
|
for field, value in obj_data.items():
|
|
old_value = getattr(db_obj, field)
|
|
if old_value != value:
|
|
changes.append(f"{field} changed from '{old_value}' to '{value}'")
|
|
setattr(db_obj, field, value)
|
|
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
|
|
# Log changes in journal if any were made
|
|
if changes:
|
|
entity_type = EntityType.LOCAL_FLIGHT if db_obj.local_flight_id else EntityType.ARRIVAL
|
|
entity_id = db_obj.local_flight_id if db_obj.local_flight_id else db_obj.arrival_id
|
|
|
|
for change in changes:
|
|
journal.log_change(
|
|
db,
|
|
entity_type,
|
|
entity_id,
|
|
f"Circuit: {change}",
|
|
user,
|
|
user_ip
|
|
)
|
|
|
|
return db_obj
|
|
|
|
def delete(self, db: Session, circuit_id: int, user: str = "system", user_ip: Optional[str] = None) -> bool:
|
|
circuit = self.get(db, circuit_id)
|
|
if circuit:
|
|
# Determine which entity this circuit belongs to
|
|
entity_type = EntityType.LOCAL_FLIGHT if circuit.local_flight_id else EntityType.ARRIVAL
|
|
entity_id = circuit.local_flight_id if circuit.local_flight_id else circuit.arrival_id
|
|
|
|
db.delete(circuit)
|
|
db.commit()
|
|
|
|
# Log deletion in journal
|
|
journal.log_change(
|
|
db,
|
|
entity_type,
|
|
entity_id,
|
|
f"Circuit deleted (recorded at {circuit.circuit_timestamp.isoformat()})",
|
|
user,
|
|
user_ip
|
|
)
|
|
|
|
return True
|
|
return False
|
|
|
|
|
|
crud_circuit = CRUDCircuit()
|