Skip to content

Commit 702ab74

Browse files
committed
Refactor pagination filters
1 parent 7e98eda commit 702ab74

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

conduit/api/routes/article.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
from fastapi import APIRouter
2-
from fastapi.params import Query
32
from starlette import status
43

54
from conduit.api.schemas.requests.article import (
6-
DEFAULT_ARTICLES_LIMIT,
7-
DEFAULT_ARTICLES_OFFSET,
85
CreateArticleRequest,
96
UpdateArticleRequest,
107
)
@@ -14,6 +11,7 @@
1411
CurrentUser,
1512
DBSession,
1613
IArticleService,
14+
Pagination,
1715
QueryFilters,
1816
)
1917

@@ -22,23 +20,26 @@
2220

2321
@router.get("/feed", response_model=ArticlesFeedResponse)
2422
async def get_article_feed(
23+
pagination: Pagination,
2524
session: DBSession,
2625
current_user: CurrentUser,
2726
article_service: IArticleService,
28-
limit: int = Query(DEFAULT_ARTICLES_LIMIT, ge=1),
29-
offset: int = Query(DEFAULT_ARTICLES_OFFSET, ge=0),
3027
) -> ArticlesFeedResponse:
3128
"""
3229
Get article feed from following users.
3330
"""
3431
articles_feed_dto = await article_service.get_articles_feed_v2(
35-
session=session, current_user=current_user, limit=limit, offset=offset
32+
session=session,
33+
current_user=current_user,
34+
limit=pagination.limit,
35+
offset=pagination.offset,
3636
)
3737
return ArticlesFeedResponse.from_dto(dto=articles_feed_dto)
3838

3939

4040
@router.get("", response_model=ArticlesFeedResponse)
4141
async def get_global_article_feed(
42+
pagination: Pagination,
4243
articles_filters: QueryFilters,
4344
session: DBSession,
4445
current_user: CurrentOptionalUser,
@@ -53,8 +54,8 @@ async def get_global_article_feed(
5354
tag=articles_filters.tag,
5455
author=articles_filters.author,
5556
favorited=articles_filters.favorited,
56-
limit=articles_filters.limit,
57-
offset=articles_filters.offset,
57+
limit=pagination.limit,
58+
offset=pagination.offset,
5859
)
5960
return ArticlesFeedResponse.from_dto(dto=articles_feed_dto)
6061

conduit/api/schemas/requests/article.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
from conduit.domain.dtos.article import CreateArticleDTO, UpdateArticleDTO
44

5-
DEFAULT_ARTICLES_LIMIT = 20
6-
DEFAULT_ARTICLES_OFFSET = 0
5+
6+
class ArticlesPagination(BaseModel):
7+
limit: int = Field(ge=1)
8+
offset: int = Field(ge=0)
79

810

911
class ArticlesFilters(BaseModel):
1012
tag: str | None = None
1113
author: str | None = None
1214
favorited: str | None = None
13-
limit: int = Field(DEFAULT_ARTICLES_LIMIT, ge=1)
14-
offset: int = Field(DEFAULT_ARTICLES_OFFSET, ge=0)
1515

1616

1717
class CreateArticleData(BaseModel):
18-
title: str
19-
description: str
20-
body: str
18+
title: str = Field(..., min_length=5)
19+
description: str = Field(min_length=10)
20+
body: str = Field(min_length=10)
2121
tags: list[str] = Field(alias="tagList")
2222

2323

conduit/core/dependencies.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
from fastapi import Depends, Query
44
from sqlalchemy.ext.asyncio import AsyncSession
55

6-
from conduit.api.schemas.requests.article import (
7-
DEFAULT_ARTICLES_LIMIT,
8-
DEFAULT_ARTICLES_OFFSET,
9-
ArticlesFilters,
10-
)
6+
from conduit.api.schemas.requests.article import ArticlesFilters, ArticlesPagination
117
from conduit.core.container import container
128
from conduit.core.security import HTTPTokenHeader
139
from conduit.domain.dtos.user import UserDTO
@@ -45,17 +41,22 @@
4541
IArticleService = Annotated[ArticleService, Depends(container.article_service)]
4642
ICommentService = Annotated[CommentService, Depends(container.comment_service)]
4743

44+
DEFAULT_ARTICLES_LIMIT = 20
45+
DEFAULT_ARTICLES_OFFSET = 0
4846

49-
def get_articles_filters(
50-
tag: str | None = None,
51-
author: str | None = None,
52-
favorited: str | None = None,
47+
48+
def get_articles_pagination(
5349
limit: int = Query(DEFAULT_ARTICLES_LIMIT, ge=1),
5450
offset: int = Query(DEFAULT_ARTICLES_OFFSET, ge=0),
51+
) -> ArticlesPagination:
52+
limit = min(limit, DEFAULT_ARTICLES_LIMIT)
53+
return ArticlesPagination(limit=limit, offset=offset)
54+
55+
56+
def get_articles_filters(
57+
tag: str | None = None, author: str | None = None, favorited: str | None = None
5558
) -> ArticlesFilters:
56-
return ArticlesFilters(
57-
tag=tag, author=author, favorited=favorited, limit=limit, offset=offset
58-
)
59+
return ArticlesFilters(tag=tag, author=author, favorited=favorited)
5960

6061

6162
async def get_current_user_or_none(
@@ -85,6 +86,7 @@ async def get_current_user(
8586
return current_user_dto
8687

8788

89+
Pagination = Annotated[ArticlesPagination, Depends(get_articles_pagination)]
8890
QueryFilters = Annotated[ArticlesFilters, Depends(get_articles_filters)]
8991
CurrentOptionalUser = Annotated[UserDTO | None, Depends(get_current_user_or_none)]
9092
CurrentUser = Annotated[UserDTO, Depends(get_current_user)]

0 commit comments

Comments
 (0)