
- 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
127 lines
3.5 KiB
Python
127 lines
3.5 KiB
Python
from datetime import datetime
|
|
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_active_user, get_db
|
|
from app.crud.crud_todo import (
|
|
create_todo,
|
|
delete_todo,
|
|
get_todo,
|
|
get_todos,
|
|
update_todo,
|
|
)
|
|
from app.models.todo import PriorityLevel
|
|
from app.models.user import User
|
|
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Todo])
|
|
def read_todos(
|
|
db: Session = Depends(get_db),
|
|
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,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve todos with optional filtering.
|
|
|
|
- **skip**: Number of records to skip for pagination
|
|
- **limit**: Maximum number of records to return
|
|
- **title**: Filter by title (contains search)
|
|
- **is_completed**: Filter by completion status
|
|
- **priority**: Filter by priority level (low, medium, high)
|
|
- **due_date_before**: Filter for todos due before this date
|
|
- **due_date_after**: Filter for todos due after this date
|
|
"""
|
|
todos = get_todos(
|
|
db=db,
|
|
owner_id=current_user.id,
|
|
skip=skip,
|
|
limit=limit,
|
|
title=title,
|
|
is_completed=is_completed,
|
|
priority=priority,
|
|
due_date_before=due_date_before,
|
|
due_date_after=due_date_after
|
|
)
|
|
return todos
|
|
|
|
|
|
@router.post("/", response_model=Todo)
|
|
def create_todo_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
todo_in: TodoCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Create new todo.
|
|
"""
|
|
todo = create_todo(db=db, todo_in=todo_in, owner_id=current_user.id)
|
|
return todo
|
|
|
|
|
|
@router.get("/{id}", response_model=Todo)
|
|
def read_todo(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get todo by ID.
|
|
"""
|
|
todo = get_todo(db=db, todo_id=id)
|
|
if not todo:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
if todo.owner_id != current_user.id:
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
return todo
|
|
|
|
|
|
@router.put("/{id}", response_model=Todo)
|
|
def update_todo_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
todo_in: TodoUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update a todo.
|
|
"""
|
|
todo = get_todo(db=db, todo_id=id)
|
|
if not todo:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
if todo.owner_id != current_user.id:
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
todo = update_todo(db=db, db_obj=todo, obj_in=todo_in)
|
|
return todo
|
|
|
|
|
|
@router.delete("/{id}", response_model=None, status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_todo_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Delete a todo.
|
|
"""
|
|
todo = get_todo(db=db, todo_id=id)
|
|
if not todo:
|
|
raise HTTPException(status_code=404, detail="Todo not found")
|
|
if todo.owner_id != current_user.id:
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
delete_todo(db=db, todo_id=id)
|
|
return None |