Add bulk complete todos endpoint for efficient batch operations
This commit is contained in:
parent
40981760c9
commit
170fac63fa
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user