This report details the implementation of a comprehensive backend development task focused on creating a RESTful API for a blog application. The API handles CRUD operations for posts, comments, and users, incorporating various aspects such as database design, API creation, authentication, and deployment. Additionally, the project integrates Celery and Redis for asynchronous email functionality. The following sections outline the completed work, addressing each requirement and providing insights into the implementation process.
The primary objectives of this project were to:
-
Create a RESTful API for a simple blog application.
-
Implement user authentication and authorization.
-
Integrate asynchronous email functionality using Celery and Redis.
-
Ensure the application is ready for deployment.
- User Registration and Login:
-
Implemented endpoints for user registration and login.
-
Utilized Django's authentication system and JWT for secure user authentication.
-
Integrated Celery and Redis to send registration and login notification emails asynchronously.
- CRUD Operations for Posts and Comments:
-
Developed endpoints for creating, reading, updating, and deleting posts and comments.
-
Implemented permissions to ensure only authenticated users can create and manage their own posts and comments.
- Viewing Posts and Comments:
- Created endpoints to allow users to view posts and comments created by other users.
The database schema was designed based on the specified entities:
-
User:
id
,username
,email
,password
-
Post:
id
,title
,content
,authorId
,createdAt
,updatedAt
-
Comment:
id
,postId
,authorId
,content
,createdAt
,updatedAt
The schema was normalized to ensure efficient data storage and retrieval. The MySQL relational database was used, with Django's ORM handling the database interactions.
The API endpoints were developed as follows:
-
POST /api/users/register
- Register a new user -
POST /api/users/login
- Authenticate user and return a token -
GET /api/users/profile
- Get user profile (Authenticated)
-
GET /api/posts
- Retrieve all posts (Paginated) -
GET /api/posts/:id
- Retrieve a single post by ID -
POST /api/posts
- Create a new post (Authenticated) -
PUT /api/posts/:id
- Update a post by ID (Authenticated & Author only) -
DELETE /api/posts/:id
- Delete a post by ID (Authenticated & Author only)
-
GET /api/posts/:postId/comments
- Retrieve all comments for a post (Paginated) -
POST /api/posts/:postId/comments
- Create a new comment on a post (Authenticated) -
PUT /api/comments/:id
- Update a comment by ID (Authenticated & Author only) -
DELETE /api/comments/:id
- Delete a comment by ID (Authenticated & Author only)
-
Implemented Celery for background task processing and Redis as the message broker.
-
Used Celery to send registration and login notification emails asynchronously:
-
On user registration, a welcome email is sent.
-
On login, a notification email is sent with details like IP address, device type, and login time.
-
Improved performance by offloading email-sending tasks from the main API flow to Celery workers.
-
Welcome Email: Sent after successful user registration to welcome new users.
-
Login Notification Email: Sent after user login, including details such as IP address, device type, and login time for security purposes.
-
Reduced response times for user registration and login requests.
-
Scalability to handle a high volume of emails.
-
Reliability through Redis as a fault-tolerant message broker.
-
Implemented JWT for user authentication to secure the endpoints.
-
Protected routes that require authentication, ensuring only authorized users can access certain operations.
-
Implemented role-based access to ensure users can only update or delete their own posts and comments.
-
Implemented pagination for listing posts and comments.
-
Added search functionality to allow users to find posts based on keywords.
-
Set up a CI/CD pipeline using GitHub Actions for automated testing and deployment.
-
Configured the pipeline to run tests on every push to the repository, ensuring code quality and functionality.
-
Deployed the application using Docker for containerization, with a
Dockerfile
anddocker-compose.yml
provided for easy setup.
-
Dockerized the application for easy deployment.
-
Prepared the application for deployment on Digital Ocean Droplet.
-
Included necessary configurations for environment variables and database connections.
-
Implemented unit and integration tests for critical parts of the application using Django's testing framework.
-
Ensured tests cover user registration, authentication, CRUD operations for posts and comments, and permissions.
-
Automated tests were integrated into the CI/CD pipeline to ensure reliability and prevent regressions.
-
Docker
-
Docker Compose
- Clone the repository:
git clone https://github.com/samuelogboye/blogapi.git
cd blogapi
-
Create a
.env
file based on.env.example
and update your database credentials. -
Build and start the containers:
docker compose up --build
- Access the API at
http://localhost:8000/api/docs
Run the tests with:
docker compose exec web python manage.py test
Postman collection for API documentation and sample requests.
The application is ready to be deployed on a cloud provider
The project has been committed to a GitHub repository. The repository includes:
-
Complete source code.
-
Instructions for setting up and running the application in the
README.md
file. -
Sample data and API documentation (e.g., Postman collection).
This report outlines the successful implementation of a comprehensive backend development task. The project demonstrates proficiency in database design, API creation, authentication, authorization, deployment, and asynchronous task handling using Celery and Redis. The application is well-prepared for deployment and provides a robust foundation for further development and enhancements.