Spaces:
Paused
Paused
Update app/api/products.py
Browse files- app/api/products.py +133 -130
app/api/products.py
CHANGED
|
@@ -1,131 +1,134 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException, status, Depends, Query
|
| 2 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
-
from sqlalchemy import select
|
| 4 |
-
from typing import List, Optional
|
| 5 |
-
from ..core.dependencies import get_current_active_user
|
| 6 |
-
from ..db.database import get_db
|
| 7 |
-
from ..db.models import Product, User
|
| 8 |
-
from ..db.schemas import ProductCreate, ProductInDB
|
| 9 |
-
|
| 10 |
-
router = APIRouter()
|
| 11 |
-
|
| 12 |
-
@router.post("/", response_model=ProductInDB)
|
| 13 |
-
async def create_product(
|
| 14 |
-
product: ProductCreate,
|
| 15 |
-
current_user: User = Depends(get_current_active_user),
|
| 16 |
-
db: AsyncSession = Depends(get_db)
|
| 17 |
-
) -> ProductInDB:
|
| 18 |
-
# Ensure user belongs to the branch they're creating the product for
|
| 19 |
-
if current_user.branch_id != product.branch_id and not current_user.is_superuser:
|
| 20 |
-
raise HTTPException(
|
| 21 |
-
status_code=403,
|
| 22 |
-
detail="You can only create products for your own branch"
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
query = query.where(Product.branch_id ==
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
return {"status": "success", "message": "Product deleted"}
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, status, Depends, Query
|
| 2 |
+
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
+
from sqlalchemy import select
|
| 4 |
+
from typing import List, Optional
|
| 5 |
+
from ..core.dependencies import get_current_active_user
|
| 6 |
+
from ..db.database import get_db
|
| 7 |
+
from ..db.models import Product, User
|
| 8 |
+
from ..db.schemas import ProductCreate, ProductInDB
|
| 9 |
+
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
@router.post("/", response_model=ProductInDB)
|
| 13 |
+
async def create_product(
|
| 14 |
+
product: ProductCreate,
|
| 15 |
+
current_user: User = Depends(get_current_active_user),
|
| 16 |
+
db: AsyncSession = Depends(get_db)
|
| 17 |
+
) -> ProductInDB:
|
| 18 |
+
# Ensure user belongs to the branch they're creating the product for
|
| 19 |
+
if current_user.branch_id != product.branch_id and not current_user.is_superuser:
|
| 20 |
+
raise HTTPException(
|
| 21 |
+
status_code=403,
|
| 22 |
+
detail="You can only create products for your own branch"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Create product with current user as seller
|
| 26 |
+
product_data = product.dict()
|
| 27 |
+
product_data["seller_id"] = current_user.id
|
| 28 |
+
db_product = Product(**product_data)
|
| 29 |
+
db.add(db_product)
|
| 30 |
+
await db.commit()
|
| 31 |
+
await db.refresh(db_product)
|
| 32 |
+
return db_product
|
| 33 |
+
|
| 34 |
+
@router.get("/", response_model=List[ProductInDB])
|
| 35 |
+
async def list_products(
|
| 36 |
+
skip: int = 0,
|
| 37 |
+
limit: int = 10,
|
| 38 |
+
category: Optional[str] = None,
|
| 39 |
+
branch_id: Optional[int] = Query(None, description="Filter products by branch"),
|
| 40 |
+
current_user: User = Depends(get_current_active_user),
|
| 41 |
+
db: AsyncSession = Depends(get_db)
|
| 42 |
+
) -> List[ProductInDB]:
|
| 43 |
+
query = select(Product)
|
| 44 |
+
|
| 45 |
+
# Filter by category if provided
|
| 46 |
+
if category:
|
| 47 |
+
query = query.where(Product.category == category)
|
| 48 |
+
|
| 49 |
+
# Filter by branch if provided, otherwise use user's branch
|
| 50 |
+
if branch_id:
|
| 51 |
+
if not current_user.is_superuser and branch_id != current_user.branch_id:
|
| 52 |
+
raise HTTPException(
|
| 53 |
+
status_code=403,
|
| 54 |
+
detail="You can only view products from your own branch"
|
| 55 |
+
)
|
| 56 |
+
query = query.where(Product.branch_id == branch_id)
|
| 57 |
+
elif not current_user.is_superuser:
|
| 58 |
+
# Non-superusers can only see products from their branch
|
| 59 |
+
query = query.where(Product.branch_id == current_user.branch_id)
|
| 60 |
+
|
| 61 |
+
query = query.offset(skip).limit(limit)
|
| 62 |
+
result = await db.execute(query)
|
| 63 |
+
return result.scalars().all()
|
| 64 |
+
|
| 65 |
+
@router.get("/{product_id}", response_model=ProductInDB)
|
| 66 |
+
async def get_product(
|
| 67 |
+
product_id: int,
|
| 68 |
+
current_user: User = Depends(get_current_active_user),
|
| 69 |
+
db: AsyncSession = Depends(get_db)
|
| 70 |
+
) -> ProductInDB:
|
| 71 |
+
stmt = select(Product).where(Product.id == product_id)
|
| 72 |
+
result = await db.execute(stmt)
|
| 73 |
+
product = result.scalar_one_or_none()
|
| 74 |
+
|
| 75 |
+
if not product:
|
| 76 |
+
raise HTTPException(status_code=404, detail="Product not found")
|
| 77 |
+
|
| 78 |
+
# Check if user has access to this product's branch
|
| 79 |
+
if not current_user.is_superuser and product.branch_id != current_user.branch_id:
|
| 80 |
+
raise HTTPException(status_code=403, detail="You cannot access products from other branches")
|
| 81 |
+
|
| 82 |
+
return product
|
| 83 |
+
|
| 84 |
+
@router.put("/{product_id}", response_model=ProductInDB)
|
| 85 |
+
async def update_product(
|
| 86 |
+
product_id: int,
|
| 87 |
+
product_update: ProductCreate,
|
| 88 |
+
current_user: User = Depends(get_current_active_user),
|
| 89 |
+
db: AsyncSession = Depends(get_db)
|
| 90 |
+
) -> ProductInDB:
|
| 91 |
+
stmt = select(Product).where(Product.id == product_id)
|
| 92 |
+
result = await db.execute(stmt)
|
| 93 |
+
product = result.scalar_one_or_none()
|
| 94 |
+
|
| 95 |
+
if not product:
|
| 96 |
+
raise HTTPException(status_code=404, detail="Product not found")
|
| 97 |
+
|
| 98 |
+
# Check if user has access to this product's branch
|
| 99 |
+
if not current_user.is_superuser and product.branch_id != current_user.branch_id:
|
| 100 |
+
raise HTTPException(status_code=403, detail="You cannot modify products from other branches")
|
| 101 |
+
|
| 102 |
+
# Ensure the branch isn't being changed to a different branch
|
| 103 |
+
if product_update.branch_id != product.branch_id:
|
| 104 |
+
raise HTTPException(status_code=400, detail="Cannot change product's branch")
|
| 105 |
+
|
| 106 |
+
# Update product fields
|
| 107 |
+
update_data = product_update.dict(exclude_unset=True)
|
| 108 |
+
for field, value in update_data.items():
|
| 109 |
+
setattr(product, field, value)
|
| 110 |
+
|
| 111 |
+
await db.commit()
|
| 112 |
+
await db.refresh(product)
|
| 113 |
+
return product
|
| 114 |
+
|
| 115 |
+
@router.delete("/{product_id}")
|
| 116 |
+
async def delete_product(
|
| 117 |
+
product_id: int,
|
| 118 |
+
current_user: User = Depends(get_current_active_user),
|
| 119 |
+
db: AsyncSession = Depends(get_db)
|
| 120 |
+
):
|
| 121 |
+
stmt = select(Product).where(Product.id == product_id)
|
| 122 |
+
result = await db.execute(stmt)
|
| 123 |
+
product = result.scalar_one_or_none()
|
| 124 |
+
|
| 125 |
+
if not product:
|
| 126 |
+
raise HTTPException(status_code=404, detail="Product not found")
|
| 127 |
+
|
| 128 |
+
# Check if user has access to this product's branch
|
| 129 |
+
if not current_user.is_superuser and product.branch_id != current_user.branch_id:
|
| 130 |
+
raise HTTPException(status_code=403, detail="You cannot delete products from other branches")
|
| 131 |
+
|
| 132 |
+
await db.delete(product)
|
| 133 |
+
await db.commit()
|
| 134 |
return {"status": "success", "message": "Product deleted"}
|