aaredb/backend/app/crud.py
2024-11-04 16:20:53 +01:00

36 lines
1.4 KiB
Python

import logging
from sqlalchemy.orm import Session, joinedload
from app.models import Shipment
def get_shipments(db: Session):
logging.info("Fetching all shipments from the database.")
shipments = db.query(Shipment).options(
joinedload(Shipment.contact_person),
joinedload(Shipment.return_address),
joinedload(Shipment.proposal),
joinedload(Shipment.dewars)
).all()
logging.info(f"Total of {len(shipments)} shipments fetched.")
for shipment in shipments:
if shipment.proposal_id is None:
logging.warning(f"Shipment {shipment.shipment_id} is missing proposal ID.")
logging.debug(f"Shipment ID: {shipment.shipment_id}, Shipment Name: {shipment.shipment_name}")
return shipments
def get_shipment_by_id(db: Session, shipment_id: str):
logging.info(f"Fetching shipment with ID: {shipment_id}")
shipment = db.query(Shipment).options(
joinedload(Shipment.contact_person),
joinedload(Shipment.return_address),
joinedload(Shipment.proposal),
joinedload(Shipment.dewars)
).filter(Shipment.shipment_id == shipment_id).first()
if shipment:
if shipment.proposal_id is None:
logging.warning(f"Shipment {shipment.shipment_id} is missing proposal ID.")
logging.info(f"Shipment found: {shipment}")
else:
logging.warning(f"Shipment with ID {shipment_id} not found.")
return shipment