- Implemented PageService with full CRUD operations - Added GetPages, CreatePage, UpdatePage, DeletePage, ReorderPages methods - Cascade deletion of widgets when page is deleted - Prevention of last page deletion - Created page HTTP endpoints (GET, POST, PUT, DELETE, reorder) - HTMX-friendly HTML fragment responses - Comprehensive unit tests for service and handlers - Updated dashboard to use PageService and create default pages
5.2 KiB
Task 4.1 Implementation: PageService with CRUD Operations
Overview
Implemented the PageService with all required CRUD operations for managing user pages in the Custom Start Page application.
Implementation Details
File: internal/services/page_service.go
Methods Implemented
-
*GetPages(ctx, userID) ([]Page, error)
- Queries all pages for a user from DynamoDB
- Sorts pages by their order field
- Returns pages in display order
-
*CreatePage(ctx, userID, name) (Page, error)
- Creates a new page with a unique UUID
- Automatically assigns the next order number
- Sets timestamps (CreatedAt, UpdatedAt)
- Persists to DynamoDB Pages table
-
*CreateDefaultPage(ctx, userID) (Page, error)
- Creates the default "Home" page for new users
- Wrapper around CreatePage with "Home" as the name
- Satisfies Requirement 2.1
-
*UpdatePage(ctx, userID, pageID, name) (Page, error)
- Updates a page's name
- Updates the UpdatedAt timestamp
- Returns the updated page
- Uses DynamoDB UpdateItem with ReturnValues
-
DeletePage(ctx, userID, pageID) error
- Validates that it's not the last page (prevents deletion)
- Queries all widgets associated with the page
- Cascade deletes all widgets in batches of 25 (DynamoDB limit)
- Deletes the page itself
- Satisfies Requirements 2.5, 2.6
-
ReorderPages(ctx, userID, pageOrder) error
- Validates all page IDs exist and belong to the user
- Validates the order list length matches existing pages
- Updates each page's order field atomically
- Updates timestamps
- Satisfies Requirement 2.7
Helper Functions
- sortPagesByOrder(pages): In-place bubble sort for pages by order field
Requirements Satisfied
- ✅ Requirement 2.1: Default "Home" page creation for new users
- ✅ Requirement 2.2: Page creation with name prompt
- ✅ Requirement 2.3: Page name updates
- ✅ Requirement 2.5: Page deletion with cascade delete of widgets
- ✅ Requirement 2.6: Prevention of last page deletion
- ✅ Requirement 2.7: Page reordering with persistence
- ✅ Requirement 2.8: Immediate persistence to storage
Testing
Unit Tests (internal/services/page_service_test.go)
Comprehensive test coverage including:
- TestCreateDefaultPage: Verifies default "Home" page creation
- TestGetPages: Tests retrieving and sorting multiple pages
- TestGetPagesEmptyResult: Tests empty result handling
- TestCreatePage: Tests page creation with auto-incrementing order
- TestUpdatePage: Tests page name updates
- TestDeletePage: Tests page deletion
- TestDeleteLastPagePrevention: Validates last page deletion prevention
- TestDeletePageWithWidgets: Tests cascade deletion of widgets
- TestReorderPages: Tests page reordering
- TestReorderPagesInvalidLength: Tests validation of order list length
- TestReorderPagesInvalidPageID: Tests validation of page IDs
- TestReorderPagesPersistence: Tests persistence of reordered pages
Unit Tests Without DynamoDB (internal/services/page_service_unit_test.go)
Tests that don't require DynamoDB:
- TestSortPagesByOrder: Tests sorting function
- TestSortPagesByOrderAlreadySorted: Tests already sorted pages
- TestSortPagesByOrderReversed: Tests reversed pages
- TestSortPagesByOrderEmpty: Tests empty slice
- TestSortPagesByOrderSingle: Tests single page
Status: All unit tests pass ✅
Key Design Decisions
-
Cascade Deletion: When a page is deleted, all associated widgets are automatically deleted to maintain data integrity.
-
Last Page Protection: The service prevents deletion of the last page to ensure users always have at least one page.
-
Batch Operations: Widget deletion uses DynamoDB BatchWriteItem with batches of 25 items to handle pages with many widgets efficiently.
-
Order Management: Pages maintain an order field that determines display sequence. The CreatePage method automatically assigns the next available order number.
-
Validation: ReorderPages validates that all page IDs exist and belong to the user before making any changes.
-
Atomic Updates: Each page update is atomic, ensuring consistency even with concurrent operations.
Dependencies
github.com/aws/aws-sdk-go-v2: AWS SDK for DynamoDB operationsgithub.com/google/uuid: UUID generation for page IDsgithub.com/stretchr/testify: Testing assertions
Integration Points
- Storage Layer: Uses
internal/storage/DynamoDBClientfor all database operations - Models: Uses
internal/models.Pageandinternal/models.Widgetdata structures - Future Integration: Will be used by HTTP handlers in
internal/handlersfor page management endpoints
Notes
- Tests require DynamoDB local running on
http://localhost:8000 - AWS credentials must be configured (can be dummy credentials for local testing)
- The service is stateless and thread-safe
- All operations use context for cancellation and timeout support
Next Steps
Task 4.2 will implement the HTTP endpoints that use this service:
- GET /dashboard
- GET /pages/:id
- POST /pages
- PUT /pages/:id
- DELETE /pages/:id
- POST /pages/reorder