Automated Action 674c0542ae Fix HTTP 204 status code response handling
- Modified delete_user_endpoint to return None for HTTP 204 status code
- Updated delete_todo endpoint to also return None for HTTP 204 status code
- Added proper return type annotations to both endpoints
- This fixes the AssertionError: Status code 204 must not have a response body
2025-05-16 02:28:23 +00:00

89 lines
2.6 KiB
Python

from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.core.deps import get_current_active_user
from app.crud import todo as todo_crud
from app.db.database import get_db
from app.models.user import User
from app.schemas.todo import Todo, TodoCreate, TodoUpdate
router = APIRouter(
prefix="/todos",
tags=["todos"],
responses={404: {"description": "Todo not found"}},
)
@router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED)
def create_todo(
todo: TodoCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
"""
Create a new todo item for the current user.
"""
return todo_crud.create_todo(db=db, todo=todo, owner_id=current_user.id)
@router.get("/", response_model=List[Todo])
def read_todos(
skip: int = 0,
limit: int = 100,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
"""
Retrieve all todo items for the current user with pagination.
"""
todos = todo_crud.get_todos_by_owner(db, owner_id=current_user.id, skip=skip, limit=limit)
return todos
@router.get("/{todo_id}", response_model=Todo)
def read_todo(
todo_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
"""
Retrieve a specific todo item by ID for the current user.
"""
db_todo = todo_crud.get_todo_by_owner(db, todo_id=todo_id, owner_id=current_user.id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@router.patch("/{todo_id}", response_model=Todo)
def update_todo(
todo_id: int,
todo: TodoUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
):
"""
Update a todo item for the current user.
"""
db_todo = todo_crud.update_todo(db, todo_id=todo_id, todo=todo, owner_id=current_user.id)
if db_todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return db_todo
@router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_todo(
todo_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_active_user)
) -> None:
"""
Delete a todo item for the current user.
"""
success = todo_crud.delete_todo(db, todo_id=todo_id, owner_id=current_user.id)
if not success:
raise HTTPException(status_code=404, detail="Todo not found")
# Explicitly return None for 204 response
return None