Automated Action 4e92bb1338 Create FastAPI E-commerce API with SQLite
This commit includes:
- Project structure setup with FastAPI
- Database models with SQLAlchemy (users, products, categories, orders)
- SQLite database configuration
- Alembic migration scripts
- API endpoints for authentication, users, products, categories, and orders
- JWT authentication
- Comprehensive documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-11 18:51:15 +00:00

150 lines
4.2 KiB
Markdown

# 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
1. **Clone the repository**
2. **Install dependencies**:
```bash
pip install -r requirements.txt
```
3. **Run database migrations**:
```bash
alembic upgrade head
```
4. **Start the API server**:
```bash
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
```
## API Endpoints
### Authentication
- `POST /api/v1/auth/register` - Register a new user
- `POST /api/v1/auth/login` - Login and get access token
### Users
- `GET /api/v1/users/me` - Get current user info
- `PUT /api/v1/users/me` - Update current user info
- `GET /api/v1/users/{user_id}` - Get user by ID
- `GET /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 categories
- `POST /api/v1/categories/` - Create a new category (admin only)
- `GET /api/v1/categories/{category_id}` - Get category by ID
- `PUT /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 products
- `POST /api/v1/products/` - Create a new product (admin only)
- `GET /api/v1/products/{product_id}` - Get product by ID
- `PUT /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 order
- `GET /api/v1/orders/{order_id}` - Get order by ID
- `PUT /api/v1/orders/{order_id}` - Update order
- `PUT /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](http://localhost:8000/docs)
- ReDoc: [http://localhost:8000/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)