Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agregué nuevo método para contar items #1391

Closed
wants to merge 3 commits into from
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
19 changes: 19 additions & 0 deletions backend/app/api/routes/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ def read_items(
return ItemsPublic(data=items, count=count)


@router.get("/count", response_model=int)
def get_item_count(session: SessionDep, current_user: CurrentUser) -> int:
"""
Devuelve el número de ítems en la base de datos.
"""
if current_user.is_superuser:
count_statement = select(func.count()).select_from(Item)
else:
count_statement = (
select(func.count())
.select_from(Item)
.where(Item.owner_id == current_user.id)
)
item_count = session.exec(count_statement).one()
return item_count



@router.get("/{id}", response_model=ItemPublic)
def read_item(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
"""
Expand Down Expand Up @@ -107,3 +125,4 @@ def delete_item(
session.delete(item)
session.commit()
return Message(message="Item deleted successfully")

Loading