119 lines
3.2 KiB
Python
119 lines
3.2 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_active_admin
|
|
from app.db.session import get_db
|
|
from app.models.product import Category
|
|
from app.models.user import User
|
|
from app.schemas.product import Category as CategorySchema
|
|
from app.schemas.product import CategoryCreate, CategoryUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[CategorySchema])
|
|
def read_categories(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve categories.
|
|
"""
|
|
categories = db.query(Category).offset(skip).limit(limit).all()
|
|
return categories
|
|
|
|
|
|
@router.post("/", response_model=CategorySchema)
|
|
def create_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_in: CategoryCreate,
|
|
current_user: User = Depends(get_current_active_admin),
|
|
) -> Any:
|
|
"""
|
|
Create new category. Only admin users can create categories.
|
|
"""
|
|
# Check if category with the same name already exists
|
|
category = db.query(Category).filter(Category.name == category_in.name).first()
|
|
if category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Category with this name already exists",
|
|
)
|
|
|
|
category = Category(**category_in.dict())
|
|
db.add(category)
|
|
db.commit()
|
|
db.refresh(category)
|
|
return category
|
|
|
|
|
|
@router.get("/{category_id}", response_model=CategorySchema)
|
|
def read_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_id: str,
|
|
) -> Any:
|
|
"""
|
|
Get category by ID.
|
|
"""
|
|
category = db.query(Category).filter(Category.id == category_id).first()
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
return category
|
|
|
|
|
|
@router.put("/{category_id}", response_model=CategorySchema)
|
|
def update_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_id: str,
|
|
category_in: CategoryUpdate,
|
|
current_user: User = Depends(get_current_active_admin),
|
|
) -> Any:
|
|
"""
|
|
Update a category. Only admin users can update categories.
|
|
"""
|
|
category = db.query(Category).filter(Category.id == category_id).first()
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
|
|
update_data = category_in.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(category, field, value)
|
|
|
|
db.add(category)
|
|
db.commit()
|
|
db.refresh(category)
|
|
return category
|
|
|
|
|
|
@router.delete("/{category_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_id: str,
|
|
current_user: User = Depends(get_current_active_admin),
|
|
) -> Any:
|
|
"""
|
|
Delete a category. Only admin users can delete categories.
|
|
"""
|
|
category = db.query(Category).filter(Category.id == category_id).first()
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
|
|
db.delete(category)
|
|
db.commit()
|
|
return None |