
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: BackendIM <https://backend.im>
E-commerce API
A RESTful API for an e-commerce application built with FastAPI and SQLite.
Features
- User authentication and authorization
- Product management
- Category management
- Shopping cart and order processing
- Admin dashboard
Tech Stack
- Framework: FastAPI
- Database: SQLite with SQLAlchemy ORM
- Migration: Alembic
- Authentication: JWT tokens
- API Documentation: Swagger UI and ReDoc (auto-generated)
Project Structure
/
├── alembic/ # Database migrations
├── app/ # Main application directory
│ ├── api/ # API endpoints
│ │ ├── endpoints/ # API route handlers
│ ├── core/ # Core components
│ ├── crud/ # CRUD operations
│ ├── db/ # Database sessions and connections
│ ├── models/ # SQLAlchemy models
│ └── schemas/ # Pydantic models/schemas
├── main.py # FastAPI application instance
└── requirements.txt # Project dependencies
Installation
-
Clone the repository
-
Install dependencies:
pip install -r requirements.txt
-
Database Setup: The application uses SQLite as the database, which is stored in
../storage/db/db.sqlite
(outside the project root directory). This directory will be created automatically when the application runs. -
Run database migrations:
alembic upgrade head
-
Start the API server:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
API Endpoints
Authentication
POST /api/v1/auth/register
- Register a new userPOST /api/v1/auth/login
- Login and get access token
Users
GET /api/v1/users/me
- Get current user infoPUT /api/v1/users/me
- Update current user infoGET /api/v1/users/{user_id}
- Get user by IDGET /api/v1/users/
- Get all users (admin only)PUT /api/v1/users/{user_id}
- Update user by ID (admin only)
Categories
GET /api/v1/categories/
- List all categoriesPOST /api/v1/categories/
- Create a new category (admin only)GET /api/v1/categories/{category_id}
- Get category by IDPUT /api/v1/categories/{category_id}
- Update category (admin only)DELETE /api/v1/categories/{category_id}
- Delete category (admin only)
Products
GET /api/v1/products/
- List all active productsPOST /api/v1/products/
- Create a new product (admin only)GET /api/v1/products/{product_id}
- Get product by IDPUT /api/v1/products/{product_id}
- Update product (admin only)DELETE /api/v1/products/{product_id}
- Delete product (admin only)GET /api/v1/products/category/{category_id}
- Get products by category
Orders
GET /api/v1/orders/
- List user's orders (or all orders for admin)POST /api/v1/orders/
- Create a new orderGET /api/v1/orders/{order_id}
- Get order by IDPUT /api/v1/orders/{order_id}
- Update orderPUT /api/v1/orders/{order_id}/status
- Update order status (admin only)GET /api/v1/orders/status/{status}
- Get orders by status (admin only)
API Documentation
When the server is running, the API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Database Schema
Users
- id: Integer (Primary Key)
- email: String (Unique)
- username: String (Unique)
- hashed_password: String
- full_name: String (Optional)
- is_active: Boolean
- is_admin: Boolean
- created_at: DateTime
- updated_at: DateTime
Categories
- id: Integer (Primary Key)
- name: String (Unique)
- description: Text (Optional)
Products
- id: Integer (Primary Key)
- name: String
- description: Text (Optional)
- price: Float
- stock: Integer
- is_active: Boolean
- category_id: Integer (Foreign Key)
- image_url: String (Optional)
- created_at: DateTime
- updated_at: DateTime
Orders
- id: Integer (Primary Key)
- user_id: Integer (Foreign Key)
- status: Enum (PENDING, PROCESSING, SHIPPED, DELIVERED, CANCELLED)
- total_amount: Float
- shipping_address: String
- created_at: DateTime
- updated_at: DateTime
Order Items
- id: Integer (Primary Key)
- order_id: Integer (Foreign Key)
- product_id: Integer (Foreign Key)
- quantity: Integer
- price: Float (Price at the time of purchase)
Description
Languages
Python
98.6%
Mako
1.4%