Skip to content

add list books route #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/http_app/routes/api/books.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Iterable

from fastapi import APIRouter, status
from pydantic import BaseModel, ConfigDict

Expand All @@ -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
Expand All @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/http_app/routes/books/test_list_books.py
Original file line number Diff line number Diff line change
@@ -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"
Loading