Feature Implementation Plan: Add Pagination to Varieties and Processing Methods¶
📋 Todo Checklist¶
- [ ] Add pagination to the
/api/varietiesendpoint. - [ ] Add pagination to the
/api/processing-methodsendpoint. - [ ] Update the
VarietyRepositoryto support pagination. - [ ] Update the
ProcessingMethodRepositoryto support pagination. - [ ] Update OpenAPI documentation for both endpoints.
- [ ] Write unit and integration tests for the new pagination logic.
- [ ] Final Review and Testing
🔍 Analysis & Investigation¶
Codebase Structure¶
- Endpoints: The plan targets two endpoints:
/api/varieties(inVarietyController) and/api/processing-methods(inProcessingMethodController). - Repositories: The corresponding repositories (
VarietyRepositoryandProcessingMethodRepository) will be modified to handle paginated queries.
Current Architecture¶
Both endpoints currently fetch all results from their respective repository methods and return them as a flat JSON array. The plan is to introduce a consistent pagination structure that is already in use in other parts of the API (e.g., /api/locations/regions). This involves:
1. Accepting page and limit query parameters in the controller.
2. Passing limit and offset to the repository.
3. Using Doctrine's Paginator in the repository to fetch the data.
4. Wrapping the response in a structure that includes the items and pagination metadata.
Dependencies & Integration Points¶
- NelmioApiDocBundle: The OpenAPI annotations for both endpoints will be updated to document the new
pageandlimitparameters and the new paginated response structure. - Doctrine ORM: Doctrine's
Paginatorclass will be used to implement the pagination logic efficiently.
Considerations & Challenges¶
- Breaking Change: This is a breaking change for both endpoints. The response format will change from a simple array
[]to an object{"items": [], "pagination": {}}. This will require coordination with the frontend team. - Consistency: The implementation should be consistent with other paginated endpoints in the application to ensure a predictable API.
📝 Implementation Plan¶
Prerequisites¶
- No new external dependencies are required.
Step-by-Step Implementation¶
-
Paginate the Varieties Endpoint
- Files to modify:
src/Controller/Api/VarietyController.php,src/Repository/VarietyRepository.php VarietyController.phpChanges:- In
getVarieties, acceptpageandlimitquery parameters with default values. - Calculate the
offset. - Call a new repository method
findWithFiltersAndPagination, passing the filters, limit, and offset. - Construct the paginated JSON response, wrapping the results in an
itemsarray and adding apaginationobject. - Update the
#[OA\Get]and#[OA\Parameter]annotations to reflect the new parameters and response structure.
- In
VarietyRepository.phpChanges:- Create a new method
findWithFiltersAndPagination(array $criteria = [], int $limit = 20, int $offset = 0): Paginator. - Move the query builder logic from the existing
findWithFiltersmethod into this new one. - Apply the
setMaxResults($limit)andsetFirstResult($offset)methods to the query builder. - Return a new
Paginatorobject. - Keep the old
findWithFiltersmethod for internal use or deprecate it if it's no longer needed.
- Create a new method
- Files to modify:
-
Paginate the Processing Methods Endpoint
- Files to modify:
src/Controller/Api/ProcessingMethodController.php,src/Repository/ProcessingMethodRepository.php ProcessingMethodController.phpChanges:- In
getProcessingMethods, acceptpageandlimitparameters. - Calculate the
offset. - Call a new repository method
findByFiltersWithPagination, passing the search term, limit, and offset. - Construct the paginated JSON response.
- Update the
#[OA\Get]and#[OA\Parameter]annotations.
- In
ProcessingMethodRepository.phpChanges:- Create a new method
findByFiltersWithPagination(?string $search = null, int $limit = 20, int $offset = 0): Paginator. - Create a query builder that can handle an optional search term.
- Apply pagination using
setMaxResultsandsetFirstResult. - Return a new
Paginatorobject.
- Create a new method
- Files to modify:
Testing Strategy¶
- Unit Tests:
- Write unit tests for the new
findWithFiltersAndPaginationmethod inVarietyRepository. - Write unit tests for the new
findByFiltersWithPaginationmethod inProcessingMethodRepository.
- Write unit tests for the new
- Integration Tests:
- Update the integration tests for
/api/varietiesto check for the new paginated response structure. Test thepageandlimitparameters. - Write new integration tests for
/api/processing-methodsto verify the pagination and the new response structure. - For both endpoints, ensure that existing filters (like
search) continue to work correctly with pagination.
- Update the integration tests for
🎯 Success Criteria¶
- The
/api/varietiesendpoint returns a paginated list of varieties. - The
/api/processing-methodsendpoint returns a paginated list of processing methods. - Both endpoints accept
pageandlimitquery parameters. - The response format for both endpoints is
{"items": [...], "pagination": {...}}. - The OpenAPI documentation is updated for both endpoints.
- All changes are covered by tests.