This is a simple RESTful API for a blogging platform built using FastAPI and MongoDB.
- Clone the repository.
- Install dependencies using
pip install -r requirements.txt
. - Start the server with
uvicorn main:app --reload
. - Ensure that you have MongoDB installed locally, documentation is at MongoDB Documentation.
- Visit
/docs
endpoint to view API documentation. - Create, read, update, and delete blog posts.
- Comment on posts.
- Like/dislike posts.
Authentication is not required to access the API. All endpoints are publicly accessible.
The API returns appropriate HTTP status codes along with descriptive error messages in case of errors. Common error codes include:
- 404 Not Found: Resource not found.
- 400 Bad Request: Invalid request data or parameters.
- 500 Internal Server Error: Unexpected server error.
Postman was used to test these endpoints:
- Method: POST
- Request Body: JSON object representing the post.
- Response: JSON object with a message indicating the success or failure of the operation along with the ID of the newly created post.
curl -X POST "http://localhost:8000/posts/" -H "Content-Type: application/json" -d '{"title": "Test Post", "content": "This is a test post content", "author": "Test User"}'
- Method: GET
- Response: List of JSON objects, each representing a post.
curl -X GET "http://localhost:8000/posts/"
- Method: GET
- Response: JSON object representing the post with the specified ID.
curl -X GET "http://localhost:8000/posts/{post_id}"
- Method: PUT
- Request Body: JSON object representing the updated post.
- Response: JSON object with a message indicating the success or failure of the operation.
curl -X PUT "http://localhost:8000/posts/{post_id}" -H
- Method: DELETE
- Response: JSON object with a message indicating the success or failure of the operation.
curl -X DELETE "http://localhost:8000/posts/{post_id}"
- Method: POST
- Request Body: JSON object representing the comment.
- Response: JSON object with a message indicating the success or failure of the operation.
curl -X POST "http://localhost:8000/posts/{post_id}/comments/" -H "Content-Type: application/json" -d '{"content": "This is a test comment", "author": "Test Commenter"}'
- Method: POST
- Response: JSON object with a message indicating the success or failure of the operation.
curl -X POST "http://localhost:8000/posts/{post_id}/like/"
- Method: POST
- Response: JSON object with a message indicating the success or failure of the operation.
curl -X POST "http://localhost:8000/posts/{post_id}/dislike/"
- The API does not support user authentication or authorization. All actions are performed anonymously.
- Posts and comments are stored in a MongoDB database. Ensure that the database connection is configured correctly before using the API.
- When updating a post, both the timestamp and last_updated fields are automatically updated to the current date and time.
- The API follows RESTful principles for URL structure and HTTP methods.
- Use appropriate HTTP status codes and error messages to handle client requests effectively.
Feel free to contribute by opening issues or submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.