from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from typing import List, Any from app.api.crud import todo as crud from app.schemas.todo import Todo, TodoCreate, TodoUpdate from app.db.session import get_db router = APIRouter() @router.get("/", response_model=List[Todo]) def read_todos( db: Session = Depends(get_db), skip: int = 0, limit: int = 100, ) -> Any: """ Retrieve all todos. """ todos = crud.get_todos(db, skip=skip, limit=limit) return todos @router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED) def create_todo( *, db: Session = Depends(get_db), todo_in: TodoCreate, ) -> Any: """ Create new todo. """ todo = crud.create_todo(db, todo_in) return todo @router.get("/{todo_id}", response_model=Todo) def read_todo( *, db: Session = Depends(get_db), todo_id: int, ) -> Any: """ Get todo by ID. """ todo = crud.get_todo(db, todo_id) if not todo: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) return todo @router.put("/{todo_id}", response_model=Todo) def update_todo( *, db: Session = Depends(get_db), todo_id: int, todo_in: TodoUpdate, ) -> Any: """ Update a todo. """ todo = crud.update_todo(db, todo_id, todo_in) if not todo: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) return todo @router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_todo( *, db: Session = Depends(get_db), todo_id: int, ) -> Any: """ Delete a todo. """ success = crud.delete_todo(db, todo_id) if not success: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) return None