Automated Action 7cef391c68 Fix database migration error by adding missing dependency
- Added pydantic-settings>=2.0.0 to requirements.txt to resolve ModuleNotFoundError
- Simplified migrations/env.py imports to avoid config dependency issues
- Removed unnecessary settings import from migration environment
- Migration now uses database URL directly from alembic.ini configuration
2025-06-17 05:55:17 +00:00

90 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()