This project is a RESTful API for managing employee data, built with FastAPI and MongoDB. It provides a full suite of CRUD operations, advanced querying capabilities, and JWT-based authentication.
- FastAPI: A modern, fast (high-performance) web framework for building APIs.
- MongoDB: A NoSQL database for storing employee documents.
- Pydantic V2: For robust data validation and settings management.
- JWT Authentication: Secure endpoints for creating, updating, and deleting data.
- Advanced Querying:
- List and paginate employees by department.
- Search for employees by their skills.
- Aggregate data to find the average salary per department.
- Full Test Suite: Unit and integration tests written with
pytest
. - Docker Support: Instructions for running a local MongoDB instance with Docker.
Follow these instructions to get the project up and running on your local machine.
- Python 3.10+
- Pip (Python package installer)
- Docker (for running MongoDB)
git clone <repository_url>
cd <repository_directory>
It's recommended to use a virtual environment to manage project dependencies.
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate
Install all the required packages from requirements.txt
.
pip install -r requirements.txt
The application uses a .env
file to manage configuration. Create one by copying the example file.
cp .env.example .env
You can modify the .env
file if needed, but the defaults are configured for a standard local setup. Remember to change SECRET_KEY
for a production environment.
The easiest way to run a local MongoDB instance is with Docker.
docker run -d -p 27017:27017 --name assessment-mongo mongo
This command will download the official MongoDB image and start a container in the background.
Use uvicorn
to run the FastAPI application.
uvicorn app.main:app --reload --port 8000
The API will now be available at http://127.0.0.1:8000
. You can access the interactive Swagger documentation at http://127.0.0.1:8000/docs
.
To run the test suite, ensure you have followed the setup steps and have the MongoDB container running.
python -m pytest
Here are some curl
examples for interacting with the API.
First, you need to get a JWT token to access the protected routes. The test user credentials are:
- Username:
testuser
- Password:
testpassword
Get a Token
curl -X POST "http://127.0.0.1:8000/auth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=testuser&password=testpassword"
The response will contain your access_token
.
{
"access_token": "your_jwt_token_here",
"token_type": "bearer"
}
Export the token to a variable for easier use in subsequent requests:
export TOKEN="your_jwt_token_here"
1. Create Employee (POST /employees
)
Protected Route
curl -X POST "http://127.0.0.1:8000/employees/" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"employee_id": "E123",
"name": "John Doe",
"department": "Engineering",
"salary": 75000,
"joining_date": "2023-01-15",
"skills": ["Python", "MongoDB", "APIs"]
}'
2. Get Employee by ID (GET /employees/{employee_id}
)
curl -X GET "http://127.0.0.1:8000/employees/E123"
3. Update Employee (PUT /employees/{employee_id}
)
Protected Route
curl -X PUT "http://127.0.0.1:8000/employees/E123" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"salary": 80000,
"skills": ["Python", "MongoDB", "FastAPI"]
}'
4. Delete Employee (DELETE /employees/{employee_id}
)
Protected Route
curl -X DELETE "http://127.0.0.1:8000/employees/E123" \
-H "Authorization: Bearer $TOKEN"
5. List Employees by Department (GET /employees
)
With pagination and filtering.
# Filter by department and get the first page
curl -X GET "http://127.0.0.1:8000/employees/?department=Engineering&page=1&page_size=5"
6. Average Salary by Department (GET /employees/avg-salary
)
curl -X GET "http://127.0.0.1:8000/employees/avg-salary"
7. Search Employees by Skill (GET /employees/search
)
curl -X GET "http://127.0.0.1:8000/employees/search?skill=Python"