
- Set up FastAPI application structure - Create database models for User, Product, Cart, CartItem, Order, and OrderItem - Set up Alembic for database migrations - Create Pydantic schemas for request/response models - Implement API endpoints for products, cart operations, and checkout process - Add health endpoint - Update README with project details and documentation
Shopping Cart and Checkout API
A FastAPI-based REST API for managing a shopping cart and checkout process.
Features
- Product management (Create, Read, Update, Delete)
- Shopping cart operations (Add, Update, Remove, Get)
- Checkout process with order creation and payment simulation
- SQLite database with SQLAlchemy ORM
- Alembic migrations for database versioning
Tech Stack
- Python 3.8+
- FastAPI
- SQLAlchemy
- Pydantic
- SQLite
- Alembic
Project Structure
.
├── alembic.ini # Alembic configuration
├── main.py # FastAPI application entry point
├── app/ # Application package
│ ├── api/ # API endpoints
│ │ ├── api.py # API router
│ │ └── endpoints/ # API endpoint modules
│ │ ├── cart.py # Cart operations
│ │ ├── checkout.py # Checkout process
│ │ ├── health.py # Health check endpoint
│ │ └── products.py # Product operations
│ ├── core/ # Core application code
│ │ └── config.py # Configuration settings
│ ├── crud/ # CRUD operations
│ │ ├── base.py # Base CRUD class
│ │ ├── cart.py # Cart CRUD operations
│ │ ├── order.py # Order CRUD operations
│ │ └── product.py # Product CRUD operations
│ ├── db/ # Database
│ │ └── session.py # DB session configuration
│ ├── models/ # SQLAlchemy models
│ │ ├── base.py # Base model class
│ │ ├── cart.py # Cart and CartItem models
│ │ ├── order.py # Order and OrderItem models
│ │ ├── product.py # Product model
│ │ └── user.py # User model
│ └── schemas/ # Pydantic schemas
│ ├── base.py # Base schema classes
│ ├── cart.py # Cart and CartItem schemas
│ ├── order.py # Order and OrderItem schemas
│ └── product.py # Product schemas
└── migrations/ # Alembic migrations
├── env.py # Alembic environment
├── script.py.mako # Migration script template
└── versions/ # Migration versions
└── 001_initial_database_schema.py # Initial schema
API Endpoints
Health Check
GET /api/v1/health
- Check API health
Products
GET /api/v1/products
- List products (with optional filtering)POST /api/v1/products
- Create a new productGET /api/v1/products/{product_id}
- Get a specific productPUT /api/v1/products/{product_id}
- Update a productDELETE /api/v1/products/{product_id}
- Delete a product
Cart
GET /api/v1/cart
- Get the current cartPOST /api/v1/cart/items
- Add an item to the cartPUT /api/v1/cart/items/{item_id}
- Update a cart itemDELETE /api/v1/cart/items/{item_id}
- Remove a cart itemDELETE /api/v1/cart
- Clear the cart
Checkout
POST /api/v1/checkout
- Create an order from the cartGET /api/v1/checkout
- List user's ordersGET /api/v1/checkout/{order_id}
- Get a specific orderPOST /api/v1/checkout/{order_id}/pay
- Process payment for an order
Getting Started
Installation
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
Database Setup
- Run migrations to create the database schema:
alembic upgrade head
Running the API
- Start the FastAPI server:
uvicorn main:app --reload
- Access the API documentation at
http://localhost:8000/docs
Database Migrations
To create a new migration:
alembic revision --autogenerate -m "description"
To apply migrations:
alembic upgrade head
API Documentation
Interactive API documentation is available at /docs
when the API is running.
Notes
- In a real-world application, authentication and authorization would be implemented
- The current implementation has a simplified user system (default user ID 1 is used for demonstration)
- Cart session management is implemented using cookies and headers
- The payment process is simulated (no actual payment processing)
Description
Languages
Python
99.1%
Mako
0.9%