Skip to content

Commit 0098fb1

Browse files
committed
return list of books under a key
1 parent 66ca4f7 commit 0098fb1

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

src/http_app/routes/api/books.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from typing import Iterable
2+
13
from fastapi import APIRouter, status
2-
from pydantic import BaseModel, RootModel, ConfigDict
4+
from pydantic import BaseModel, ConfigDict
35

46
from domains.books import BookService, dto
57

@@ -20,23 +22,25 @@ class CreateBookResponse(BaseModel):
2022
)
2123

2224

23-
class ListBooksResponse(RootModel):
24-
root: list[dto.Book]
25+
class ListBooksResponse(BaseModel):
26+
books: Iterable[dto.Book]
2527
model_config = ConfigDict(
2628
json_schema_extra={
27-
"example":
28-
[
29-
{
30-
"title": "The Hitchhiker's Guide to the Galaxy",
31-
"author_name": "Douglas Adams",
32-
"book_id": 123,
33-
},
34-
{
35-
"title": "Clean Architecture: A Craftsman's Guide to Software Structure and Design",
36-
"author_name": "Robert C. 'Uncle Bob' Martin",
37-
"book_id": 321,
38-
},
39-
]
29+
"example": {
30+
"books": [
31+
{
32+
"title": "The Hitchhiker's Guide to the Galaxy",
33+
"author_name": "Douglas Adams",
34+
"book_id": 123,
35+
},
36+
{
37+
"title": "Clean Architecture: "
38+
"A Craftsman's Guide to Software Structure and Design",
39+
"author_name": "Robert C. 'Uncle Bob' Martin",
40+
"book_id": 321,
41+
},
42+
]
43+
}
4044
}
4145
)
4246

@@ -65,11 +69,12 @@ class CreateBookRequest(BaseModel):
6569
into the format needed for the proper HTTP Response
6670
"""
6771

72+
6873
@router_v1.get("/", status_code=status.HTTP_200_OK)
6974
async def list_books() -> ListBooksResponse:
7075
book_service = BookService()
7176
books = await book_service.list_books()
72-
return ListBooksResponse(root=books)
77+
return ListBooksResponse(books=books)
7378

7479

7580
@router_v1.post("/", status_code=status.HTTP_201_CREATED)

tests/http_app/routes/books/test_list_books.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
async def test_list_books(testapp):
66
ac = TestClient(app=testapp, base_url="http://test")
7-
response = ac.get(
8-
"/api/books/v1/"
9-
)
7+
response = ac.get("/api/books/v1/")
108
assert response.status_code == status.HTTP_200_OK
11-
assert len(response.json()) == 1
12-
assert response.json()[0]['title'] == "The Shining"
13-
assert response.json()[0]['author_name'] == "Stephen King"
9+
body = response.json()
10+
assert "books" in body
11+
assert len(body["books"]) == 1
12+
assert body["books"][0]["title"] == "The Shining"
13+
assert body["books"][0]["author_name"] == "Stephen King"

0 commit comments

Comments
 (0)