Feature Implementation Plan: Top Varieties by Bean Count¶
📋 Todo Checklist¶
- [ ] Create a new repository method in
VarietyRepositoryto get top varieties. - [ ] Create a new
/api/varieties/topendpoint inVarietyController. - [ ] Define the API response structure.
- [ ] Update OpenAPI documentation for the new endpoint.
- [ ] Write unit and integration tests.
- [ ] Final Review and Testing
🔍 Analysis & Investigation¶
Codebase Structure¶
- Entity: The
Varietyentity has acoffeeBeanscollection, which is the basis for the counting and aggregation. - Repository: A new method,
findTopByAvailableBeanCount, will be added tosrc/Repository/VarietyRepository.php. This keeps the complex query logic isolated in the repository layer. - Controller: A new route and method will be added to
src/Controller/Api/VarietyController.phpto expose the new functionality.
Current Architecture¶
The plan follows the existing application architecture. A new controller method will handle the API request, and a new repository method will encapsulate the database query. This maintains a clear separation of concerns. The response will be a custom structure containing the variety data and the associated bean count, which is a common pattern for aggregated data.
Dependencies & Integration Points¶
- NelmioApiDocBundle: Will be used to document the new
/api/varieties/topendpoint, including itslimitparameter and response structure. - Doctrine: The Doctrine Query Builder will be used to construct the aggregation query, which involves a
JOIN,WHERE,GROUP BY,ORDER BY, andLIMIT.
Considerations & Challenges¶
- Performance: The aggregation query can be expensive on large datasets. However, since it will be limited to a small number of results (e.g., top 5 or 10), the performance impact should be minimal. Caching the result of this endpoint could be a future optimization if needed.
- Response Structure: The API will return not just the
Varietyentities but also thebeanCount. This requires a custom query result and a specific JSON structure, which will be defined in the OpenAPI documentation.
📝 Implementation Plan¶
Prerequisites¶
- No new external dependencies are required.
Step-by-Step Implementation¶
-
Create New Repository Method
- Files to modify:
src/Repository/VarietyRepository.php - Changes needed:
- Create a new public method
findTopByAvailableBeanCount(int $limit = 10): array. - Use the Query Builder to construct the following query:
- Create a new public method
- Files to modify:
-
Create New Controller Endpoint
- Files to modify:
src/Controller/Api/VarietyController.php - Changes needed:
- Add a new public method
getTopVarieties(Request $request): JsonResponse. - Add the
#[Route('/varieties/top', name: 'varieties_top', methods: ['GET'])]annotation. - In the method, get the
limitfrom the request query parameters, with a default value of 10 and a maximum of 50. - Call the new
varietyRepository->findTopByAvailableBeanCount($limit)method. - Format the response to be an array of objects, each containing the variety data and the
beanCount. - Add
#[OA\Get]and#[OA\Parameter]annotations to document the endpoint, itslimitparameter, and the response structure. The response should be documented as an array of objects withvarietyandbeanCountproperties.
- Add a new public method
- Files to modify:
Testing Strategy¶
- Unit Tests:
- Write a unit test for the
VarietyRepository::findTopByAvailableBeanCountmethod. This will require setting up test data with varieties and associated coffee beans (both available and unavailable) and asserting that the method returns the correct varieties in the correct order with the correct counts.
- Write a unit test for the
- Integration Tests:
- Write an integration test for the
/api/varieties/topendpoint. - Test the default limit.
- Test with a custom
limitparameter. - Assert that the response structure is correct and the data is as expected based on the test database state.
- Write an integration test for the
🎯 Success Criteria¶
- A
GETrequest to/api/varieties/topreturns a JSON array of the top varieties. - Each object in the array contains the full variety data and a
beanCountfield. - The list is correctly sorted by the number of available coffee beans in descending order.
- The
limitparameter correctly controls the number of returned varieties. - The new endpoint is documented in the OpenAPI specification.
- The new functionality is covered by tests.