Comments

The Comments API allows users to add, retrieve, and manage comments on blog posts. All authenticated users can create and view comments, while comment deletion is restricted to the comment author or administrators.

Endpoints

Create Comment

Adds a new comment to a specific blog post and increments the blog's comment count.

Endpoint: POST /comments/

Authentication: Required (Bearer token)

Request Body:

{
  "blogId": "60b7c8b4f9b3c12345678903",
  "content": "Great article! The examples really helped me understand the concepts better."
}

Success Response (201 Created):

{
  "comment": {
    "_id": "60b7c8b4f9b3c12345678905",
    "blogId": "60b7c8b4f9b3c12345678903",
    "userId": "60b7c8b4f9b3c12345678901",
    "content": "Great article! The examples really helped me understand the concepts better.",
    "likesCount": 0,
    "createdAt": "2024-01-21T14:30:00.000Z",
    "updatedAt": "2024-01-21T14:30:00.000Z"
  }
}

Error Responses:

  • 400 - Validation errors (missing blogId, content too long, etc.)

  • 401 - Unauthorized

  • 404 - Blog post not found

  • 500 - Internal server error

Validation Rules:

  • blogId: Must be a valid MongoDB ObjectId

  • content: Required, maximum 1000 characters

  • userId: Automatically set from authenticated user


Get Comments by Blog

Retrieves all comments associated with a specific blog post, ordered by creation date.

Endpoint: GET /comments/blog/{blogId}

Authentication: Required (Bearer token)

Path Parameters:

  • blogId: MongoDB ObjectId of the blog post

Success Response (200 OK):

Error Responses:

  • 401 - Unauthorized

  • 404 - Blog post not found

  • 500 - Internal server error

Notes:

  • Comments are returned in chronological order (oldest first)

  • No pagination is currently implemented for comments

  • Returns empty array if no comments exist


Delete Comment

Deletes a specific comment. Only the comment author or administrators can delete comments. Decrements the blog's comment count.

Endpoint: DELETE /comments/{commentId}

Authentication: Required (Bearer token)

Path Parameters:

  • commentId: MongoDB ObjectId of the comment to delete

Success Response (204 No Content): Empty response body

Error Responses:

  • 401 - Unauthorized

  • 403 - Forbidden (user is not the comment author or admin)

  • 404 - Comment not found

  • 500 - Internal server error

Authorization Logic:

  • Comment authors can delete their own comments

  • Administrators can delete any comment

  • Other users receive a 403 Forbidden error

Comment Lifecycle

1. Creation Flow

2. Retrieval

  • Comments are fetched when viewing a blog post

  • All comments for a blog are returned in a single request

  • Comments include creation timestamps and like counts

3. Deletion Flow

Code Examples

Create a Comment

Get Comments for a Blog Post

Delete a Comment

Complete Comment Management Class

React Component Example

Content Guidelines

Comment Content

  • Length: Maximum 1000 characters

  • Format: Plain text (HTML is not processed)

  • Language: Should be respectful and constructive

  • Links: URLs are not automatically converted to clickable links

Moderation

  • Comments are not pre-moderated

  • Users can delete their own comments

  • Admins can delete any comment

  • Consider implementing reporting functionality for inappropriate content

Best Practices

For Users

  1. Be Constructive: Provide helpful, relevant feedback

  2. Stay On Topic: Keep comments related to the blog post

  3. Be Respectful: Maintain a professional and courteous tone

  4. Check Length: Ensure comments are under 1000 characters

For Developers

  1. Input Validation: Always validate comment content on both client and server

  2. Rate Limiting: Consider implementing comment rate limiting to prevent spam

  3. Error Handling: Provide clear error messages for failed operations

  4. Real-time Updates: Consider WebSocket integration for real-time comment updates

  5. Pagination: Implement pagination for blogs with many comments

Security Considerations

  1. XSS Prevention: Sanitize comment content before display

  2. Authorization: Always verify user permissions before allowing operations

  3. Content Filtering: Consider implementing profanity filters

  4. Spam Detection: Implement basic spam detection mechanisms

Future Enhancements

The current comment system can be extended with:

  1. Comment Likes: Allow users to like/unlike comments

  2. Nested Replies: Support threaded comment conversations

  3. Edit Comments: Allow users to edit their comments within a time limit

  4. Comment Notifications: Notify blog authors of new comments

  5. Rich Text: Support for basic markdown formatting

  6. Comment Search: Search functionality within comments

  7. User Mentions: @ mention functionality for other users

Last updated