""" Router for Todo CRUD operations """ from fastapi import APIRouter, Depends, HTTPException, Response, status from sqlalchemy.orm import Session from app.core.database import get_db from app.schemas.todo import Todo, TodoCreate, TodoUpdate from app.services import todo as todo_service router = APIRouter() @router.get("/todos", response_model=list[Todo]) def get_todos( skip: int = 0, limit: int = 100, db: Session = Depends(get_db) ): """ Get all todos with pagination """ todos = todo_service.get_todos(db, skip=skip, limit=limit) return todos @router.get("/todos/{todo_id}", response_model=Todo) def get_todo(todo_id: int, db: Session = Depends(get_db)): """ Get a single todo by ID """ db_todo = todo_service.get_todo(db, todo_id=todo_id) if db_todo is None: raise HTTPException( status_code=404, detail=f"Todo with ID {todo_id} not found" ) return db_todo @router.post("/todos", response_model=Todo, status_code=status.HTTP_201_CREATED) def create_todo(todo: TodoCreate, db: Session = Depends(get_db)): """ Create a new todo """ return todo_service.create_todo(db=db, todo=todo) @router.put("/todos/{todo_id}", response_model=Todo) def update_todo( todo_id: int, todo_update: TodoUpdate, db: Session = Depends(get_db) ): """ Update an existing todo """ db_todo = todo_service.update_todo(db, todo_id, todo_update) if db_todo is None: raise HTTPException( status_code=404, detail=f"Todo with ID {todo_id} not found" ) return db_todo @router.delete("/todos/{todo_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None) def delete_todo(todo_id: int, db: Session = Depends(get_db)): """ Delete a todo """ success = todo_service.delete_todo(db, todo_id) if not success: raise HTTPException( status_code=404, detail=f"Todo with ID {todo_id} not found" ) return Response(status_code=status.HTTP_204_NO_CONTENT)