Serverside spool find limit and offset
This commit is contained in:
@@ -119,6 +119,16 @@ async def find(
|
||||
),
|
||||
example="filament.name:asc,location:desc",
|
||||
),
|
||||
limit: Optional[int] = Query(
|
||||
default=None,
|
||||
title="Limit",
|
||||
description="Maximum number of items in the response.",
|
||||
),
|
||||
offset: int = Query(
|
||||
default=0,
|
||||
title="Offset",
|
||||
description="Offset in the full result set if a limit is set.",
|
||||
),
|
||||
) -> list[Spool]:
|
||||
sort_by: dict[str, SortOrder] = {}
|
||||
if sort is not None:
|
||||
@@ -137,6 +147,8 @@ async def find(
|
||||
lot_nr=lot_nr,
|
||||
allow_archived=allow_archived,
|
||||
sort_by=sort_by,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
return [Spool.from_db(db_item) for db_item in db_items]
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ def parse_nested_field(base_obj: type[models.Base], field: str) -> sqlalchemy.Co
|
||||
return getattr(base_obj, fields[0])
|
||||
|
||||
|
||||
async def find( # noqa: C901
|
||||
async def find( # noqa: C901, PLR0912
|
||||
*,
|
||||
db: AsyncSession,
|
||||
filament_name: Optional[str] = None,
|
||||
@@ -110,6 +110,8 @@ async def find( # noqa: C901
|
||||
lot_nr: Optional[str] = None,
|
||||
allow_archived: bool = False,
|
||||
sort_by: Optional[dict[str, SortOrder]] = None,
|
||||
limit: Optional[int] = None,
|
||||
offset: int = 0,
|
||||
) -> list[models.Spool]:
|
||||
"""Find a list of spool objects by search criteria.
|
||||
|
||||
@@ -153,6 +155,9 @@ async def find( # noqa: C901
|
||||
elif order == SortOrder.DESC:
|
||||
stmt = stmt.order_by(field.desc())
|
||||
|
||||
if limit is not None:
|
||||
stmt = stmt.offset(offset).limit(limit)
|
||||
|
||||
rows = await db.execute(stmt)
|
||||
return list(rows.scalars().all())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user