Skip to content

Commit 66ca4f7

Browse files
committed
add list books route
1 parent f7e12ef commit 66ca4f7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/http_app/routes/api/books.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from fastapi import APIRouter, status
2-
from pydantic import BaseModel, ConfigDict
2+
from pydantic import BaseModel, RootModel, ConfigDict
33

44
from domains.books import BookService, dto
55

@@ -20,6 +20,27 @@ class CreateBookResponse(BaseModel):
2020
)
2121

2222

23+
class ListBooksResponse(RootModel):
24+
root: list[dto.Book]
25+
model_config = ConfigDict(
26+
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+
]
40+
}
41+
)
42+
43+
2344
class CreateBookRequest(BaseModel):
2445
title: str
2546
author_name: str
@@ -44,6 +65,12 @@ class CreateBookRequest(BaseModel):
4465
into the format needed for the proper HTTP Response
4566
"""
4667

68+
@router_v1.get("/", status_code=status.HTTP_200_OK)
69+
async def list_books() -> ListBooksResponse:
70+
book_service = BookService()
71+
books = await book_service.list_books()
72+
return ListBooksResponse(root=books)
73+
4774

4875
@router_v1.post("/", status_code=status.HTTP_201_CREATED)
4976
async def create_book(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from fastapi import status
2+
from fastapi.testclient import TestClient
3+
4+
5+
async def test_list_books(testapp):
6+
ac = TestClient(app=testapp, base_url="http://test")
7+
response = ac.get(
8+
"/api/books/v1/"
9+
)
10+
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"

0 commit comments

Comments
 (0)