Feature Implementation Plan: Update Regions and Varieties Filters¶
📋 Todo Checklist¶
- [ ] Update the
/api/locations/regionsendpoint to acceptcountryIds[]. - [ ] Update the
RegionRepositoryto filter by multiple country IDs. - [ ] Update the
/api/varietiesendpoint to acceptspeciesIds[]. - [ ] Update the
VarietyRepositoryto filter by multiple species IDs. - [ ] Update OpenAPI documentation for both endpoints.
- [ ] Write unit and integration tests for the new filtering logic.
- [ ] Final Review and Testing
🔍 Analysis & Investigation¶
Codebase Structure¶
- Regions Endpoint: The logic is handled by
src/Controller/Api/LocationController.phpandsrc/Repository/RegionRepository.php. The controller is responsible for parsing request parameters, and the repository handles the database query. - Varieties Endpoint: The logic is handled by
src/Controller/Api/VarietyController.phpandsrc/Repository/VarietyRepository.php. The structure is similar to the regions endpoint.
Current Architecture¶
The API follows a consistent pattern: controllers receive HTTP requests, extract filter parameters, and pass them to a repository method that builds and executes a Doctrine query. The plan will follow this existing pattern to ensure consistency. The changes are localized to the respective controllers and repositories, making them low-risk.
Dependencies & Integration Points¶
- NelmioApiDocBundle: The OpenAPI annotations in the controllers will need to be updated to reflect the new array-based parameters (
countryIds[]andspeciesIds[]). - Doctrine: The Doctrine Query Builder will be used to add
WHERE IN (...)clauses to the repository queries.
Considerations & Challenges¶
- API Change: Replacing the single
countryparameter withcountryIds[]is a breaking change for the/api/locations/regionsendpoint. This will require coordination with the frontend team. - Parameter Validation: The incoming UUIDs in the arrays must be validated to prevent errors and ensure data integrity.
📝 Implementation Plan¶
Prerequisites¶
- No new external dependencies are required.
Step-by-Step Implementation¶
-
Update Regions Filtering
- Files to modify:
src/Controller/Api/LocationController.php,src/Repository/RegionRepository.php LocationController.phpChanges:- In
getRegions, remove the logic for the oldcountryparameter. - Add logic to check for the new
countryIdsparameter. If it exists, extract it as an array. - Validate that each item in the array is a valid UUID.
- Pass the array to the
regionRepository->findByFiltersWithPaginationmethod. - Replace the
#[OA\Parameter]forcountrywith one forcountryIds[], changing the type to an array of strings and updating the description.
- In
RegionRepository.phpChanges:- In the
applyFiltersmethod, replace the country filter logic. - If
countryIdsis present in the$filtersarray and is not empty, use$qb->andWhere('r.country IN (:countryIds)'). - Bind the array to the
:countryIdsparameter.
- In the
- Files to modify:
-
Update Varieties Filtering
- Files to modify:
src/Controller/Api/VarietyController.php,src/Repository/VarietyRepository.php VarietyController.phpChanges:- In
getVarieties, add logic to check for and extract the newspeciesIdsarray parameter. - Validate each UUID in the array.
- Pass the array to the
varietyRepository->findWithFiltersmethod. - Add a new
#[OA\Parameter]annotation forspeciesIds[]to the method's docblock.
- In
VarietyRepository.phpChanges:- In the
findWithFiltersmethod, add a new condition. - If
speciesIdsis present in the$criteriaarray and is not empty, add$qb->andWhere('v.species IN (:speciesIds)'). - Bind the array to the
:speciesIdsparameter.
- In the
- Files to modify:
Testing Strategy¶
- Unit Tests:
- Update unit tests for
RegionRepositoryto assert that the correctWHERE INclause is added whencountryIdsare provided. - Add unit tests for
VarietyRepositoryto assert that the correctWHERE INclause is added whenspeciesIdsare provided.
- Update unit tests for
- Integration Tests:
- Write an integration test for the
/api/locations/regionsendpoint that passes multiplecountryIdsand asserts that only regions from those countries are returned. - Write an integration test for the
/api/varietiesendpoint that passes multiplespeciesIdsand asserts that only varieties of those species are returned. - Ensure that the
searchparameter continues to work in conjunction with the new array-based filters for both endpoints.
- Write an integration test for the
🎯 Success Criteria¶
- The
/api/locations/regionsendpoint correctly filters regions by an array of country IDs. - The
/api/varietiesendpoint correctly filters varieties by an array of species IDs. - The OpenAPI documentation for both endpoints is updated to reflect the new parameters.
- The new filtering logic is covered by tests.