- ID
- a6802c80-e6ab-4340-80fc-50fd453ce01d
BE-7471 Create serializers, viewsets and hook up URL routing for Cookbooks
- source :: https://hungryroot.atlassian.net/browse/BE-7471
- tags :: Hungryroot API Project
- epic :: EP-627 Exp 232: Cookbook MVP
- sprint :: HR BE Sprint 107
- status :: Status - To Do
- priority :: Priority - High
- points :: 2
Description
We need to expose the cookbooks functionality via the REST API. This task
involves creating Django Rest Framework (DRF) serializers, ViewSets, and
configuring URL routing.
Crucially, the ViewSet logic must delegate write operations (create/update) to
the existing CookbookService to ensure the upsert behavior and transactional
integrity implemented in BE-7470 are enforced via the API.
This task also includes writing integration tests for the API endpoints to
ensure the serializers, views, and service layer interact correctly.
Acceptance Criteria
Serializers:
- Create serializers for Cookbook, CookbookAuthor, CookbookSection, and CookbookItem.
- Implement nested serializers for writing (e.g., accepting items data within the Cookbook payload).
- Ensure validation logic matches model constraints.
ViewSets:
- Create a CookbookViewSet extending ModelViewSet or ViewSet.
- Override create and update methods to call CookbookService.upsert_cookbook instead of direct model serialization saving.
- Ensure read operations (list/retrieve) function as expected.
URL Routing:
- Register the CookbookViewSet with the default router.
- Ensure endpoints are accessible (e.g., /api/cookbooks/, /api/cookbooks/{slug}/).
API Testing:
- Write integration tests using APIClient.
- Test GET (list/retrieve) for correct data structure.
- Test PUT (upsert) to verify service layer invocation and transactional behavior.
- Test validation errors (e.g., missing required fields).
Documentation:
Ensure API schema (Swagger/Redoc) reflects the new endpoints correctly.
Technical Implementation Notes
Service Integration:
- Do not call serializer.save() directly for write operations.
- Nested Writes: Ensure the Serializer write methods handle the nested items and author data correctly before passing it to the Service.
- Permissions: Apply standard project permissions (e.g., IsAuthenticated or IsAdminUser) as per project standards.
Testing:
Use django.test.TestCase or pytest with APIClient.
Verify that a PUT request with nested items triggers the _update_items logic in
the service (upsert).