
- Add priority levels (low, medium, high) to Todo model - Add due date field to Todo model - Create Alembic migration for new fields - Update Todo schemas to include new fields - Enhance CRUD operations with priority and due date filtering - Update API endpoints to support new fields - Implement smart ordering by priority and due date - Update documentation to reflect changes
79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
from datetime import datetime
|
|
from typing import List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.todo import PriorityLevel, Todo
|
|
from app.schemas.todo import TodoCreate, TodoUpdate
|
|
|
|
|
|
def get_todo(db: Session, todo_id: int) -> Optional[Todo]:
|
|
return db.query(Todo).filter(Todo.id == todo_id).first()
|
|
|
|
|
|
def get_todos(
|
|
db: Session,
|
|
owner_id: int,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
title: Optional[str] = None,
|
|
is_completed: Optional[bool] = None,
|
|
priority: Optional[PriorityLevel] = None,
|
|
due_date_before: Optional[datetime] = None,
|
|
due_date_after: Optional[datetime] = None
|
|
) -> List[Todo]:
|
|
query = db.query(Todo).filter(Todo.owner_id == owner_id)
|
|
|
|
if title:
|
|
query = query.filter(Todo.title.contains(title))
|
|
|
|
if is_completed is not None:
|
|
query = query.filter(Todo.is_completed == is_completed)
|
|
|
|
if priority is not None:
|
|
query = query.filter(Todo.priority == priority)
|
|
|
|
if due_date_before is not None:
|
|
query = query.filter(Todo.due_date <= due_date_before)
|
|
|
|
if due_date_after is not None:
|
|
query = query.filter(Todo.due_date >= due_date_after)
|
|
|
|
# Order by priority (highest first) and due date (earliest first)
|
|
query = query.order_by(Todo.priority.desc(), Todo.due_date.asc())
|
|
|
|
return query.offset(skip).limit(limit).all()
|
|
|
|
|
|
def create_todo(db: Session, todo_in: TodoCreate, owner_id: int) -> Todo:
|
|
db_todo = Todo(
|
|
title=todo_in.title,
|
|
description=todo_in.description,
|
|
is_completed=todo_in.is_completed,
|
|
priority=todo_in.priority,
|
|
due_date=todo_in.due_date,
|
|
owner_id=owner_id
|
|
)
|
|
db.add(db_todo)
|
|
db.commit()
|
|
db.refresh(db_todo)
|
|
return db_todo
|
|
|
|
|
|
def update_todo(
|
|
db: Session, db_obj: Todo, obj_in: TodoUpdate
|
|
) -> Todo:
|
|
update_data = obj_in.model_dump(exclude_unset=True)
|
|
for field in update_data:
|
|
setattr(db_obj, field, update_data[field])
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
|
|
def delete_todo(db: Session, todo_id: int) -> Todo:
|
|
todo = db.query(Todo).filter(Todo.id == todo_id).first()
|
|
db.delete(todo)
|
|
db.commit()
|
|
return todo |