
- Setup project structure and FastAPI application - Create SQLAlchemy models for users, products, carts, and orders - Implement Alembic migrations - Add CRUD operations and endpoints for all resources - Setup authentication with JWT - Add role-based access control - Update documentation
128 lines
3.5 KiB
Markdown
128 lines
3.5 KiB
Markdown
# Simple Ecommerce API
|
|
|
|
A simple ecommerce API built with FastAPI and SQLite.
|
|
|
|
## Features
|
|
|
|
- User authentication (register, login)
|
|
- Product management (CRUD)
|
|
- Shopping cart functionality
|
|
- Order management
|
|
- Role-based access control (admin/regular users)
|
|
|
|
## Tech Stack
|
|
|
|
- Python 3.9+
|
|
- FastAPI
|
|
- SQLAlchemy ORM
|
|
- Alembic for database migrations
|
|
- SQLite for database
|
|
- JWT for authentication
|
|
|
|
## API Endpoints
|
|
|
|
### Authentication
|
|
|
|
- `POST /api/v1/auth/register`: Register a new user
|
|
- `POST /api/v1/auth/login`: Login and get access token
|
|
|
|
### Products
|
|
|
|
- `GET /api/v1/products`: List all products
|
|
- `GET /api/v1/products/{id}`: Get a specific product
|
|
- `POST /api/v1/products`: Create a new product (admin only)
|
|
- `PUT /api/v1/products/{id}`: Update a product (admin only)
|
|
- `DELETE /api/v1/products/{id}`: Delete a product (admin only)
|
|
|
|
### Cart
|
|
|
|
- `GET /api/v1/cart`: Get current user's cart
|
|
- `POST /api/v1/cart/items`: Add an item to cart
|
|
- `PUT /api/v1/cart/items/{id}`: Update cart item quantity
|
|
- `DELETE /api/v1/cart/items/{id}`: Remove an item from cart
|
|
- `DELETE /api/v1/cart`: Clear the cart
|
|
|
|
### Orders
|
|
|
|
- `GET /api/v1/orders`: List user's orders (or all orders for admin)
|
|
- `POST /api/v1/orders`: Create a new order from cart
|
|
- `GET /api/v1/orders/{id}`: Get a specific order with details
|
|
- `PUT /api/v1/orders/{id}/status`: Update order status (admin only)
|
|
- `DELETE /api/v1/orders/{id}`: Cancel an order
|
|
|
|
### Users
|
|
|
|
- `GET /api/v1/users`: List all users (admin only)
|
|
- `GET /api/v1/users/me`: Get current user details
|
|
- `PUT /api/v1/users/me`: Update current user details
|
|
- `GET /api/v1/users/{id}`: Get a specific user (admin only)
|
|
- `PUT /api/v1/users/{id}`: Update a user (admin only)
|
|
- `DELETE /api/v1/users/{id}`: Delete a user (admin only)
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── alembic.ini # Alembic configuration
|
|
├── app/ # Main application package
|
|
│ ├── api/ # API endpoints
|
|
│ │ └── v1/ # API version 1
|
|
│ ├── core/ # Core functionality
|
|
│ ├── crud/ # Database CRUD operations
|
|
│ ├── db/ # Database session and models
|
|
│ ├── models/ # SQLAlchemy models
|
|
│ └── schemas/ # Pydantic schemas
|
|
├── main.py # Application entry point
|
|
├── migrations/ # Database migrations
|
|
└── requirements.txt # Project dependencies
|
|
```
|
|
|
|
## Getting Started
|
|
|
|
### Prerequisites
|
|
|
|
- Python 3.9+
|
|
|
|
### Installation
|
|
|
|
1. Clone the repository:
|
|
```
|
|
git clone <repository-url>
|
|
cd simple-ecommerce-api
|
|
```
|
|
|
|
2. Install dependencies:
|
|
```
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Set up environment variables:
|
|
```
|
|
export SECRET_KEY="your-secret-key"
|
|
export API_URL="http://localhost:8000" # Optional, default is http://localhost:8000
|
|
```
|
|
|
|
4. Run database migrations:
|
|
```
|
|
alembic upgrade head
|
|
```
|
|
|
|
5. Start the server:
|
|
```
|
|
uvicorn main:app --reload
|
|
```
|
|
|
|
6. Open your browser and navigate to `http://localhost:8000/docs` to see the interactive API documentation.
|
|
|
|
## Environment Variables
|
|
|
|
| Variable | Description | Default |
|
|
|----------|-------------|---------|
|
|
| SECRET_KEY | JWT secret key | "supersecretkey" |
|
|
| API_URL | Base URL for the API | "http://localhost:8000" |
|
|
| FIRST_SUPERUSER_EMAIL | Email for initial superuser | None |
|
|
| FIRST_SUPERUSER_PASSWORD | Password for initial superuser | None |
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License. |