from typing import Any, List from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from app.core.deps import get_db from app.db.repositories.todo import todo_repository from app.schemas.todo import Todo, TodoCreate, TodoList, TodoUpdate router = APIRouter() @router.get("/", response_model=TodoList) def list_todos( skip: int = 0, limit: int = 100, db: Session = Depends(get_db) ) -> Any: """ Retrieve all todos. """ todos = todo_repository.get_todos(db, skip=skip, limit=limit) count = todo_repository.get_todo_count(db) return {"items": todos, "count": count} @router.post("/", response_model=Todo, status_code=status.HTTP_201_CREATED) def create_todo( todo_in: TodoCreate, db: Session = Depends(get_db) ) -> Any: """ Create new todo. """ todo = todo_repository.create_todo(db, todo_in) return todo @router.get("/{todo_id}", response_model=Todo) def get_todo( todo_id: int, db: Session = Depends(get_db) ) -> Any: """ Get todo by ID. """ todo = todo_repository.get_todo(db, todo_id=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( todo_id: int, todo_in: TodoUpdate, db: Session = Depends(get_db) ) -> Any: """ Update a todo. """ todo = todo_repository.get_todo(db, todo_id=todo_id) if not todo: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) todo = todo_repository.update_todo(db, db_obj=todo, obj_in=todo_in) return todo @router.delete("/{todo_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_todo( todo_id: int, db: Session = Depends(get_db) ) -> None: """ Delete a todo. """ todo = todo_repository.get_todo(db, todo_id=todo_id) if not todo: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) todo_repository.delete_todo(db, todo_id=todo_id)