111 lines
3.0 KiB
Python
111 lines
3.0 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models, schemas
|
|
from app.api import deps
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[schemas.Category])
|
|
def read_categories(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Retrieve categories.
|
|
"""
|
|
categories = crud.category.get_multi(db, skip=skip, limit=limit)
|
|
return categories
|
|
|
|
|
|
@router.post("/", response_model=schemas.Category)
|
|
def create_category(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
category_in: schemas.CategoryCreate,
|
|
current_user: models.User = Depends(deps.get_current_admin_user),
|
|
) -> Any:
|
|
"""
|
|
Create new category (admin only).
|
|
"""
|
|
category = crud.category.get_by_name(db, name=category_in.name)
|
|
if category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Category with this name already exists",
|
|
)
|
|
category = crud.category.create(db, obj_in=category_in)
|
|
return category
|
|
|
|
|
|
@router.get("/{id}", response_model=schemas.Category)
|
|
def read_category(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get category by ID.
|
|
"""
|
|
category = crud.category.get(db, id=id)
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
return category
|
|
|
|
|
|
@router.put("/{id}", response_model=schemas.Category)
|
|
def update_category(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
category_in: schemas.CategoryUpdate,
|
|
current_user: models.User = Depends(deps.get_current_admin_user),
|
|
) -> Any:
|
|
"""
|
|
Update a category (admin only).
|
|
"""
|
|
category = crud.category.get(db, id=id)
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
|
|
# Check if name already exists (if updating name)
|
|
if category_in.name and category_in.name != category.name:
|
|
existing_category = crud.category.get_by_name(db, name=category_in.name)
|
|
if existing_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Category with this name already exists",
|
|
)
|
|
|
|
category = crud.category.update(db, db_obj=category, obj_in=category_in)
|
|
return category
|
|
|
|
|
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_category(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
id: int,
|
|
current_user: models.User = Depends(deps.get_current_admin_user),
|
|
) -> None:
|
|
"""
|
|
Delete a category (admin only).
|
|
"""
|
|
category = crud.category.get(db, id=id)
|
|
if not category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found",
|
|
)
|
|
crud.category.remove(db, id=id)
|