Skip to content

A FastAPI-based backend for an eCommerce system that manages products and orders, ensuring data validation, stock management, and order processing.

Notifications You must be signed in to change notification settings

princemeghani/ecommerce-fast-api

Repository files navigation

eCommerce API

A FastAPI-based backend for an eCommerce system that manages products and orders, ensuring data validation, stock management, and order processing.

Features

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.

Technologies Used

  • FastAPI – Web framework
  • SQLAlchemy – ORM for database interactions
  • SQLite/PostgreSQL – Database
  • Pytest – Testing framework
  • Pydantic – Data validation
  • Docker – Containerization

Installation

1️⃣ Clone the repository

git clone https://github.com/princemeghani/ecommerce-fast-api.git
cd ecommerce-fast-api

2️⃣ Create and activate a virtual environment

python -m venv env
source env/bin/activate  # On Windows: env\Scripts\activate

3️⃣ Install dependencies

pip install -r requirements.txt

4️⃣ Configure environment variables

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

Running the API

🚀 Start the FastAPI server

uvicorn app.main:app --reload

🌍 Access the API


API Endpoints

🛍️ Product Endpoints

Method Endpoint Description
POST /products Add a new product
GET /products Retrieve all products

📦 Order Endpoints

Method Endpoint Description
POST /orders Create an order

🛠️ Utility Endpoints

Method Endpoint Description
GET / API Home
GET /health API Health Check

Running Tests

To run tests with pytest, execute:

pytest

Database Migrations (Using Alembic)

1️⃣ Initialize Alembic

alembic init alembic

2️⃣ Generate a migration

alembic revision --autogenerate -m "Initial migration"

3️⃣ Apply migrations

alembic upgrade head

Docker Setup

🛠️ Build and Run with Docker

1️⃣ Build the Docker image

docker build -t ecommerce-api .

2️⃣ Run the container

docker run -p 8000:8000 --env-file .env ecommerce-api

3️⃣ Run container in detached mode

docker run -d -p 8000:8000 --env-file .env --name ecommerce-container ecommerce-api

4️⃣ Stop the container

docker stop ecommerce-container

5️⃣ Remove the container

docker rm ecommerce-container

6️⃣ View running containers

docker ps

7️⃣ View all containers (including stopped ones)

docker ps -a

8️⃣ View logs from a running container

docker logs ecommerce-container

License

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! 🚀

About

A FastAPI-based backend for an eCommerce system that manages products and orders, ensuring data validation, stock management, and order processing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published