Spaces:
Sleeping
Sleeping
Upload app/api/categories.py
Browse files- app/api/categories.py +45 -0
app/api/categories.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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
|
| 9 |
+
|
| 10 |
+
router = APIRouter(prefix="/categories", tags=["Categories"])
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@router.get("", response_model=AuthResponse)
|
| 14 |
+
async def list_categories(db: Session = Depends(get_db)):
|
| 15 |
+
categories = db.query(Category).order_by(Category.sort_order).all()
|
| 16 |
+
items = [CategoryResponse.model_validate(c).model_dump() for c in categories]
|
| 17 |
+
|
| 18 |
+
return AuthResponse(
|
| 19 |
+
isSuccess=True,
|
| 20 |
+
value={"items": items},
|
| 21 |
+
statusCode=200,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@router.post("", response_model=AuthResponse, status_code=status.HTTP_201_CREATED)
|
| 26 |
+
async def create_category(
|
| 27 |
+
request: CategoryCreate,
|
| 28 |
+
user_id: int = Depends(get_current_user),
|
| 29 |
+
db: Session = Depends(get_db),
|
| 30 |
+
):
|
| 31 |
+
category = Category(
|
| 32 |
+
name_ar=request.name_ar,
|
| 33 |
+
name_en=request.name_en,
|
| 34 |
+
icon=request.icon,
|
| 35 |
+
sort_order=request.sort_order,
|
| 36 |
+
)
|
| 37 |
+
db.add(category)
|
| 38 |
+
db.commit()
|
| 39 |
+
db.refresh(category)
|
| 40 |
+
|
| 41 |
+
return AuthResponse(
|
| 42 |
+
isSuccess=True,
|
| 43 |
+
value={"category_id": category.id},
|
| 44 |
+
statusCode=201,
|
| 45 |
+
)
|