Automated Action d60767d0ba Add categories and tags features
- Create Category and Tag models
- Create TodoTag association table
- Add category_id to Todo model
- Create Alembic migration for new tables
- Create schemas for Category and Tag
- Update Todo schemas to include Category and Tags
- Create CRUD operations for Categories and Tags
- Update Todo CRUD operations to handle categories and tags
- Create API endpoints for categories and tags
- Update Todo API endpoints with category and tag filtering
- Update documentation
2025-06-17 02:52:26 +00:00

111 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_user, get_db
from app.crud.crud_category import (
create_category,
delete_category,
get_categories,
get_category,
get_category_by_name,
update_category,
)
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_active_user),
) -> Any:
"""
Retrieve categories.
"""
categories = get_categories(db=db, owner_id=current_user.id, skip=skip, limit=limit)
return categories
@router.post("/", response_model=Category)
def create_category_item(
*,
db: Session = Depends(get_db),
category_in: CategoryCreate,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Create new category.
"""
category = get_category_by_name(
db=db, name=category_in.name, owner_id=current_user.id
)
if category:
raise HTTPException(
status_code=400,
detail="The category with this name already exists",
)
category = create_category(db=db, category_in=category_in, owner_id=current_user.id)
return category
@router.get("/{id}", response_model=Category)
def read_category(
*,
db: Session = Depends(get_db),
id: int,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Get category by ID.
"""
category = get_category(db=db, category_id=id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
if category.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
return category
@router.put("/{id}", response_model=Category)
def update_category_item(
*,
db: Session = Depends(get_db),
id: int,
category_in: CategoryUpdate,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Update a category.
"""
category = get_category(db=db, category_id=id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
if category.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
category = update_category(db=db, db_obj=category, obj_in=category_in)
return category
@router.delete("/{id}", response_model=None, status_code=status.HTTP_204_NO_CONTENT)
def delete_category_item(
*,
db: Session = Depends(get_db),
id: int,
current_user: User = Depends(get_current_active_user),
) -> Any:
"""
Delete a category.
"""
category = get_category(db=db, category_id=id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
if category.owner_id != current_user.id:
raise HTTPException(status_code=400, detail="Not enough permissions")
delete_category(db=db, category_id=id)
return None