Task for the CodeAcademy course Create a Back-End App with JavaScript
This PR implements the missing comment system for The Scoop web application, allowing users to create, edit, delete, and vote on comments. Additionally, it adds YAML-based database persistence to maintain data across server restarts.
- Database Structure: Added
commentsobject andnextCommentIdcounter to the database - Routes: Implemented 5 new comment routes:
POST /comments- Create new commentsPUT /comments/:id- Update comment bodyDELETE /comments/:id- Delete comments and clean up referencesPUT /comments/:id/upvote- Upvote commentsPUT /comments/:id/downvote- Downvote comments
- Handler Functions: Created all corresponding route handlers following the existing article pattern, including proper validation, error handling, and bidirectional linking between comments, users, and articles
- saveDatabase(): Saves the current database state to
config.ymlafter each request - loadDatabase(): Loads database from
config.ymlon server startup with error handling for missing files - Uses the
figglibrary for YAML file operations
- All tests pass successfully using the provided test suite (
npm test) - Verified edge cases including missing IDs, invalid usernames, non-existent articles, and proper error handling
- Confirmed data persistence works correctly across server restarts
- All comment routes follow the same patterns as existing article routes for consistency
- Proper cleanup of comment references in both user and article objects when comments are deleted
- Error handling includes appropriate HTTP status codes (400, 404, 201, 200, 204)
- YAML persistence includes try/catch error handling to gracefully handle missing config files on first run