Skip to content

Commit

Permalink
bugfix get_client_by_room_number.py
Browse files Browse the repository at this point in the history
  • Loading branch information
WSL0809 committed May 26, 2024
1 parent 84b90e7 commit 4165e2d
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions api/get_client_by_room_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ClientResp(BaseModel):
class GetClientByRoomResp(BaseModel):
status: Union[str, int]
details: str
clients: ClientResp
clients: List[ClientResp]


def do_get_client_in_room(db: Session, room_number: str):
Expand Down Expand Up @@ -80,21 +80,31 @@ def do_get_client_in_room(db: Session, room_number: str):
GROUP BY client.id, meal_plan.meal_plan_id, recovery_plan.recovery_plan_id, baby_nurse.baby_nurse_id;
""")
client = db.execute(query_sql, {"room_number": room_number}).mappings().all()
if len(client) > 1:
raise HTTPException(status_code=400, detail="Multiple clients found in the same room")
return dict(client[0])
clients = db.execute(query_sql, {"room_number": room_number}).mappings().all()
# if len(client) > 1:
# raise HTTPException(status_code=400, detail="Multiple clients found in the same room")
return [dict(client) for client in clients]


# @router.get("/get_client_in_room", response_model=GetClientByRoomResp)
# async def get_client_in_room(room_number: str, current_user: User = Depends(get_current_active_user),
# db: Session = Depends(get_db)):
# if current_user.role != "admin":
# raise HTTPException(
# status_code=status.HTTP_401_UNAUTHORIZED
# )
# client = do_get_client_in_room(db, room_number)
# return GetClientByRoomResp(status="success", details="Client fetched successfully.",
# clients=ClientResp(**client))
@router.get("/get_client_in_room", response_model=GetClientByRoomResp)
async def get_client_in_room(room_number: str, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED
)
client = do_get_client_in_room(db, room_number)
if not client:
raise HTTPException(status_code=404, detail="Client not found")
return GetClientByRoomResp(status="success", details="Client fetched successfully.",
clients=ClientResp(**client))
clients = do_get_client_in_room(db, room_number)
if clients is None:
return GetClientByRoomResp(status="error", details="Invalid room number.", clients=[])
client_resps = [ClientResp(**client) for client in clients]
return GetClientByRoomResp(status="success", details="Client fetched successfully.", clients=client_resps)

0 comments on commit 4165e2d

Please sign in to comment.