High-Performance E-commerce Shopping Cart Microservice
Based on FastAPI + SQLAlchemy 2.0 async architecture, supporting full shopping cart lifecycle management
English | 中文 | Русский | 한국어 | 日本語
- Development Log: Agent&Chat.md
- 🛒 Cart Management - Create, query, and clear shopping carts
- 📦 Item Operations - Add, update quantity, and remove items
- 🔄 Cart Merge - Support merging anonymous cart with user cart
- ⚡ Async Architecture - High-performance design based on async/await
- 📊 Price Snapshot - Record unit price when item is added
| Technology | Version | Purpose |
|---|---|---|
| Python | 3.10+ | Runtime Environment |
| FastAPI | 0.109+ | High-performance Async Web Framework |
| PostgreSQL | 15+ | Relational Database |
| SQLAlchemy | 2.0+ | Async ORM |
| Pydantic | v2 | Data Validation |
| Alembic | 1.13+ | Database Migration |
cart-service/
├── app/
│ ├── api/v1/endpoints/ # API Routes
│ ├── core/ # Configuration Management
│ ├── db/ # Database Connection
│ ├── models/ # ORM Models
│ ├── schemas/ # Pydantic Models
│ ├── services/ # Business Logic Layer
│ └── main.py # Application Entry
├── alembic/ # Database Migration Scripts
├── .env.example # Environment Variables Template
├── alembic.ini # Alembic Configuration
└── requirements.txt # Dependencies
cd projects/cart-service
pip install -r requirements.txtCreate PostgreSQL database:
CREATE DATABASE cart_db;Configure environment variables:
copy .env.example .env
# Edit .env file and set correct database connection infoalembic upgrade headuvicorn app.main:app --reload- Swagger Docs: http://127.0.0.1:8000/docs
- ReDoc Docs: http://127.0.0.1:8000/redoc
- Health Check: http://127.0.0.1:8000/health
| Method | Path | Description |
|---|---|---|
| GET | /api/v1/carts/{cart_id} |
Get cart details |
| POST | /api/v1/carts |
Create cart |
| POST | /api/v1/carts/{cart_id}/items |
Add item |
| PATCH | /api/v1/carts/{cart_id}/items/{item_id} |
Update item quantity |
| DELETE | /api/v1/carts/{cart_id}/items/{item_id} |
Remove item |
| DELETE | /api/v1/carts/{cart_id} |
Clear cart |
| POST | /api/v1/carts/{cart_id}/merge |
Merge carts |
| Field | Type | Description |
|---|---|---|
| id | UUID | Primary Key |
| user_id | UUID | User ID (nullable) |
| status | VARCHAR | Status |
| created_at | DATETIME | Creation Time |
| updated_at | DATETIME | Update Time |
| Field | Type | Description |
|---|---|---|
| id | UUID | Primary Key |
| cart_id | UUID | Cart ID |
| product_id | VARCHAR | Product SKU |
| quantity | INTEGER | Quantity |
| unit_price | DECIMAL | Unit Price |
| added_at | DATETIME | Added Time |
- AI Agent Development Guide - Tech stack constraints and development standards
This project is licensed under the MIT License.
Made with ❤️ using Python and FastAPI