117 lines
3.3 KiB
Python
117 lines
3.3 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_superuser, get_current_user
|
|
from app.crud import category
|
|
from app.db.utils import get_db
|
|
from app.models.user import User
|
|
from app.schemas.category import Category, CategoryCreate, CategoryUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Category])
|
|
def read_categories(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve categories.
|
|
"""
|
|
categories = category.get_multi(db, skip=skip, limit=limit)
|
|
return categories
|
|
|
|
|
|
@router.post("/", response_model=Category)
|
|
def create_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_in: CategoryCreate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new category.
|
|
"""
|
|
db_category = category.get_by_name(db, name=category_in.name)
|
|
if db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="The category with this name already exists in the system.",
|
|
)
|
|
return category.create(db, obj_in=category_in)
|
|
|
|
|
|
@router.get("/{id}", response_model=Category)
|
|
def read_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get category by ID.
|
|
"""
|
|
db_category = category.get(db, id=id)
|
|
if not db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
return db_category
|
|
|
|
|
|
@router.put("/{id}", response_model=Category)
|
|
def update_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
category_in: CategoryUpdate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update a category.
|
|
"""
|
|
db_category = category.get(db, id=id)
|
|
if not db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
|
|
# If name is being updated, check that it doesn't conflict with an existing category
|
|
if category_in.name and category_in.name != db_category.name:
|
|
existing_category = category.get_by_name(db, name=category_in.name)
|
|
if existing_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="The category with this name already exists in the system.",
|
|
)
|
|
|
|
return category.update(db, db_obj=db_category, obj_in=category_in)
|
|
|
|
|
|
@router.delete("/{id}", response_model=Category)
|
|
def delete_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete a category.
|
|
"""
|
|
db_category = category.get(db, id=id)
|
|
if not db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
|
|
# Optional: Check if the category has items and handle accordingly
|
|
# For now, we'll allow deletion even if items are associated with it
|
|
|
|
return category.remove(db, id=id) |