Spaces:
Sleeping
Sleeping
Upload app/api/categories.py
Browse files- app/api/categories.py +37 -3
app/api/categories.py
CHANGED
|
@@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
|
| 4 |
from app.db.base import get_db
|
| 5 |
-
from app.models.product import Category
|
| 6 |
from app.schemas.product import CategoryCreate, CategoryResponse
|
| 7 |
from app.schemas.auth import AuthResponse
|
| 8 |
from app.core.security import get_current_user
|
|
@@ -12,8 +12,42 @@ router = APIRouter(prefix="/categories", tags=["Categories"])
|
|
| 12 |
|
| 13 |
@router.get("", response_model=AuthResponse)
|
| 14 |
async def list_categories(db: Session = Depends(get_db)):
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
return AuthResponse(
|
| 19 |
isSuccess=True,
|
|
|
|
| 2 |
from sqlalchemy.orm import Session
|
| 3 |
|
| 4 |
from app.db.base import get_db
|
| 5 |
+
from app.models.product import Category, Product
|
| 6 |
from app.schemas.product import CategoryCreate, CategoryResponse
|
| 7 |
from app.schemas.auth import AuthResponse
|
| 8 |
from app.core.security import get_current_user
|
|
|
|
| 12 |
|
| 13 |
@router.get("", response_model=AuthResponse)
|
| 14 |
async def list_categories(db: Session = Depends(get_db)):
|
| 15 |
+
# 1. Fetch all categories
|
| 16 |
+
all_categories = db.query(Category).all()
|
| 17 |
+
|
| 18 |
+
# 2. Identify category IDs that have at least one active product
|
| 19 |
+
# We use a set for O(1) lookups
|
| 20 |
+
active_product_cat_ids = {
|
| 21 |
+
row[0] for row in db.query(Product.category_id)
|
| 22 |
+
.filter(Product.is_active == True)
|
| 23 |
+
.distinct()
|
| 24 |
+
.all()
|
| 25 |
+
if row[0] is not None
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# 3. Determine visibility: a category is visible if it has products
|
| 29 |
+
# OR if any of its descendants have products.
|
| 30 |
+
visible_ids = set()
|
| 31 |
+
|
| 32 |
+
# Helper to mark a category and all its ancestors as visible
|
| 33 |
+
def mark_visible(cat_id: int):
|
| 34 |
+
if cat_id in visible_ids:
|
| 35 |
+
return
|
| 36 |
+
visible_ids.add(cat_id)
|
| 37 |
+
# Find the category object to find its parent
|
| 38 |
+
cat = next((c for c in all_categories if c.id == cat_id), None)
|
| 39 |
+
if cat and cat.parent_id:
|
| 40 |
+
mark_visible(cat.parent_id)
|
| 41 |
+
|
| 42 |
+
for cat_id in active_product_cat_ids:
|
| 43 |
+
mark_visible(cat_id)
|
| 44 |
+
|
| 45 |
+
# 4. Filter and Sort
|
| 46 |
+
filtered_categories = [c for c in all_categories if c.id in visible_ids]
|
| 47 |
+
# Maintain the intended sort order
|
| 48 |
+
filtered_categories.sort(key=lambda x: x.sort_order)
|
| 49 |
+
|
| 50 |
+
items = [CategoryResponse.model_validate(c).model_dump() for c in filtered_categories]
|
| 51 |
|
| 52 |
return AuthResponse(
|
| 53 |
isSuccess=True,
|