Automated Action 091c42482c Add todo categories and due dates with filtering capabilities
- Added category field to Todo model for organizing todos
- Added due_date field with timezone support for deadline tracking
- Enhanced CRUD operations with filtering by category, completion status, and overdue items
- Added new API endpoints for category-based and overdue todo retrieval
- Updated API documentation with new filtering query parameters
- Created database migration for new fields with proper indexing
- Updated README with comprehensive feature documentation
2025-06-17 05:51:35 +00:00

92 lines
2.6 KiB
Python

from typing import List, Optional
from datetime import datetime
from sqlalchemy.orm import Session
from sqlalchemy import and_, not_
from app.models.todo import Todo
from app.schemas.todo import TodoCreate, TodoUpdate
class CRUDTodo:
"""CRUD operations for Todo model."""
def get(self, db: Session, todo_id: int) -> Optional[Todo]:
"""Get a single todo by ID."""
return db.query(Todo).filter(Todo.id == todo_id).first()
def get_multi(
self,
db: Session,
*,
skip: int = 0,
limit: int = 100,
category: Optional[str] = None,
completed: Optional[bool] = None,
overdue_only: bool = False,
) -> List[Todo]:
"""Get multiple todos with pagination and filtering."""
query = db.query(Todo)
filters = []
if category:
filters.append(Todo.category == category)
if completed is not None:
filters.append(Todo.completed == completed)
if overdue_only:
filters.append(
and_(Todo.due_date < datetime.now(), not_(Todo.completed))
)
if filters:
query = query.filter(and_(*filters))
return query.offset(skip).limit(limit).all()
def create(self, db: Session, *, obj_in: TodoCreate) -> Todo:
"""Create a new todo."""
db_obj = Todo(
title=obj_in.title,
description=obj_in.description,
completed=obj_in.completed,
category=obj_in.category,
due_date=obj_in.due_date,
)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def update(self, db: Session, *, db_obj: Todo, obj_in: TodoUpdate) -> Todo:
"""Update a todo."""
update_data = obj_in.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(db_obj, field, value)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def remove(self, db: Session, *, todo_id: int) -> Optional[Todo]:
"""Delete a todo."""
obj = db.query(Todo).get(todo_id)
if obj:
db.delete(obj)
db.commit()
return obj
def get_by_category(self, db: Session, *, category: str) -> List[Todo]:
"""Get all todos by category."""
return db.query(Todo).filter(Todo.category == category).all()
def get_overdue(self, db: Session) -> List[Todo]:
"""Get all overdue todos."""
return (
db.query(Todo)
.filter(and_(Todo.due_date < datetime.now(), not_(Todo.completed)))
.all()
)
todo = CRUDTodo()