Automated Action d2b80dacc4 Implement Shopping Cart and Checkout API
- 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
2025-05-18 00:00:02 +00:00
2025-05-17 23:47:56 +00:00

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 product
  • GET /api/v1/products/{product_id} - Get a specific product
  • PUT /api/v1/products/{product_id} - Update a product
  • DELETE /api/v1/products/{product_id} - Delete a product

Cart

  • GET /api/v1/cart - Get the current cart
  • POST /api/v1/cart/items - Add an item to the cart
  • PUT /api/v1/cart/items/{item_id} - Update a cart item
  • DELETE /api/v1/cart/items/{item_id} - Remove a cart item
  • DELETE /api/v1/cart - Clear the cart

Checkout

  • POST /api/v1/checkout - Create an order from the cart
  • GET /api/v1/checkout - List user's orders
  • GET /api/v1/checkout/{order_id} - Get a specific order
  • POST /api/v1/checkout/{order_id}/pay - Process payment for an order

Getting Started

Installation

  1. Clone the repository
  2. Install dependencies:
    pip install -r requirements.txt
    

Database Setup

  1. Run migrations to create the database schema:
    alembic upgrade head
    

Running the API

  1. Start the FastAPI server:
    uvicorn main:app --reload
    
  2. 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
Project: Shopping Cart and Checkout API
Readme 55 KiB
Languages
Python 99.1%
Mako 0.9%