Skip to content

feat: add dynamic filtering and pagination for Department search API#34

Open
Sahilmansoori373 wants to merge 1 commit intopmoustopoulos:masterfrom
Sahilmansoori373:feature/dynamic-filtering-pagination
Open

feat: add dynamic filtering and pagination for Department search API#34
Sahilmansoori373 wants to merge 1 commit intopmoustopoulos:masterfrom
Sahilmansoori373:feature/dynamic-filtering-pagination

Conversation

@Sahilmansoori373
Copy link

This pull request introduces dynamic filtering, pagination, and sorting for the Department search functionality, enhancing the /api/v1/departments/search endpoint with more flexible and efficient querying capabilities.

🔍 What’s New

Dynamic Filtering: Implemented using Spring Data JPA Specifications, allowing queries based on multiple optional criteria (departmentCode, departmentName, departmentDescription, etc.).

Pagination & Sorting: Integrated pageable and sort functionality to handle large datasets and improve response management.

Code Enhancements:

Added DepartmentSpecification.java for reusable query construction.

Updated DepartmentRepository.java to extend JpaSpecificationExecutor.

Modified DepartmentServiceImpl.java to apply specifications with pagination and sorting.

🧩 Technical Details

Uses CriteriaBuilder and Predicate to build dynamic queries at runtime.

Returns results as Page to maintain consistent response structure.

Backward compatible — no impact on existing Department CRUD operations.

Optimized for maintainability and future extensibility (e.g., filtering by date ranges or relationships).

🧪 How to Test

Run the Spring Boot application.

Open Swagger UI → http://localhost:8080/department-api/swagger-ui/index.html

Navigate to the POST /api/v1/departments/search endpoint.

Try the following example:

{
"departmentCode": "HR",
"departmentName": "eng",
"departmentDescription": "engineer",
"page": 0,
"size": 5,
"sortList": [
{ "field": "departmentName", "direction": "ASC" }
]
}

Verify results are filtered, paginated, and sorted dynamically.

🚀 Benefits

Enables multi-field search without hardcoding queries.

Improves performance and scalability for large datasets.

Makes the API more developer-friendly and extensible.

Follows Spring Boot 3 + Java 21 best practices.

✅ Summary

This PR makes the Department API more powerful and production-ready by introducing a dynamic search system with pagination and sorting, paving the way for advanced enterprise-grade querying capabilities.

@pmoustopoulos
Copy link
Owner

Hi @Sahilmansoori373,

Thank you for taking the time to propose improvements, really appreciate the initiative and the thoughtfulness in using Spring Data JPA Specifications. Your approach is clean and would definitely help if the search logic becomes more complex over time.

Right now, my existing implementation already covers the PR’s goals:

Dynamic filtering: The JPQL query with SpEL applies optional criteria on departmentCode, departmentName, and departmentDescription with case‑insensitive matching.

Pagination & sorting: I construct Pageable from the incoming SortItem list. Multiple sort fields and directions are supported, and behavior is already verified by tests.

DTO mapping and Page response remain consistent.

On page/size handling: in our controller, these are validated with @NotNull and positivity annotations, so requests with null values are rejected early with a 400. Additionally, I have internal defaults in Utils to keep things resilient if these values ever come through null from non‑HTTP flows.

Given the current scope (a small set of filters and straightforward operators), the JPQL+SpEL approach is simpler and well‑tested, so I would prefer to keep it for now. That said, I like the direction you suggested. If/when I add more filters, need composable predicates, or introduce joins, I would be happy to switch this endpoint to Specifications using a DepartmentSpecification builder and your PR would be a great starting point for that.

What do you think? If you see any gaps in my reasoning or specific cases where Specifications would add immediate value here, I am happy to discuss and adjust.

Thanks again for the contribution and for pushing the codebase forward! Let’s revisit Specifications as soon as the query needs grow.

Context

  • This comment corresponds to the Department search endpoint at POST /api/v1/departments/search.
  • Relevant classes:
    • com.ainigma100.departmentapi.controller.DepartmentController
    • com.ainigma100.departmentapi.service.impl.DepartmentServiceImpl
    • com.ainigma100.departmentapi.repository.DepartmentRepository
    • com.ainigma100.departmentapi.dto.DepartmentSearchCriteriaDTO
    • com.ainigma100.departmentapi.utils.Utils (createPageableBasedOnPageAndSizeAndSorting)

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.

2 participants