Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions backend/app/api/routes/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@

@router.get("/", response_model=ItemsPublic)
def read_items(
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
session: SessionDep,
current_user: CurrentUser,
skip: int = 0,
limit: int = 100,
title: str | None = None,
keyword: str | None = None,
owner_id: uuid.UUID | None = None,
) -> Any:
"""
Retrieve items.
"""
statement = select(Item)

if current_user.is_superuser:
count_statement = select(func.count()).select_from(Item)
count = session.exec(count_statement).one()
statement = select(Item).offset(skip).limit(limit)
items = session.exec(statement).all()
else:
count_statement = (
select(func.count())
.select_from(Item)
.where(Item.owner_id == current_user.id)
)
count = session.exec(count_statement).one()
statement = (
select(Item)
.where(Item.owner_id == current_user.id)
.offset(skip)
.limit(limit)
if not current_user.is_superuser:
statement = statement.where(Item.owner_id == current_user.id)

if current_user.is_superuser and owner_id:
statement = statement.where(Item.owner_id == owner_id)

if title:
statement = statement.where(Item.title.ilike(f"%{title}%"))

if keyword:
statement = statement.where(
Item.title.ilike(f"%{keyword}%") |
Item.description.ilike(f"%{keyword}%")
)
items = session.exec(statement).all()

count_statement = select(func.count()).select_from(statement.subquery())
count = session.exec(count_statement).one()

items = session.exec(statement.offset(skip).limit(limit)).all()

return ItemsPublic(data=items, count=count)

Expand Down
Loading