-
Notifications
You must be signed in to change notification settings - Fork 214
feat(blog):add dynamic sorting, filtering, and pagination to blog retrieval #1198
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
base: dev
Are you sure you want to change the base?
Changes from 1 commit
29ed73a
1ee5367
7c3ebbe
b731938
a3f782f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,11 +26,35 @@ def create(self, db: Session, schema: BlogCreate, author_id: str): | |
| db.refresh(new_blogpost) | ||
| return new_blogpost | ||
|
|
||
| def fetch_all(self): | ||
| """Fetch all blog posts""" | ||
| def fetch_all( | ||
| self, | ||
| sort_by: Optional[str] = "created_at", | ||
| sort_order: Optional[str] = "desc", | ||
| author_id: Optional[str] = None, | ||
| category: Optional[str] = None, | ||
| limit: int = 10, | ||
| offset: int = 0 | ||
| ): | ||
| """Fetch all blog posts with sorting, filtering, and pagination""" | ||
|
|
||
| query = self.db.query(Blog).filter(Blog.is_deleted.is_(False)) | ||
|
|
||
| # Apply filters | ||
| if author_id: | ||
| query = query.filter(Blog.author_id == author_id) | ||
| if category: | ||
| query = query.filter(Blog.category == category) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This category filter is case-sensitive, consider using ilike instead of ==
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the category filter to use ilike instead of ==, making it case-insensitive. Now, searches will match categories regardless of letter case. |
||
|
|
||
| # Apply sorting | ||
| if sort_order == "desc": | ||
| query = query.order_by(desc(getattr(Blog, sort_by, Blog.created_at))) | ||
| else: | ||
| query = query.order_by(asc(getattr(Blog, sort_by, Blog.created_at))) | ||
|
|
||
| # Apply pagination | ||
| blogs = query.offset(offset).limit(limit).all() | ||
|
|
||
| blogs = self.db.query(Blog).filter(Blog.is_deleted == False).all() | ||
| return blogs | ||
| return blogs | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When implementing pagination, it's often useful to return the total count of records (before applying limit and offset) so that clients can calculate the total number of pages.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added total count to the response to support frontend pagination as suggested. Now the response includes both blogs and total_count. |
||
|
|
||
| def fetch(self, blog_id: str): | ||
| """Fetch a blog post by its ID""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.