Likes

The Likes API allows users to like and unlike blog posts, providing engagement metrics for content. Each user can like a blog post only once, and the system tracks the total number of likes for each post.

Endpoints

Like Blog Post

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

Endpoint: POST /likes/blog/{blogId}

Authentication: Required (Bearer token)

Path Parameters:

  • blogId: MongoDB ObjectId of the blog post to like

Success Response (200 OK):

{
  "likesCount": 156
}

Error Responses:

  • 400 - User has already liked this blog post

  • 401 - Unauthorized

  • 404 - Blog post not found

  • 500 - Internal server error

Example Error Response (400 Bad Request):


Unlike Blog Post

Removes a like from a specific blog post and decrements the blog's like count.

Endpoint: DELETE /likes/blog/{blogId}

Authentication: Required (Bearer token)

Path Parameters:

  • blogId: MongoDB ObjectId of the blog post to unlike

Success Response (204 No Content): Empty response body

Error Responses:

  • 400 - User has not liked this blog post previously

  • 401 - Unauthorized

  • 404 - Blog post not found

  • 500 - Internal server error

Example Error Response (400 Bad Request):

Like System Behavior

One Like Per User

  • Each user can like a blog post only once

  • Attempting to like an already-liked post returns a 400 error

  • Attempting to unlike a post that wasn't liked returns a 400 error

Like Count Tracking

  • Blog posts maintain a likesCount field

  • Count is automatically incremented/decremented with likes/unlikes

  • The like count is returned when liking a post

  • Count is visible in blog post responses

Like Persistence

  • Likes are permanently stored in the database

  • The system tracks which user liked which post

  • Like history is maintained for analytics and user experience

Code Examples

Like a Blog Post

Unlike a Blog Post

Complete Like Management Class

React Like Button Component

Bulk Like Status Check

User Experience Patterns

Optimistic Updates

For better user experience, implement optimistic updates:

Debounced Likes

To prevent rapid clicking issues:

Analytics and Insights

Like Metrics

The like system provides valuable engagement metrics:

  • Total Likes: Overall engagement across all blog posts

  • Like Rate: Percentage of viewers who like a post

  • Popular Content: Posts with highest like counts

  • User Engagement: Most active users based on likes given

Potential Analytics Endpoints

While not currently implemented, these could be valuable additions:

Best Practices

For Users

  1. Meaningful Likes: Like content that genuinely provides value

  2. Authentic Engagement: Avoid like/unlike spamming

  3. Discover Content: Use likes to bookmark interesting posts

For Developers

  1. Error Handling: Always handle like/unlike errors gracefully

  2. User Feedback: Provide clear visual feedback for like actions

  3. Performance: Implement optimistic updates for better UX

  4. Rate Limiting: Consider implementing like rate limiting

  5. Analytics: Track like patterns for content insights

Security Considerations

  1. Authentication: Always verify user authentication

  2. Duplicate Prevention: Enforce one-like-per-user rule

  3. Rate Limiting: Prevent like spam attacks

  4. Input Validation: Validate blogId parameters

Integration with Other Features

Comments Integration

Social Sharing

The Likes API provides a foundation for user engagement and content popularity metrics. While simple in design, it can be extended with additional features like comment likes, user like histories, and advanced analytics as the platform grows.

Last updated