|
| 1 | +"""TodoList API router with CRUD endpoints.""" |
| 2 | + |
| 3 | +from typing import Annotated, List |
| 4 | + |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 6 | + |
| 7 | +from app.models import TodoList, TodoListCreate, TodoListUpdate |
| 8 | +from app.services.todo_lists import TodoListService, get_todo_list_service |
| 9 | + |
| 10 | +router = APIRouter(prefix="/api/todolists", tags=["todolists"]) |
| 11 | + |
| 12 | + |
| 13 | +@router.get("", response_model=List[TodoList], status_code=status.HTTP_200_OK) |
| 14 | +async def index( |
| 15 | + service: Annotated[TodoListService, Depends(get_todo_list_service)], |
| 16 | +) -> List[TodoList]: |
| 17 | + """ |
| 18 | + Get all todo lists. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + List of all TodoList objects |
| 22 | + """ |
| 23 | + return service.all() |
| 24 | + |
| 25 | + |
| 26 | +@router.get("/{todo_list_id}", response_model=TodoList, status_code=status.HTTP_200_OK) |
| 27 | +async def show( |
| 28 | + todo_list_id: int, |
| 29 | + service: Annotated[TodoListService, Depends(get_todo_list_service)], |
| 30 | +) -> TodoList: |
| 31 | + """ |
| 32 | + Get a specific todo list by ID. |
| 33 | +
|
| 34 | + Args: |
| 35 | + todo_list_id: The ID of the todo list to retrieve |
| 36 | + service: Injected TodoListService instance |
| 37 | +
|
| 38 | + Returns: |
| 39 | + TodoList object |
| 40 | +
|
| 41 | + Raises: |
| 42 | + HTTPException: 404 if todo list not found |
| 43 | + """ |
| 44 | + todo_list = service.get(todo_list_id) |
| 45 | + if todo_list is None: |
| 46 | + raise HTTPException( |
| 47 | + status_code=status.HTTP_404_NOT_FOUND, |
| 48 | + detail=f"TodoList with id {todo_list_id} not found", |
| 49 | + ) |
| 50 | + return todo_list |
| 51 | + |
| 52 | + |
| 53 | +@router.post("", response_model=TodoList, status_code=status.HTTP_201_CREATED) |
| 54 | +async def create( |
| 55 | + todo_list_data: TodoListCreate, |
| 56 | + service: Annotated[TodoListService, Depends(get_todo_list_service)], |
| 57 | +) -> TodoList: |
| 58 | + """ |
| 59 | + Create a new todo list. |
| 60 | +
|
| 61 | + Args: |
| 62 | + todo_list_data: Data for creating the todo list |
| 63 | + service: Injected TodoListService instance |
| 64 | +
|
| 65 | + Returns: |
| 66 | + The newly created TodoList object |
| 67 | + """ |
| 68 | + return service.create(todo_list_data) |
| 69 | + |
| 70 | + |
| 71 | +@router.put("/{todo_list_id}", response_model=TodoList, status_code=status.HTTP_200_OK) |
| 72 | +async def update( |
| 73 | + todo_list_id: int, |
| 74 | + todo_list_data: TodoListUpdate, |
| 75 | + service: Annotated[TodoListService, Depends(get_todo_list_service)], |
| 76 | +) -> TodoList: |
| 77 | + """ |
| 78 | + Update an existing todo list. |
| 79 | +
|
| 80 | + Args: |
| 81 | + todo_list_id: The ID of the todo list to update |
| 82 | + todo_list_data: New data for the todo list |
| 83 | + service: Injected TodoListService instance |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Updated TodoList object |
| 87 | +
|
| 88 | + Raises: |
| 89 | + HTTPException: 404 if todo list not found |
| 90 | + """ |
| 91 | + updated_todo_list = service.update(todo_list_id, todo_list_data) |
| 92 | + if updated_todo_list is None: |
| 93 | + raise HTTPException( |
| 94 | + status_code=status.HTTP_404_NOT_FOUND, |
| 95 | + detail=f"TodoList with id {todo_list_id} not found", |
| 96 | + ) |
| 97 | + return updated_todo_list |
| 98 | + |
| 99 | + |
| 100 | +@router.delete("/{todo_list_id}", status_code=status.HTTP_204_NO_CONTENT) |
| 101 | +async def delete( |
| 102 | + todo_list_id: int, |
| 103 | + service: Annotated[TodoListService, Depends(get_todo_list_service)], |
| 104 | +) -> None: |
| 105 | + """ |
| 106 | + Delete a todo list by ID. |
| 107 | +
|
| 108 | + Args: |
| 109 | + todo_list_id: The ID of the todo list to delete |
| 110 | + service: Injected TodoListService instance |
| 111 | +
|
| 112 | + Raises: |
| 113 | + HTTPException: 404 if todo list not found |
| 114 | + """ |
| 115 | + deleted = service.delete(todo_list_id) |
| 116 | + if not deleted: |
| 117 | + raise HTTPException( |
| 118 | + status_code=status.HTTP_404_NOT_FOUND, |
| 119 | + detail=f"TodoList with id {todo_list_id} not found", |
| 120 | + ) |
0 commit comments