Skip to content

Commit 6da9144

Browse files
committed
feature: page number pagination added to list endpoints
1 parent 836f293 commit 6da9144

File tree

4 files changed

+75
-32
lines changed

4 files changed

+75
-32
lines changed

src/authors/routes.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ async def update_author_profile_image(
134134

135135

136136
@author_router.get("/list", status_code=status.HTTP_200_OK)
137-
async def list_authors(session: AsyncSession = Depends(get_session)):
138-
authors = await author_service.list_authors(session)
137+
async def list_authors(request: Request, session: AsyncSession = Depends(get_session)):
138+
page = int(request.query_params.get("page", 1))
139+
140+
authors = await author_service.list_authors(page, session)
139141

140142
return JSONResponse(
141143
status_code=status.HTTP_200_OK,
@@ -153,9 +155,13 @@ async def list_authors(session: AsyncSession = Depends(get_session)):
153155

154156
@author_router.get("/list/{nationality}", status_code=status.HTTP_200_OK)
155157
async def list_authors_by_nationality(
156-
nationality: str, session: AsyncSession = Depends(get_session)
158+
request: Request, nationality: str, session: AsyncSession = Depends(get_session)
157159
):
158-
authors = await author_service.list_authors_by_nationality(nationality, session)
160+
page = int(request.query_params.get("page", 1))
161+
162+
authors = await author_service.list_authors_by_nationality(
163+
nationality, page, session
164+
)
159165

160166
return JSONResponse(
161167
status_code=status.HTTP_200_OK,

src/authors/service.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,18 @@ async def get_author_by_name(
7575
return author
7676

7777
async def list_authors_by_nationality(
78-
self, nationality: str, session: AsyncSession
78+
self, nationality: str, page: int, session: AsyncSession
7979
):
8080
result = await session.execute(
81-
select(Author).where(Author.nationality == nationality)
81+
select(Author)
82+
.where(Author.nationality == nationality)
83+
.offset((page - 1) * 10)
84+
.limit(10)
8285
)
8386
authors = result.scalars().all()
8487
return authors
8588

86-
async def list_authors(self, session: AsyncSession):
87-
result = await session.execute(select(Author))
89+
async def list_authors(self, page: int, session: AsyncSession):
90+
result = await session.execute(select(Author).offset((page - 1) * 10).limit(10))
8891
authors = result.scalars().all()
8992
return authors

src/books/routes.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ async def update_book_category(
9898

9999

100100
@book_category_router.get("/list", status_code=status.HTTP_200_OK)
101-
async def list_book_categories(session: AsyncSession = Depends(get_session)):
102-
book_categories = await book_category_service.list_book_categories(session)
101+
async def list_book_categories(
102+
request: Request, session: AsyncSession = Depends(get_session)
103+
):
104+
page = int(request.query_params.get("page", 1))
105+
106+
book_categories = await book_category_service.list_book_categories(page, session)
103107

104108
return JSONResponse(
105109
status_code=status.HTTP_200_OK,
@@ -232,8 +236,12 @@ async def update_book_genre(
232236

233237

234238
@book_genre_router.get("/list", status_code=status.HTTP_200_OK)
235-
async def list_book_genres(session: AsyncSession = Depends(get_session)):
236-
book_genres = await book_genre_service.list_book_genres(session)
239+
async def list_book_genres(
240+
request: Request, session: AsyncSession = Depends(get_session)
241+
):
242+
page = int(request.query_params.get("page", 1))
243+
244+
book_genres = await book_genre_service.list_book_genres(page, session)
237245

238246
return JSONResponse(
239247
status_code=status.HTTP_200_OK,
@@ -363,8 +371,10 @@ async def update_book(
363371

364372

365373
@book_router.get("/list", status_code=status.HTTP_200_OK)
366-
async def list_books(session: AsyncSession = Depends(get_session)):
367-
books = await book_service.list_books(session)
374+
async def list_books(request: Request, session: AsyncSession = Depends(get_session)):
375+
page = int(request.query_params.get("page", 1))
376+
377+
books = await book_service.list_books(page, session)
368378

369379
return JSONResponse(
370380
status_code=status.HTTP_200_OK,
@@ -443,9 +453,11 @@ async def get_book_by_title(title: str, session: AsyncSession = Depends(get_sess
443453

444454
@book_router.get("/list/category/{category}", status_code=status.HTTP_200_OK)
445455
async def list_books_by_category(
446-
category: str, session: AsyncSession = Depends(get_session)
456+
request: Request, category: str, session: AsyncSession = Depends(get_session)
447457
):
448-
books = await book_service.list_books_by_category(category, session)
458+
page = int(request.query_params.get("page", 1))
459+
460+
books = await book_service.list_books_by_category(category, page, session)
449461

450462
return JSONResponse(
451463
status_code=status.HTTP_200_OK,
@@ -460,8 +472,12 @@ async def list_books_by_category(
460472

461473

462474
@book_router.get("/list/genre/{genre}", status_code=status.HTTP_200_OK)
463-
async def list_books_by_genre(genre: str, session: AsyncSession = Depends(get_session)):
464-
books = await book_service.list_books_by_genre(genre, session)
475+
async def list_books_by_genre(
476+
request: Request, genre: str, session: AsyncSession = Depends(get_session)
477+
):
478+
page = int(request.query_params.get("page", 1))
479+
480+
books = await book_service.list_books_by_genre(genre, page, session)
465481

466482
return JSONResponse(
467483
status_code=status.HTTP_200_OK,
@@ -477,9 +493,11 @@ async def list_books_by_genre(genre: str, session: AsyncSession = Depends(get_se
477493

478494
@book_router.get("/list/author/{author}", status_code=status.HTTP_200_OK)
479495
async def list_books_by_author(
480-
author: str, session: AsyncSession = Depends(get_session)
496+
request: Request, author: str, session: AsyncSession = Depends(get_session)
481497
):
482-
books = await book_service.list_books_by_author(author, session)
498+
page = int(request.query_params.get("page", 1))
499+
500+
books = await book_service.list_books_by_author(author, page, session)
483501

484502
return JSONResponse(
485503
status_code=status.HTTP_200_OK,

src/books/service.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ async def get_book_category_by_category(self, category: str, session: AsyncSessi
4444
book_category = result.scalars().first()
4545
return book_category
4646

47-
async def list_book_categories(self, session: AsyncSession):
48-
result = await session.execute(select(BookCategory))
47+
async def list_book_categories(self, page: int, session: AsyncSession):
48+
result = await session.execute(
49+
select(BookCategory).offset((page - 1) * 10).limit(10)
50+
)
4951
book_categories = result.scalars().all()
5052
return book_categories
5153

@@ -88,8 +90,10 @@ async def get_book_genre_by_genre(self, genre: str, session: AsyncSession):
8890
book_genre = result.scalars().first()
8991
return book_genre
9092

91-
async def list_book_genres(self, session: AsyncSession):
92-
result = await session.execute(select(BookGenre))
93+
async def list_book_genres(self, page: int, session: AsyncSession):
94+
result = await session.execute(
95+
select(BookGenre).offset((page - 1) * 10).limit(10)
96+
)
9397
book_genres = result.scalars().all()
9498
return book_genres
9599

@@ -136,26 +140,38 @@ async def get_book_by_title(self, title: str, session: AsyncSession):
136140
book = result.scalars().first()
137141
return book
138142

139-
async def list_books(self, session: AsyncSession):
140-
result = await session.execute(select(Book))
143+
async def list_books(self, page: int, session: AsyncSession):
144+
result = await session.execute(select(Book).offset((page - 1) * 10).limit(10))
141145
books = result.scalars().all()
142146
return books
143147

144-
async def list_books_by_category(self, category: str, session: AsyncSession):
148+
async def list_books_by_category(
149+
self, category: str, page: int, session: AsyncSession
150+
):
145151
result = await session.execute(
146-
select(Book).where(Book.categories.contains(category))
152+
select(Book).where(
153+
Book.categories.contains(category).offset((page - 1) * 10).limit(10)
154+
)
147155
)
148156
books = result.scalars().all()
149157
return books
150158

151-
async def list_books_by_genre(self, genre: str, session: AsyncSession):
152-
result = await session.execute(select(Book).where(Book.genres.contains(genre)))
159+
async def list_books_by_genre(self, genre: str, page: int, session: AsyncSession):
160+
result = await session.execute(
161+
select(Book)
162+
.where(Book.genres.contains(genre))
163+
.offset((page - 1) * 10)
164+
.limit(10)
165+
)
153166
books = result.scalars().all()
154167
return books
155168

156-
async def list_books_by_author(self, author: str, session: AsyncSession):
169+
async def list_books_by_author(self, author: str, page: int, session: AsyncSession):
157170
result = await session.execute(
158-
select(Book).where(Book.authors.contains(author))
171+
select(Book)
172+
.where(Book.authors.contains(author))
173+
.offset((page - 1) * 10)
174+
.limit(10)
159175
)
160176
books = result.scalars().all()
161177
return books

0 commit comments

Comments
 (0)