Skip to content

Commit b2b0472

Browse files
Added category
1 parent ac60cae commit b2b0472

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Add category to items
2+
3+
Revision ID: add_category_to_items
4+
Revises: d98dd8ec85a3
5+
Create Date: 2023-11-30 10:00:00.000000
6+
7+
"""
8+
import sqlalchemy as sa
9+
import sqlmodel.sql.sqltypes
10+
from alembic import op
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "add_category_to_items"
14+
down_revision = "d98dd8ec85a3"
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column(
22+
"item",
23+
sa.Column("category", sqlmodel.sql.sqltypes.AutoString(length=100), nullable=True)
24+
)
25+
# ### end Alembic commands ###
26+
27+
28+
def downgrade():
29+
# ### commands auto generated by Alembic - please adjust! ###
30+
op.drop_column("item", "category")
31+
# ### end Alembic commands ###

backend/app/api/routes/items.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,58 @@
1212

1313
@router.get("/", response_model=ItemsPublic)
1414
def read_items(
15-
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
15+
session: SessionDep,
16+
current_user: CurrentUser,
17+
skip: int = 0,
18+
limit: int = 100,
19+
category: str | None = None
1620
) -> Any:
1721
"""
1822
Retrieve items.
1923
"""
20-
24+
25+
# Base query
2126
if current_user.is_superuser:
22-
count_statement = select(func.count()).select_from(Item)
23-
count = session.exec(count_statement).one()
24-
statement = select(Item).offset(skip).limit(limit)
25-
items = session.exec(statement).all()
27+
query = select(Item)
28+
count_query = select(func.count()).select_from(Item)
2629
else:
27-
count_statement = (
28-
select(func.count())
29-
.select_from(Item)
30-
.where(Item.owner_id == current_user.id)
31-
)
32-
count = session.exec(count_statement).one()
33-
statement = (
34-
select(Item)
35-
.where(Item.owner_id == current_user.id)
36-
.offset(skip)
37-
.limit(limit)
38-
)
39-
items = session.exec(statement).all()
30+
query = select(Item).where(Item.owner_id == current_user.id)
31+
count_query = select(func.count()).select_from(Item).where(Item.owner_id == current_user.id)
32+
33+
# Apply category filter if provided
34+
if category:
35+
query = query.where(Item.category == category)
36+
count_query = count_query.where(Item.category == category)
37+
38+
# Apply pagination
39+
query = query.offset(skip).limit(limit)
40+
41+
# Execute queries
42+
count = session.exec(count_query).one()
43+
items = session.exec(query).all()
4044

4145
return ItemsPublic(data=items, count=count)
4246

4347

48+
@router.get("/categories", response_model=list[str])
49+
def get_item_categories(
50+
session: SessionDep, current_user: CurrentUser
51+
) -> Any:
52+
"""
53+
Get all unique item categories.
54+
"""
55+
if current_user.is_superuser:
56+
statement = select(Item.category).distinct()
57+
else:
58+
statement = select(Item.category).where(
59+
Item.owner_id == current_user.id
60+
).distinct()
61+
62+
categories = session.exec(statement).all()
63+
# Filter out None values and return unique non-empty categories
64+
return [cat for cat in categories if cat]
65+
66+
4467
@router.get("/{id}", response_model=ItemPublic)
4568
def read_item(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
4669
"""

backend/app/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class UsersPublic(SQLModel):
6060
class ItemBase(SQLModel):
6161
title: str = Field(min_length=1, max_length=255)
6262
description: str | None = Field(default=None, max_length=255)
63+
category: str | None = Field(default=None, max_length=100)
6364

6465

6566
# Properties to receive on item creation

0 commit comments

Comments
 (0)