From 530c9efae02584ffeddcbdf00c1f9d9f9d2875cf Mon Sep 17 00:00:00 2001 From: GotthardG <51994228+GotthardG@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:10:14 +0100 Subject: [PATCH] retrieve pucks that are in the tell dewar --- backend/app/routers/logistics.py | 4 --- backend/app/routers/puck.py | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/backend/app/routers/logistics.py b/backend/app/routers/logistics.py index d693168..2ebcdb2 100644 --- a/backend/app/routers/logistics.py +++ b/backend/app/routers/logistics.py @@ -132,10 +132,6 @@ async def scan_dewar(event_data: LogisticsEventCreate, db: Session = Depends(get return {"message": "Status updated successfully"} - - - - @router.get("/slots", response_model=List[SlotSchema]) async def get_all_slots(db: Session = Depends(get_db)): slots = db.query(SlotModel).options(joinedload(SlotModel.dewar)).all() diff --git a/backend/app/routers/puck.py b/backend/app/routers/puck.py index bc02d8f..e931137 100644 --- a/backend/app/routers/puck.py +++ b/backend/app/routers/puck.py @@ -18,6 +18,55 @@ async def get_pucks(db: Session = Depends(get_db)): return db.query(PuckModel).all() +@router.get("/with-tell-position", response_model=List[dict]) +async def get_pucks_with_tell_position(db: Session = Depends(get_db)): + """ + Retrieve all pucks with a `tell_position` set (not null) and their associated samples. + """ + # Query all pucks that have an event with a non-null tell_position + pucks = ( + db.query(PuckModel) + .join(PuckEventModel, PuckModel.id == PuckEventModel.puck_id) + .filter(PuckEventModel.tell_position.isnot(None)) + .all() + ) + logger.info(f"Pucks with tell position: {pucks}") + + if not pucks: + logger.info("No pucks with tell_position found.") # Log for debugging + raise HTTPException( + status_code=404, + detail="No pucks with a `tell_position` found." + ) + + result = [] + + for puck in pucks: + # Get associated samples for the puck + samples = db.query(SampleModel).filter(SampleModel.puck_id == puck.id).all() + sample_data = [ + { + "id": sample.id, + "sample_name": sample.sample_name, + "position": sample.position, # Updated field based on schema + } + for sample in samples + ] + + # Add puck and sample info to the result + result.append( + { + "id": puck.id, + "puck_name": puck.puck_name, + "puck_type": puck.puck_type, + "puck_location_in_dewar": puck.puck_location_in_dewar, + "dewar_id": puck.dewar_id, + "samples": sample_data, # Add associated samples + } + ) + + return result + @router.get("/{puck_id}", response_model=PuckSchema) async def get_puck(puck_id: str, db: Session = Depends(get_db)): puck = db.query(PuckModel).filter(PuckModel.id == puck_id).first()