Skip to content
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

feat(blog):add dynamic sorting, filtering, and pagination to blog retrieval #1198

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions api/v1/services/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This category filter is case-sensitive, consider using ilike instead of ==

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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"""
Expand Down