From 170fac63fa67b119f20223e18a86e46925f66433 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Tue, 17 Jun 2025 06:28:41 +0000 Subject: [PATCH] Add bulk complete todos endpoint for efficient batch operations --- app/api/v1/todos.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/api/v1/todos.py b/app/api/v1/todos.py index b6b88f2..497ae8c 100644 --- a/app/api/v1/todos.py +++ b/app/api/v1/todos.py @@ -2,6 +2,7 @@ from typing import List, Optional from fastapi import APIRouter, Depends, HTTPException, status, Query from sqlalchemy.orm import Session +from pydantic import BaseModel from app.crud import todo from app.db.session import get_db @@ -10,6 +11,10 @@ from app.schemas.todo import TodoCreate, TodoResponse, TodoUpdate router = APIRouter() +class BulkCompleteRequest(BaseModel): + todo_ids: List[int] + + @router.get("/", response_model=List[TodoResponse]) def read_todos( skip: int = 0, @@ -117,3 +122,36 @@ def delete_todo( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) return todo_obj + + +@router.post("/bulk-complete", response_model=List[TodoResponse]) +def bulk_complete_todos( + *, + db: Session = Depends(get_db), + request: BulkCompleteRequest, +): + """ + Mark multiple todos as completed. + """ + completed_todos = [] + not_found_ids = [] + + for todo_id in request.todo_ids: + todo_obj = todo.get(db=db, todo_id=todo_id) + if todo_obj: + updated_todo = todo.update( + db=db, + db_obj=todo_obj, + obj_in={"completed": True} + ) + completed_todos.append(updated_todo) + else: + not_found_ids.append(todo_id) + + if not_found_ids: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=f"Todos not found: {not_found_ids}" + ) + + return completed_todos