Today, I reached an important milestone by building the backend API for the commenting feature, which enables users to engage with posts through comments. This addition enhances user interaction, allowing for meaningful discussions on various posts.
Steps and Achievements:
-
Commenting API Design:
- Created an endpoint (
POST /comments
) that lets users submit comments on posts. This endpoint requires the post ID, the comment text, and the user's authentication token. - Implemented input validation to ensure comments are not empty and that users are authenticated before submission.
- Stored comments in the MongoDB database, linking them to the corresponding post and user ID.
- Created an endpoint (
-
Data Management:
- Utilized MongoDB's reference system to associate comment data with the relevant post. Each comment contains:
- User details (such as user ID and username).
- The actual comment text.
- A timestamp indicating when the comment was posted.
- Made sure that each comment is correctly linked to its respective post by using the post ID.
- Utilized MongoDB's reference system to associate comment data with the relevant post. Each comment contains:
-
User Authentication and Authorization:
- Implemented JWT-based authentication to restrict commenting to authenticated users. The middleware checks the user's token before processing any comments.
- Conducted authorization checks to confirm that users can only comment on posts they are permitted to access.
-
Error Management:
- Incorporated error handling for various situations, including:
- Missing or invalid input (like empty comment text).
- Unauthorized attempts to comment by users who are not authenticated.
- Comments on posts that do not exist or have been removed.
- Incorporated error handling for various situations, including:
-
API Testing:
- I utilized Postman to evaluate the comment API by sending a range of requests, both valid and invalid.
- I confirmed that the API:
- Accepts valid comments and provides a success response.
- Issues appropriate error messages for invalid or unauthorized requests.
- I made sure that the comment data is accurately stored in the database and associated with the correct post.
-
Test-Driven Development (TDD):
- I adopted a test-driven methodology to ensure the functionality performed as intended.
- I created unit tests to confirm:
- Successful creation of comments when the data is valid.
- Error responses are generated when required fields are missing or incorrect.
- Users are unable to comment without a valid token.
-
Insights and Takeaways:
- I gained significant experience in handling authentication and authorization during API development, particularly in safeguarding user actions like commenting.
- I also deepened my understanding of database relationships and how to organize data in MongoDB using references.
- The TDD approach proved beneficial in verifying that each component of the API operates correctly before proceeding to frontend integration.
Upcoming Actions:
Next, I will focus on developing the frontend for the commenting feature. This will include integrating the backend API to showcase comments and designing the user interface for users to submit comments on posts. I’ll also prioritize making the comment section engaging and responsive to improve the user experience.
Top comments (0)