This is the project structure:
order_management/
├── app/
│ ├── __init__.py
│ ├── main.py
│ ├── core/
│ │ ├── __init__.py
│ │ ├── config.py # Configuration settings
│ │ ├── security.py # JWT authentication
│ │ └── database.py # Database connection
│ ├── api/
│ │ ├── __init__.py
│ │ ├── v1/
│ │ │ ├── __init__.py
│ │ │ ├── endpoints/
│ │ │ │ ├── __init__.py
│ │ │ │ └── orders.py
│ │ │ └── router.py
│ ├── models/
│ │ ├── __init__.py
│ │ └── orders.py
│ ├── schemas/
│ │ ├── __init__.py
│ │ └── orders.py
│ ├── services/
│ │ ├── __init__.py
│ │ ├── orders.py
│ │ └── email.py
│ └── workers/
│ ├── __init__.py
│ └── email_worker.py
├── tests/
│ ├── __init__.py
│ └── test_orders.py
├── requirements.txt
└── alembic
└── README.mdCreate a RESTFUL API for managing orders in an e-commerce system.
This project is a RESTful Order Management API built with FastAPI and PostgreSQL. It provides a solution for managing e-commerce orders with features like pagination, authentication, email notifications.
-
API Endpoints:
POST /orders- Create a new orderGET /orders/:id- Retrieve a specific orderGET /orders- List all orders with paginationGET /orders/search- Search orders with filtersPUT /orders/:id- Update an orderDELETE /orders/:id- Soft delete (cancel) an order
-
Database Models:
Order- Stores order information with status trackingProduct- Stores product information for price calculation
-
Authentication:
- JWT-based authentication for API security
-
Background Processing:
- Celery worker is used for sending order confirmation emails
-
Caching:
- Redis is used for improved order retrieval performance
-
Testing:
- Pytest-based test framework with database transaction management
To run this project locally:
- Clone the repository
git clone https://github.com/gpaudel10/ecom_sys.git
cd ecom_sys- Create virtual environment
python -m venv venv
source venv/bin/activate - Install dependencies
pip install -r requirements.txt- Create PostgreSQL databases
# Connect to PostgreSQL
psql -U postgres
# Create main and test databases
CREATE DATABASE order_db;
CREATE DATABASE test_order_db;- Set up environment variables by creating a
.envfile:
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/order_db
REDIS_URL=redis://localhost:6379
SECRET_KEY=your-super-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
MAIL_USERNAME= your_email
MAIL_PASSWORD=your_password
MAIL_FROM=yor_email
MAIL_PORT=587
MAIL_SERVER=smtp.gmail.com
MAIL_FROM_NAME=Order Management System
- Run database migrations
alembic upgrade head- Start the main application
uvicorn app.main:app --reload- Start Celery worker (in a separate terminal)
celery -A app.workers.email_worker worker --loglevel=infoif permission error the use this:
celery -A app.workers.email_worker:celery_app worker --loglevel=info --pool=solo- To check whether mail server is running, try sending an email to yourself from the FastMail app. In shell run
python(after activating env)
from app.workers.email_worker import send_order_confirmation
result = send_order_confirmation.delay(email="your_mail@gmail.com")
print(f"Task ID: {result.id}")You see the task ID in the output.
pytest -vOnce the application is running, access the swagger documentation at:
http://localhost:8000/docs