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
2025-07-21 09:15:22 +00:00

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:
pip install -r requirements.txt
  1. Run database migrations:
alembic upgrade head
  1. 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 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

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 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:

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
Project: Cart and Checkout System
Readme 39 KiB
Languages
Python 96.5%
Mako 3.5%