Skip to content

Commit a286d7a

Browse files
committed
Camnbio de metodo para contar items en la API
1 parent 45135bb commit a286d7a

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

backend/app/api/routes/items.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ def read_items(
4141
return ItemsPublic(data=items, count=count)
4242

4343

44+
@router.get("/count", response_model=int)
45+
def get_item_count(session: SessionDep, current_user: CurrentUser) -> int:
46+
"""
47+
Devuelve el número de ítems en la base de datos.
48+
"""
49+
if current_user.is_superuser:
50+
count_statement = select(func.count()).select_from(Item)
51+
else:
52+
count_statement = (
53+
select(func.count())
54+
.select_from(Item)
55+
.where(Item.owner_id == current_user.id)
56+
)
57+
item_count = session.exec(count_statement).one()
58+
return item_count
59+
60+
61+
4462
@router.get("/{id}", response_model=ItemPublic)
4563
def read_item(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
4664
"""
@@ -108,21 +126,3 @@ def delete_item(
108126
session.commit()
109127
return Message(message="Item deleted successfully")
110128

111-
112-
113-
@router.get("/count", response_model=int)
114-
def count_items(session: SessionDep, current_user: CurrentUser) -> int:
115-
"""
116-
Get the total count of items.
117-
"""
118-
if current_user.is_superuser:
119-
count_statement = select(func.count()).select_from(Item)
120-
else:
121-
count_statement = (
122-
select(func.count())
123-
.select_from(Item)
124-
.where(Item.owner_id == current_user.id)
125-
)
126-
127-
count = session.exec(count_statement).one()
128-
return count

0 commit comments

Comments
 (0)