added pucks and samples
This commit is contained in:
@ -1,20 +1,36 @@
|
||||
import logging
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from app.models import Shipment
|
||||
|
||||
|
||||
def get_shipments(db: Session):
|
||||
from app.models import Shipment
|
||||
return db.query(Shipment).options(
|
||||
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):
|
||||
from app.models import Shipment
|
||||
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
|
@ -14,7 +14,7 @@ class Shipment(Base):
|
||||
comments = Column(String, nullable=True)
|
||||
contact_person_id = Column(Integer, ForeignKey("contact_persons.id"))
|
||||
return_address_id = Column(Integer, ForeignKey("addresses.id"))
|
||||
proposal_id = Column(Integer, ForeignKey("proposals.id"))
|
||||
proposal_id = Column(Integer, ForeignKey('proposals.id'), nullable=True)
|
||||
|
||||
contact_person = relationship("ContactPerson", back_populates="shipments")
|
||||
return_address = relationship("Address", back_populates="shipments")
|
||||
@ -97,6 +97,7 @@ class Puck(Base):
|
||||
positions = relationship("Sample", back_populates="puck")
|
||||
dewar = relationship("Dewar", back_populates="pucks")
|
||||
|
||||
|
||||
class Sample(Base):
|
||||
__tablename__ = 'samples'
|
||||
|
||||
|
@ -11,7 +11,8 @@ router = APIRouter()
|
||||
|
||||
@router.get("/", response_model=List[DewarSchema])
|
||||
async def get_dewars(db: Session = Depends(get_db)):
|
||||
return db.query(DewarModel).all()
|
||||
dewars = db.query(DewarModel).options(joinedload(DewarModel.pucks)).all()
|
||||
return dewars
|
||||
|
||||
|
||||
@router.post("/", response_model=DewarSchema, status_code=status.HTTP_201_CREATED)
|
||||
@ -22,8 +23,6 @@ async def create_dewar(dewar: DewarCreate, db: Session = Depends(get_db)) -> Dew
|
||||
id=dewar_id,
|
||||
dewar_name=dewar.dewar_name,
|
||||
tracking_number=dewar.tracking_number,
|
||||
number_of_pucks=dewar.number_of_pucks,
|
||||
number_of_samples=dewar.number_of_samples,
|
||||
status=dewar.status,
|
||||
ready_date=dewar.ready_date,
|
||||
shipping_date=dewar.shipping_date,
|
||||
@ -37,6 +36,7 @@ async def create_dewar(dewar: DewarCreate, db: Session = Depends(get_db)) -> Dew
|
||||
db.add(db_dewar)
|
||||
db.commit()
|
||||
db.refresh(db_dewar)
|
||||
|
||||
return db_dewar
|
||||
|
||||
|
||||
@ -49,7 +49,12 @@ async def get_dewar(dewar_id: str, db: Session = Depends(get_db)):
|
||||
if not dewar:
|
||||
raise HTTPException(status_code=404, detail="Dewar not found")
|
||||
|
||||
return dewar
|
||||
# Ensure dewar.pucks is an empty list if there are no pucks
|
||||
dewar_dict = dewar.__dict__
|
||||
if dewar_dict.get("pucks") is None:
|
||||
dewar_dict["pucks"] = []
|
||||
|
||||
return DewarSchema.from_orm(dewar)
|
||||
|
||||
|
||||
@router.put("/{dewar_id}", response_model=DewarSchema)
|
||||
@ -60,7 +65,9 @@ async def update_dewar(dewar_id: str, dewar_update: DewarUpdate, db: Session = D
|
||||
raise HTTPException(status_code=404, detail="Dewar not found")
|
||||
|
||||
for key, value in dewar_update.dict(exclude_unset=True).items():
|
||||
setattr(dewar, key, value)
|
||||
# Ensure we're only setting directly settable attributes
|
||||
if hasattr(dewar, key):
|
||||
setattr(dewar, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(dewar)
|
||||
|
@ -4,9 +4,10 @@ from typing import List, Optional
|
||||
import uuid
|
||||
import json
|
||||
from datetime import date
|
||||
import logging
|
||||
|
||||
from app.models import Shipment as ShipmentModel, ContactPerson as ContactPersonModel, Address as AddressModel, Proposal as ProposalModel, Dewar as DewarModel
|
||||
from app.schemas import ShipmentCreate, Shipment as ShipmentSchema, DewarUpdate, ContactPerson as ContactPersonSchema
|
||||
from app.schemas import ShipmentCreate, UpdateShipmentComments, Shipment as ShipmentSchema, DewarUpdate, ContactPerson as ContactPersonSchema
|
||||
from app.schemas import Sample as SampleSchema
|
||||
from app.database import get_db
|
||||
from app.crud import get_shipments, get_shipment_by_id
|
||||
@ -23,9 +24,16 @@ async def fetch_shipments(shipment_id: Optional[str] = Query(None), db: Session
|
||||
if shipment_id:
|
||||
shipment = get_shipment_by_id(db, shipment_id)
|
||||
if not shipment:
|
||||
logging.error(f"Shipment with ID {shipment_id} not found")
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
logging.info(f"Shipment found: {shipment}")
|
||||
return [shipment]
|
||||
return get_shipments(db)
|
||||
|
||||
shipments = get_shipments(db)
|
||||
logging.info(f"Total shipments fetched: {len(shipments)}")
|
||||
for shipment in shipments:
|
||||
logging.info(f"Shipment ID: {shipment.shipment_id}, Shipment Name: {shipment.shipment_name}")
|
||||
return shipments
|
||||
|
||||
@router.post("", response_model=ShipmentSchema, status_code=status.HTTP_201_CREATED)
|
||||
async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db)):
|
||||
@ -80,7 +88,8 @@ async def update_shipment(shipment_id: str, updated_shipment: ShipmentCreate, db
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
# Validate relationships by IDs
|
||||
contact_person = db.query(ContactPersonModel).filter(ContactPersonModel.id == updated_shipment.contact_person_id).first()
|
||||
contact_person = db.query(ContactPersonModel).filter(
|
||||
ContactPersonModel.id == updated_shipment.contact_person_id).first()
|
||||
return_address = db.query(AddressModel).filter(AddressModel.id == updated_shipment.return_address_id).first()
|
||||
if not contact_person:
|
||||
raise HTTPException(status_code=404, detail="Contact person not found")
|
||||
@ -101,17 +110,18 @@ async def update_shipment(shipment_id: str, updated_shipment: ShipmentCreate, db
|
||||
if not dewar:
|
||||
raise HTTPException(status_code=404, detail=f"Dewar with ID {dewar_data.dewar_id} not found")
|
||||
|
||||
# Dynamically update the dewar fields based on provided input
|
||||
update_fields = dewar_data.dict(exclude_unset=True)
|
||||
for key, value in update_fields.items():
|
||||
if key == 'contact_person_id':
|
||||
contact_person = db.query(ContactPersonModel).filter(ContactPersonModel.id == value).first()
|
||||
if not contact_person:
|
||||
raise HTTPException(status_code=404, detail=f"Contact person with ID {value} for Dewar {dewar_data.dewar_id} not found")
|
||||
raise HTTPException(status_code=404,
|
||||
detail=f"Contact person with ID {value} for Dewar {dewar_data.dewar_id} not found")
|
||||
if key == 'return_address_id':
|
||||
address = db.query(AddressModel).filter(AddressModel.id == value).first()
|
||||
if not address:
|
||||
raise HTTPException(status_code=404, detail=f"Address with ID {value} for Dewar {dewar_data.dewar_id} not found")
|
||||
raise HTTPException(status_code=404,
|
||||
detail=f"Address with ID {value} for Dewar {dewar_data.dewar_id} not found")
|
||||
|
||||
for key, value in update_fields.items():
|
||||
if key != 'dewar_id':
|
||||
@ -121,6 +131,7 @@ async def update_shipment(shipment_id: str, updated_shipment: ShipmentCreate, db
|
||||
db.refresh(shipment)
|
||||
return shipment
|
||||
|
||||
|
||||
@router.post("/{shipment_id}/add_dewar", response_model=ShipmentSchema)
|
||||
async def add_dewar_to_shipment(shipment_id: str, dewar_id: str, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.shipment_id == shipment_id).first()
|
||||
@ -179,4 +190,16 @@ def get_samples_in_dewar(shipment_id: str, dewar_id: str, db: Session = Depends(
|
||||
for puck in dewar.pucks:
|
||||
samples.extend(puck.positions)
|
||||
|
||||
return samples
|
||||
return samples
|
||||
|
||||
|
||||
@router.put("/{shipment_id}/comments", response_model=ShipmentSchema)
|
||||
async def update_shipment_comments(shipment_id: str, comments_data: UpdateShipmentComments, db: Session = Depends(get_db)):
|
||||
shipment = db.query(ShipmentModel).filter(ShipmentModel.shipment_id == shipment_id).first()
|
||||
if not shipment:
|
||||
raise HTTPException(status_code=404, detail="Shipment not found")
|
||||
|
||||
shipment.comments = comments_data.comments
|
||||
db.commit()
|
||||
db.refresh(shipment)
|
||||
return shipment
|
@ -107,8 +107,6 @@ class Dewar(DewarBase):
|
||||
class DewarUpdate(BaseModel):
|
||||
dewar_name: Optional[str] = None
|
||||
tracking_number: Optional[str] = None
|
||||
number_of_pucks: Optional[int] = None
|
||||
number_of_samples: Optional[int] = None
|
||||
status: Optional[str] = None
|
||||
ready_date: Optional[date] = None
|
||||
shipping_date: Optional[date] = None
|
||||
@ -156,3 +154,7 @@ class ShipmentCreate(BaseModel):
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class UpdateShipmentComments(BaseModel):
|
||||
comments: str
|
BIN
backend/test.db
BIN
backend/test.db
Binary file not shown.
Reference in New Issue
Block a user