
- Created FastAPI application with SQLite database - Implemented cart management (create, get, add items, remove items, clear) - Added checkout functionality with payment processing simulation - Set up database models using SQLAlchemy with Cart and CartItem tables - Configured Alembic for database migrations - Added comprehensive API documentation and examples - Included health endpoint and CORS support - Formatted code with ruff linting
Cart and Checkout System
A FastAPI-based REST API for managing shopping carts and processing checkouts.
Features
- Create and manage shopping carts
- Add, update, and remove items from carts
- Checkout functionality with payment processing simulation
- SQLite database with SQLAlchemy ORM
- Alembic database migrations
- Automatic API documentation with FastAPI
- CORS support for cross-origin requests
Installation
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the application:
uvicorn main:app --reload
The application will be available at http://localhost:8000
.
API Documentation
Once the application is running, you can access:
- Interactive API documentation:
http://localhost:8000/docs
- Alternative documentation:
http://localhost:8000/redoc
- OpenAPI JSON schema:
http://localhost:8000/openapi.json
API Endpoints
Cart Management
POST /api/v1/carts/
- Create a new cartGET /api/v1/carts/{cart_id}
- Get cart by IDGET /api/v1/carts/user/{user_id}
- Get all carts for a userDELETE /api/v1/carts/{cart_id}
- Clear all items from a cart
Cart Items
POST /api/v1/carts/{cart_id}/items/
- Add item to cartPUT /api/v1/carts/{cart_id}/items/{item_id}
- Update item quantityDELETE /api/v1/carts/{cart_id}/items/{item_id}
- Remove item from cart
Checkout
POST /api/v1/checkout/
- Process checkout
System
GET /
- Service informationGET /health
- Health check endpoint
Example Usage
Create a Cart
curl -X POST "http://localhost:8000/api/v1/carts/" \
-H "Content-Type: application/json" \
-d '{"user_id": "user123"}'
Add Item to Cart
curl -X POST "http://localhost:8000/api/v1/carts/1/items/" \
-H "Content-Type: application/json" \
-d '{
"product_id": "prod123",
"product_name": "Example Product",
"price": 29.99,
"quantity": 2
}'
Checkout
curl -X POST "http://localhost:8000/api/v1/checkout/" \
-H "Content-Type: application/json" \
-d '{
"cart_id": 1,
"payment_method": "credit_card",
"billing_address": {
"street": "123 Main St",
"city": "Anytown",
"state": "ST",
"zip": "12345"
}
}'
Database Schema
Carts Table
id
- Primary keyuser_id
- User identifierstatus
- Cart status (active, checked_out, abandoned)created_at
- Creation timestampupdated_at
- Last update timestamp
Cart Items Table
id
- Primary keycart_id
- Foreign key to carts tableproduct_id
- Product identifierproduct_name
- Product nameprice
- Item pricequantity
- Item quantity
Development
Database Migrations
To create a new migration:
alembic revision -m "Description of changes"
To apply migrations:
alembic upgrade head
To rollback migrations:
alembic downgrade -1
Code Linting
The project uses Ruff for code formatting and linting:
ruff check .
ruff format .
Architecture
- FastAPI - Modern Python web framework
- SQLAlchemy - SQL toolkit and ORM
- SQLite - Lightweight database
- Alembic - Database migration tool
- Pydantic - Data validation and serialization
Description
Languages
Python
96.5%
Mako
3.5%