The Collections API allows users to create named collections and organize posts within them. This provides a way for users to bookmark and categorize posts for quick access.
All endpoints require authentication. Include the Authorization: Bearer <token> header in your requests.
GET /api/collections
Returns a list of all collections created by the authenticated user.
Response:
[
{
"id": 1,
"user_id": 123,
"name": "My Favorites",
"description": "Posts I want to revisit",
"created_at": "2024-01-15T10:30:00.000Z",
"updated_at": "2024-01-15T10:30:00.000Z"
}
]POST /api/collections
Creates a new collection for the authenticated user.
Request Body:
{
"name": "My Favorites",
"description": "Posts I want to revisit" // optional
}Response:
{
"id": 1,
"user_id": 123,
"name": "My Favorites",
"description": "Posts I want to revisit",
"created_at": "2024-01-15T10:30:00.000Z",
"updated_at": "2024-01-15T10:30:00.000Z"
}GET /api/collections/:collection_name
Returns details about a specific collection.
Response:
{
"id": 1,
"user_id": 123,
"name": "My Favorites",
"description": "Posts I want to revisit",
"created_at": "2024-01-15T10:30:00.000Z",
"updated_at": "2024-01-15T10:30:00.000Z"
}GET /api/collections/:collection_name/posts
Returns all posts in a specific collection with their details.
Response:
{
"id": 1,
"user_id": 123,
"name": "My Favorites",
"description": "Posts I want to revisit",
"created_at": "2024-01-15T10:30:00.000Z",
"updated_at": "2024-01-15T10:30:00.000Z",
"posts": [
{
"id": 456,
"title": "Interesting Post",
"content": "This is the post content...",
"rating": 15,
"created_at": "2024-01-14T09:00:00.000Z",
"added_at": "2024-01-15T10:35:00.000Z"
}
]
}PATCH /api/collections/:collection_name
Updates a collection's name and/or description.
Request Body:
{
"name": "Updated Name", // optional
"description": "Updated description" // optional
}Response:
{
"id": 1,
"user_id": 123,
"name": "Updated Name",
"description": "Updated description",
"created_at": "2024-01-15T10:30:00.000Z",
"updated_at": "2024-01-15T11:00:00.000Z"
}DELETE /api/collections/:collection_name
Deletes a collection and removes all posts from it.
Response:
{
"message": "Collection \"My Favorites\" deleted successfully"
}POST /api/collections/:collection_name/posts
Adds a post to a collection.
Request Body:
{
"post_id": 456
}Response:
{
"message": "Post 456 added to collection \"My Favorites\""
}DELETE /api/collections/:collection_name/posts/:post_id
Removes a post from a collection.
Response:
{
"message": "Post 456 removed from collection \"My Favorites\""
}{
"errors": [
{
"property": "name",
"constraints": {
"minLength": "name must be longer than or equal to 1 characters"
}
}
]
}{
"error": "Invalid or expired access token"
}{
"error": "Collection \"NonExistent\" not found"
}{
"error": "Collection with name \"My Favorites\" already exists"
}The implementation utilizes two tables:
id(INT, PRIMARY KEY, AUTO_INCREMENT)user_id(INT, FOREIGN KEY to user.id)name(VARCHAR(100), NOT NULL)description(VARCHAR(255), NULLABLE)created_at(DATETIME)updated_at(DATETIME)- Unique constraint on (user_id, name)
collection_id(INT, FOREIGN KEY to collection.id)post_id(INT, FOREIGN KEY to post.id)added_at(DATETIME)- Primary key on (collection_id, post_id)
# Create collection
curl -X POST http://localhost:3000/api/collections \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "Tech Articles", "description": "Interesting tech posts"}'
# Add post to collection
curl -X POST http://localhost:3000/api/collections/Tech%20Articles/posts \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"post_id": 123}'
# Get posts in collection
curl -X GET http://localhost:3000/api/collections/Tech%20Articles/posts \
-H "Authorization: Bearer YOUR_TOKEN"