
- Set up project structure with FastAPI and SQLite - Create models for products and cart items - Implement schemas for API request/response validation - Add database migrations with Alembic - Create service layer for business logic - Implement API endpoints for cart operations - Add health endpoint for monitoring - Update documentation - Fix linting issues
Simple Cart System
A simple shopping cart system API built with FastAPI and SQLite.
Features
- Product management (CRUD operations)
- Shopping cart functionality
- User-specific carts
- Stock management
Getting Started
Prerequisites
- Python 3.8 or higher
- pip (Python package manager)
Installation
- Clone the repository:
git clone <repository-url>
cd simplecartsystem
- Install the dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the application:
uvicorn main:app --reload
The API will be available at http://localhost:8000.
API Documentation
Interactive API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
API Endpoints
Health Check
GET /api/v1/health
- Check API and database health
Products
GET /api/v1/products
- List all products (with pagination)GET /api/v1/products/{product_id}
- Get a specific productPOST /api/v1/products
- Create a new productPUT /api/v1/products/{product_id}
- Update a productDELETE /api/v1/products/{product_id}
- Delete a product
Cart
GET /api/v1/cart
- View current cartPOST /api/v1/cart/items
- Add item to cartPUT /api/v1/cart/items/{item_id}
- Update cart item quantityDELETE /api/v1/cart/items/{item_id}
- Remove item from cartDELETE /api/v1/cart
- Clear cart
Cart Usage Example
Add Product to Cart
curl -X POST "http://localhost:8000/api/v1/cart/items" \
-H "Content-Type: application/json" \
-H "user-id: user123" \
-d '{
"product_id": 1,
"quantity": 2
}'
View Cart
curl -X GET "http://localhost:8000/api/v1/cart" \
-H "user-id: user123"
Database Schema
The system uses SQLite with the following main tables:
products
: Stores product informationcarts
: Stores user cart informationcart_items
: Stores items in user carts
Technology Stack
- FastAPI - Web framework
- SQLAlchemy - ORM
- Alembic - Database migrations
- Pydantic - Data validation
- SQLite - Database
Description
Languages
Python
98.4%
Mako
1.6%