A FastAPI-based backend for an eCommerce system that manages products and orders, ensuring data validation, stock management, and order processing.
✅ Product Management – Add and retrieve products.
✅ Order Management – Create orders with validation for stock and pricing.
✅ Validation & Error Handling – Ensures data integrity for product pricing, quantity, and stock availability.
✅ Database Management – Uses SQLAlchemy with SQLite/PostgreSQL.
✅ Health Check API – Provides a /health
endpoint for API monitoring.
✅ Automated Testing – Includes unit and integration tests with pytest.
- FastAPI – Web framework
- SQLAlchemy – ORM for database interactions
- SQLite/PostgreSQL – Database
- Pytest – Testing framework
- Pydantic – Data validation
- Docker – Containerization
git clone https://github.com/princemeghani/ecommerce-fast-api.git
cd ecommerce-fast-api
python -m venv env
source env/bin/activate # On Windows: env\Scripts\activate
pip install -r requirements.txt
Create a .env
file and add database credentials:
DATABASE_URL=sqlite:///./database.db
TEST_DATABASE_URL=sqlite:///./test_database.db
For PostgreSQL:
DATABASE_URL=postgresql://user:password@localhost/ecommerce_db
TEST_DATABASE_URL=postgresql://user:password@localhost/test_ecommerce_db
uvicorn app.main:app --reload
- Docs: http://127.0.0.1:8000/docs
- Redoc: http://127.0.0.1:8000/redoc
Method | Endpoint | Description |
---|---|---|
POST |
/products |
Add a new product |
GET |
/products |
Retrieve all products |
Method | Endpoint | Description |
---|---|---|
POST |
/orders |
Create an order |
Method | Endpoint | Description |
---|---|---|
GET |
/ |
API Home |
GET |
/health |
API Health Check |
To run tests with pytest, execute:
pytest
1️⃣ Initialize Alembic
alembic init alembic
2️⃣ Generate a migration
alembic revision --autogenerate -m "Initial migration"
3️⃣ Apply migrations
alembic upgrade head
docker build -t ecommerce-api .
docker run -p 8000:8000 --env-file .env ecommerce-api
docker run -d -p 8000:8000 --env-file .env --name ecommerce-container ecommerce-api
docker stop ecommerce-container
docker rm ecommerce-container
docker ps
docker ps -a
docker logs ecommerce-container
This project is licensed under the MIT License.
This README is now structured, clear, and includes Docker commands for easy setup. Let me know if you need further modifications! 🚀