from typing import Any, List, Optional from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from app import crud, models, schemas from app.api.deps import get_db from app.crud.crud_todo import todo router = APIRouter() @router.get("/", response_model=List[schemas.Todo]) def read_todos( db: Session = Depends(get_db), skip: int = 0, limit: int = 100, completed: Optional[bool] = None, ) -> Any: """ Retrieve todos. """ if completed is not None: todos = todo.get_multi_by_completion(db, completed=completed, skip=skip, limit=limit) else: todos = todo.get_multi(db, skip=skip, limit=limit) return todos @router.post("/", response_model=schemas.Todo) def create_todo( *, db: Session = Depends(get_db), todo_in: schemas.TodoCreate, ) -> Any: """ Create new todo. """ return todo.create(db=db, obj_in=todo_in) @router.get("/{id}", response_model=schemas.Todo) def read_todo( *, db: Session = Depends(get_db), id: int, ) -> Any: """ Get todo by ID. """ todo_item = todo.get(db=db, id=id) if not todo_item: raise HTTPException(status_code=404, detail="Todo not found") return todo_item @router.put("/{id}", response_model=schemas.Todo) def update_todo( *, db: Session = Depends(get_db), id: int, todo_in: schemas.TodoUpdate, ) -> Any: """ Update a todo. """ todo_item = todo.get(db=db, id=id) if not todo_item: raise HTTPException(status_code=404, detail="Todo not found") todo_item = todo.update(db=db, db_obj=todo_item, obj_in=todo_in) return todo_item @router.delete("/{id}", response_model=schemas.Todo) def delete_todo( *, db: Session = Depends(get_db), id: int, ) -> Any: """ Delete a todo. """ todo_item = todo.get(db=db, id=id) if not todo_item: raise HTTPException(status_code=404, detail="Todo not found") todo_item = todo.remove(db=db, id=id) return todo_item