todoapp-vgv7cg/app/api/v1/categories.py
Automated Action d993db2f17 Implement subtasks functionality for todo system
- Add parent_id field to Todo model with self-referential foreign key
- Add parent/children relationships and is_subtask property
- Update TodoCreate/TodoUpdate schemas to include parent_id
- Add subtasks list to Todo schema and create SubtaskCreate schema
- Enhance get_todos CRUD function with parent_id filtering
- Add subtask-specific CRUD functions: get_subtasks, create_subtask, move_subtask
- Add API endpoints for subtask management
- Create migration for adding parent_id column
- Update imports and fix circular dependencies
- Ensure proper cycle prevention and validation

Features added:
- GET /todos/{todo_id}/subtasks - Get all subtasks for a todo
- POST /todos/{todo_id}/subtasks - Create a new subtask
- PUT /subtasks/{subtask_id}/move - Move subtask or convert to main todo
- Query parameter parent_id for filtering by parent
- Query parameter include_subtasks for excluding subtasks from main list
2025-06-19 00:04:18 +00:00

88 lines
2.8 KiB
Python

from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.crud import category as category_crud
from app.schemas.category import Category, CategoryCreate, CategoryUpdate
router = APIRouter()
@router.get("/", response_model=List[Category])
def read_categories(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
"""
Retrieve categories.
"""
categories = category_crud.get_categories(db, skip=skip, limit=limit)
return categories
@router.post("/", response_model=Category, status_code=status.HTTP_201_CREATED)
def create_category(category: CategoryCreate, db: Session = Depends(get_db)):
"""
Create new category.
"""
# Check if category with same name already exists
existing_category = category_crud.get_category_by_name(db, name=category.name)
if existing_category:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Category with this name already exists",
)
return category_crud.create_category(db=db, category=category)
@router.get("/{category_id}", response_model=Category)
def read_category(category_id: int, db: Session = Depends(get_db)):
"""
Get category by ID.
"""
db_category = category_crud.get_category(db, category_id=category_id)
if db_category is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Category not found"
)
return db_category
@router.put("/{category_id}", response_model=Category)
def update_category(
category_id: int, category: CategoryUpdate, db: Session = Depends(get_db)
):
"""
Update category.
"""
# Check if category exists
db_category = category_crud.get_category(db, category_id=category_id)
if db_category is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Category not found"
)
# Check if name is being updated and already exists
if category.name and category.name != db_category.name:
existing_category = category_crud.get_category_by_name(db, name=category.name)
if existing_category:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Category with this name already exists",
)
return category_crud.update_category(
db=db, category_id=category_id, category=category
)
@router.delete("/{category_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_category(category_id: int, db: Session = Depends(get_db)):
"""
Delete category.
"""
success = category_crud.delete_category(db=db, category_id=category_id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Category not found"
)