Feature Implementation Plan: Add Search to Top Endpoints¶
📋 Todo Checklist¶
- [ ] Update the
VarietyRepository'sfindTopByAvailableBeanCountmethod to accept a search term. - [ ] Update the
/api/varieties/topendpoint to accept and pass on thesearchparameter. - [ ] Update the
RegionRepository'sfindTopByAvailableBeanCountmethod to accept a search term. - [ ] Update the
/api/locations/regions/topendpoint to accept and pass on thesearchparameter. - [ ] Update OpenAPI documentation for both endpoints to include the new
searchparameter. - [ ] Write unit and integration tests for the new search 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: The logic will be updated in
src/Controller/Api/LocationController.phpandsrc/Repository/RegionRepository.php.
Current Architecture¶
The plan enhances the existing "top" endpoint architecture. The core principle of using a dedicated endpoint for aggregated data remains. The change involves adding a conditional WHERE clause to the aggregation queries before the GROUP BY and COUNT operations. This is the correct and most efficient way to implement this feature, as it filters the dataset early, ensuring that the aggregation is performed only on the relevant (searched-for) items that have available beans.
Dependencies & Integration Points¶
- NelmioApiDocBundle: The OpenAPI documentation for both
/api/varieties/topand/api/locations/regions/topwill be updated to reflect the new optionalsearchparameter. - Doctrine: The Doctrine Query Builder will be used to conditionally add the
AND WHERE name LIKE :searchclause to the existing aggregation queries.
Considerations & Challenges¶
- Query Logic: It is critical to add the search condition to the
WHEREclause, not aHAVINGclause. AWHEREclause filters rows before aggregation, which is what we need. AHAVINGclause filters groups after aggregation and would be less efficient and semantically incorrect here. - Performance: Adding a
LIKEclause can impact query performance. However, since the query is already joining on and filtering bycoffee_bean, the dataset is constrained. Ensuring thenamecolumns on thevarietyandregiontables are indexed is a good practice to mitigate any potential slowdown.
📝 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, get the optionalsearchparameter from theRequest. - Pass the
searchterm to thefindTopByAvailableBeanCountrepository method. - Update the
#[OA\Parameter]annotations to document the new optionalsearchparameter.
- In
VarietyRepository.phpChanges:- Modify the
findTopByAvailableBeanCountmethod signature to accept an optional?string $search = null. - Inside the method, add a conditional block to the query builder:
- Modify the
- Files to modify:
-
Update Top Regions Endpoint
- Files to modify:
src/Controller/Api/LocationController.php,src/Repository/RegionRepository.php LocationController.phpChanges:- In
getTopRegions, get the optionalsearchparameter from theRequest. - Pass the
searchterm to thefindTopByAvailableBeanCountrepository method. - Update the
#[OA\Parameter]annotations to document the new optionalsearchparameter.
- In
RegionRepository.phpChanges:- Modify the
findTopByAvailableBeanCountmethod signature to accept an optional?string $search = null. - Inside the method, add a conditional block to the query builder:
- Modify the
- Files to modify:
Testing Strategy¶
- Unit Tests:
- Update the unit tests for both
VarietyRepository::findTopByAvailableBeanCountandRegionRepository::findTopByAvailableBeanCount. - Test each method twice: once without the search term, and once with it, asserting that the correct
WHEREclause is added to the query.
- Update the unit tests for both
- Integration Tests:
- Update the integration tests for both
/api/varieties/topand/api/locations/regions/top. - For each endpoint, add a new test that passes a
searchparameter. - Assert that the results are filtered by the search term.
- Assert that the results are still correctly sorted by
beanCountin descending order. - Assert that a search for a term that matches a variety/region with no available beans returns an empty array.
- Update the integration tests for both
🎯 Success Criteria¶
- The
/api/varieties/topendpoint can now be filtered by an optionalsearchparameter on the variety name. - The
/api/locations/regions/topendpoint can now be filtered by an optionalsearchparameter on the region name. - In both cases, the search only considers items with at least one available coffee bean.
- The results for both endpoints remain correctly sorted by the count of available beans.
- The OpenAPI documentation for both endpoints is updated to include the new
searchparameter. - All updated functionality is covered by tests.