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 1 commit
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
29 changes: 28 additions & 1 deletion src/http_app/routes/api/books.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fastapi import APIRouter, status
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, RootModel, ConfigDict

from domains.books import BookService, dto

Expand All @@ -20,6 +20,27 @@ class CreateBookResponse(BaseModel):
)


class ListBooksResponse(RootModel):
root: list[dto.Book]
model_config = ConfigDict(
json_schema_extra={
"example":
[
{
"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 @@ -44,6 +65,12 @@ class CreateBookRequest(BaseModel):
into the format needed for the proper HTTP Response
"""

@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(root=books)


@router_v1.post("/", status_code=status.HTTP_201_CREATED)
async def create_book(
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
assert len(response.json()) == 1
assert response.json()[0]['title'] == "The Shining"
assert response.json()[0]['author_name'] == "Stephen King"