Automated Action e04059c1e3 Implement FastAPI cart and checkout system
- 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
2025-07-21 09:20:12 +00:00

152 lines
3.3 KiB
Markdown

# 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
1. Install dependencies:
```bash
pip install -r requirements.txt
```
2. Run database migrations:
```bash
alembic upgrade head
```
3. Start the application:
```bash
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 cart
- `GET /api/v1/carts/{cart_id}` - Get cart by ID
- `GET /api/v1/carts/user/{user_id}` - Get all carts for a user
- `DELETE /api/v1/carts/{cart_id}` - Clear all items from a cart
### Cart Items
- `POST /api/v1/carts/{cart_id}/items/` - Add item to cart
- `PUT /api/v1/carts/{cart_id}/items/{item_id}` - Update item quantity
- `DELETE /api/v1/carts/{cart_id}/items/{item_id}` - Remove item from cart
### Checkout
- `POST /api/v1/checkout/` - Process checkout
### System
- `GET /` - Service information
- `GET /health` - Health check endpoint
## Example Usage
### Create a Cart
```bash
curl -X POST "http://localhost:8000/api/v1/carts/" \
-H "Content-Type: application/json" \
-d '{"user_id": "user123"}'
```
### Add Item to Cart
```bash
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
```bash
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 key
- `user_id` - User identifier
- `status` - Cart status (active, checked_out, abandoned)
- `created_at` - Creation timestamp
- `updated_at` - Last update timestamp
### Cart Items Table
- `id` - Primary key
- `cart_id` - Foreign key to carts table
- `product_id` - Product identifier
- `product_name` - Product name
- `price` - Item price
- `quantity` - Item quantity
## Development
### Database Migrations
To create a new migration:
```bash
alembic revision -m "Description of changes"
```
To apply migrations:
```bash
alembic upgrade head
```
To rollback migrations:
```bash
alembic downgrade -1
```
### Code Linting
The project uses Ruff for code formatting and linting:
```bash
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