fix(backend): include steps in list_routines response
Batch-load all routine steps in a single query and attach them to each routine dict, mirroring the detail endpoint pattern. Fixes "0 steps" shown on the routines list page. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d4e3040674
commit
78c67b6179
1 changed files with 20 additions and 2 deletions
|
|
@ -290,7 +290,7 @@ ZASADY:
|
|||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("", response_model=list[Routine])
|
||||
@router.get("")
|
||||
def list_routines(
|
||||
from_date: Optional[date] = None,
|
||||
to_date: Optional[date] = None,
|
||||
|
|
@ -304,7 +304,25 @@ def list_routines(
|
|||
stmt = stmt.where(Routine.routine_date <= to_date)
|
||||
if part_of_day is not None:
|
||||
stmt = stmt.where(Routine.part_of_day == part_of_day)
|
||||
return session.exec(stmt).all()
|
||||
routines = session.exec(stmt).all()
|
||||
|
||||
routine_ids = [r.id for r in routines]
|
||||
steps_by_routine: dict = {}
|
||||
if routine_ids:
|
||||
all_steps = session.exec(
|
||||
select(RoutineStep).where(RoutineStep.routine_id.in_(routine_ids))
|
||||
).all()
|
||||
for step in all_steps:
|
||||
steps_by_routine.setdefault(step.routine_id, []).append(step)
|
||||
|
||||
result = []
|
||||
for r in routines:
|
||||
data = r.model_dump(mode="json")
|
||||
data["steps"] = [
|
||||
s.model_dump(mode="json") for s in steps_by_routine.get(r.id, [])
|
||||
]
|
||||
result.append(data)
|
||||
return result
|
||||
|
||||
|
||||
@router.post("", response_model=Routine, status_code=201)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue