diff --git a/README.md b/README.md index 58d53843..5dadb4e1 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ Create your GitHub repository using this template (The big green `Use this templ Optionally tweak name and authors in the `pyproject.toml` file, however the metadata are not used when building the application, nor are referenced anywhere in the code. +Before running any commands, install `uv`: + +- On Mac (using `brew`): `brew install uv` + Using Docker: * `make containers`: Build containers diff --git a/src/http_app/routes/api/books.py b/src/http_app/routes/api/books.py index b99797ed..79821d42 100644 --- a/src/http_app/routes/api/books.py +++ b/src/http_app/routes/api/books.py @@ -1,3 +1,5 @@ +from typing import Iterable + from fastapi import APIRouter, status from pydantic import BaseModel, ConfigDict @@ -20,6 +22,29 @@ class CreateBookResponse(BaseModel): ) +class ListBooksResponse(BaseModel): + books: Iterable[dto.Book] + model_config = ConfigDict( + json_schema_extra={ + "example": { + "books": [ + { + "title": "The Hitchhiker's Guide to the Galaxy", + "author_name": "Douglas Adams", + "book_id": 123, + }, + { + "title": "Clean Architecture: " + "A Craftsman's Guide to Software Structure and Design", + "author_name": "Robert C. 'Uncle Bob' Martin", + "book_id": 321, + }, + ] + } + } + ) + + class CreateBookRequest(BaseModel): title: str author_name: str @@ -45,6 +70,13 @@ class CreateBookRequest(BaseModel): """ +@router_v1.get("/", status_code=status.HTTP_200_OK) +async def list_books() -> ListBooksResponse: + book_service = BookService() + books = await book_service.list_books() + return ListBooksResponse(books=books) + + @router_v1.post("/", status_code=status.HTTP_201_CREATED) async def create_book( data: CreateBookRequest, diff --git a/tests/http_app/routes/books/test_list_books.py b/tests/http_app/routes/books/test_list_books.py new file mode 100644 index 00000000..98ed07d8 --- /dev/null +++ b/tests/http_app/routes/books/test_list_books.py @@ -0,0 +1,13 @@ +from fastapi import status +from fastapi.testclient import TestClient + + +async def test_list_books(testapp): + ac = TestClient(app=testapp, base_url="http://test") + response = ac.get("/api/books/v1/") + assert response.status_code == status.HTTP_200_OK + body = response.json() + assert "books" in body + assert len(body["books"]) == 1 + assert body["books"][0]["title"] == "The Shining" + assert body["books"][0]["author_name"] == "Stephen King"