Feature Implementation Plan: Top Aggregated Endpoints for Varieties and Regions¶
📋 Todo Checklist¶
- [ ] Update the
VarietyRepositoryto add aspeciesIds[]filter to the top varieties query. - [ ] Update the
/api/varieties/topendpoint to accept thespeciesIds[]filter. - [ ] Create a new repository method in
RegionRepositoryto get top regions by bean count, with acountryIds[]filter. - [ ] Create a new
/api/locations/regions/topendpoint. - [ ] Update OpenAPI documentation for both endpoints.
- [ ] Write unit and integration tests for all new and updated functionality.
- [ ] Final Review and Testing
🔍 Analysis & Investigation¶
Codebase Structure¶
- Varieties: The logic will be updated in
src/Controller/Api/VarietyController.phpandsrc/Repository/VarietyRepository.php. - Regions: New logic will be added to
src/Controller/Api/LocationController.phpandsrc/Repository/RegionRepository.php.
Current Architecture¶
The plan builds on the previously designed architecture for "top" endpoints. It uses dedicated endpoints (/top) to return aggregated data, which is a clean and maintainable approach. The core logic for querying and aggregation is kept within the repository layer, while the controller handles API requests and responses. The addition of optional filters makes these endpoints more flexible and powerful.
Dependencies & Integration Points¶
- NelmioApiDocBundle: The OpenAPI documentation for both
/api/varieties/topand the new/api/locations/regions/topwill be updated to reflect the new optional filter parameters (speciesIds[]andcountryIds[]). - Doctrine: The Doctrine Query Builder will be used to add
WHERE IN (...)clauses to the aggregation queries when the filters are provided.
Considerations & Challenges¶
- Query Complexity: The repository methods will now have conditional logic to add the
WHERE INclause only when the filter parameters are present. This is a minor increase in complexity but is a standard pattern. - Performance: As before, these aggregation queries can be intensive. The filters will actually help performance by reducing the dataset before the
GROUP BYoperation. Caching remains a viable future optimization.
📝 Implementation Plan¶
Prerequisites¶
- No new external dependencies are required.
Step-by-Step Implementation¶
-
Update Top Varieties Endpoint
- Files to modify:
src/Controller/Api/VarietyController.php,src/Repository/VarietyRepository.php VarietyController.phpChanges:- In
getTopVarieties, add logic to check for and extract the newspeciesIds[]array parameter. - Validate the UUIDs in the array.
- Pass the
speciesIdsarray to thefindTopByAvailableBeanCountrepository method. - Update the
#[OA\Parameter]annotations to document the new optionalspeciesIds[]filter.
- In
VarietyRepository.phpChanges:- Modify the
findTopByAvailableBeanCountmethod signature to accept an optionalarray $speciesIds = []. - Inside the method, if the
$speciesIdsarray is not empty, add the following condition to the query builder:
- Modify the
- Files to modify:
-
Create Top Regions Endpoint
- Files to modify:
src/Controller/Api/LocationController.php,src/Repository/RegionRepository.php LocationController.phpChanges:- Create a new public method
getTopRegions(Request $request): JsonResponse. - Add the
#[Route('/regions/top', name: 'regions_top', methods: ['GET'])]annotation. - In the method, get the
limitand optionalcountryIds[]from the request. - Call a new
regionRepository->findTopByAvailableBeanCount($limit, $countryIds)method. - Format the response to be an array of objects, each containing the region data and the
beanCount. - Add
#[OA\Get]and#[OA\Parameter]annotations to document the new endpoint and its parameters.
- Create a new public method
RegionRepository.phpChanges:- Create a new public method
findTopByAvailableBeanCount(int $limit = 10, array $countryIds = []): array. - Use the Query Builder to construct a query similar to the top varieties one.
- If the
$countryIdsarray is not empty, add aWHEREclause to filter the regions:
- Create a new public method
- Files to modify:
Testing Strategy¶
- Unit Tests:
- Update the unit test for
VarietyRepository::findTopByAvailableBeanCountto test thespeciesIdsfilter. - Write a new unit test for
RegionRepository::findTopByAvailableBeanCount, testing both the basic aggregation and thecountryIdsfilter.
- Update the unit test for
- Integration Tests:
- Update the integration test for
/api/varieties/topto pass thespeciesIdsfilter and assert the results are correct. - Write a new integration test for
/api/locations/regions/top. Test the default case, thelimitparameter, and thecountryIdsfilter.
- Update the integration test for
🎯 Success Criteria¶
- The
/api/varieties/topendpoint can now be filtered byspeciesIds[]. - A new
/api/locations/regions/topendpoint exists and returns the top regions by available bean count. - The
/api/locations/regions/topendpoint can be filtered bycountryIds[]. - Both endpoints are documented in the OpenAPI specification.
- All new and updated functionality is covered by tests.