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

Conversation

Karadumk
Copy link

@Karadumk Karadumk commented Mar 1, 2025

Description

This PR introduces dynamic sorting, filtering, and pagination for the blog retrieval endpoint. Users can now specify sorting fields, filter by relevant attributes, and paginate results efficiently. Additionally, validation has been added to ensure that sort_by parameters are valid columns. The response now includes metadata like total_count to improve frontend pagination.

Motivation and Context

This change enhances the flexibility of blog retrieval, making it easier for users to query and navigate blog posts effectively. It also prevents invalid sorting parameters and improves API usability for frontend developers.

How Has This Been Tested?

  • Manual testing using Postman to verify sorting, filtering, and pagination behavior.
  • Unit tests added for query validation and response format.
  • Tested against existing API consumers to ensure backward compatibility.

Types of Changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

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.


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants