
- 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
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.category import Category
|
|
from app.schemas.category import CategoryCreate, CategoryUpdate
|
|
|
|
|
|
def get_category(db: Session, category_id: int) -> Optional[Category]:
|
|
"""Get a single category by ID."""
|
|
return db.query(Category).filter(Category.id == category_id).first()
|
|
|
|
|
|
def get_category_by_name(db: Session, name: str) -> Optional[Category]:
|
|
"""Get a single category by name."""
|
|
return db.query(Category).filter(Category.name == name).first()
|
|
|
|
|
|
def get_categories(db: Session, skip: int = 0, limit: int = 100) -> List[Category]:
|
|
"""Get all categories with pagination."""
|
|
return db.query(Category).offset(skip).limit(limit).all()
|
|
|
|
|
|
def create_category(db: Session, category: CategoryCreate) -> Category:
|
|
"""Create a new category."""
|
|
db_category = Category(
|
|
name=category.name,
|
|
description=category.description,
|
|
color=category.color,
|
|
)
|
|
db.add(db_category)
|
|
db.commit()
|
|
db.refresh(db_category)
|
|
return db_category
|
|
|
|
|
|
def update_category(
|
|
db: Session, category_id: int, category: CategoryUpdate
|
|
) -> Optional[Category]:
|
|
"""Update an existing category."""
|
|
db_category = get_category(db, category_id)
|
|
if db_category:
|
|
update_data = category.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_category, field, value)
|
|
db.commit()
|
|
db.refresh(db_category)
|
|
return db_category
|
|
|
|
|
|
def delete_category(db: Session, category_id: int) -> bool:
|
|
"""Delete a category."""
|
|
db_category = get_category(db, category_id)
|
|
if db_category:
|
|
db.delete(db_category)
|
|
db.commit()
|
|
return True
|
|
return False
|