11from fastapi import APIRouter , status
2- from pydantic import BaseModel , ConfigDict
2+ from pydantic import BaseModel , RootModel , ConfigDict
33
44from 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+
2344class 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 )
4976async def create_book (
0 commit comments