from typing import List from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from app.db.session import get_db from app.db import crud from app.models.schemas import TodoCreate, TodoResponse, TodoUpdate router = APIRouter() @router.get("/todos", response_model=List[TodoResponse]) def get_todos(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): """Get all todos with pagination""" todos = crud.get_todos(db, skip=skip, limit=limit) return todos @router.get("/todos/{todo_id}", response_model=TodoResponse) def get_todo(todo_id: int, db: Session = Depends(get_db)): """Get a specific todo by ID""" todo = crud.get_todo(db, todo_id=todo_id) if todo is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) return todo @router.post("/todos", response_model=TodoResponse, status_code=status.HTTP_201_CREATED) def create_todo(todo: TodoCreate, db: Session = Depends(get_db)): """Create a new todo""" return crud.create_todo(db=db, todo=todo) @router.put("/todos/{todo_id}", response_model=TodoResponse) def update_todo(todo_id: int, todo_update: TodoUpdate, db: Session = Depends(get_db)): """Update an existing todo""" todo = crud.update_todo(db, todo_id=todo_id, todo_update=todo_update) if todo is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" ) return todo @router.delete("/todos/{todo_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_todo(todo_id: int, db: Session = Depends(get_db)): """Delete a todo""" success = crud.delete_todo(db, todo_id=todo_id) if not success: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found" )