Skip to content

Commit fa37b73

Browse files
authored
add list books route (#231)
add list books route
1 parent 6178ecb commit fa37b73

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Create your GitHub repository using this template (The big green `Use this templ
4141
Optionally tweak name and authors in the `pyproject.toml` file, however the metadata
4242
are not used when building the application, nor are referenced anywhere in the code.
4343

44+
Before running any commands, install `uv`:
45+
46+
- On Mac (using `brew`): `brew install uv`
47+
4448
Using Docker:
4549

4650
* `make containers`: Build containers

src/http_app/routes/api/books.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Iterable
2+
13
from fastapi import APIRouter, status
24
from pydantic import BaseModel, ConfigDict
35

@@ -20,6 +22,29 @@ class CreateBookResponse(BaseModel):
2022
)
2123

2224

25+
class ListBooksResponse(BaseModel):
26+
books: Iterable[dto.Book]
27+
model_config = ConfigDict(
28+
json_schema_extra={
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+
}
44+
}
45+
)
46+
47+
2348
class CreateBookRequest(BaseModel):
2449
title: str
2550
author_name: str
@@ -45,6 +70,13 @@ class CreateBookRequest(BaseModel):
4570
"""
4671

4772

73+
@router_v1.get("/", status_code=status.HTTP_200_OK)
74+
async def list_books() -> ListBooksResponse:
75+
book_service = BookService()
76+
books = await book_service.list_books()
77+
return ListBooksResponse(books=books)
78+
79+
4880
@router_v1.post("/", status_code=status.HTTP_201_CREATED)
4981
async def create_book(
5082
data: CreateBookRequest,
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("/api/books/v1/")
8+
assert response.status_code == status.HTTP_200_OK
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)