2025-05-26 14:14:50 +00:00

147 lines
4.0 KiB
Markdown

# E-Commerce API
A full-featured e-commerce API built with FastAPI and SQLite.
## Features
- **User Management**: Registration, authentication, and profile management
- **Product Management**: Products with categories, pricing, and inventory management
- **Shopping Cart**: Add, update, remove items in user's shopping cart
- **Order Processing**: Create orders from cart, track order status
- **Authentication**: Username-based JWT authentication with role-based access control
## Tech Stack
- **FastAPI**: Modern, fast API framework
- **SQLAlchemy**: SQL toolkit and ORM
- **Alembic**: Database migration tool
- **SQLite**: Serverless database engine
- **JWT**: JSON Web Tokens for authentication
- **Pydantic**: Data validation and settings management
## API Endpoints
### Authentication
- `POST /api/v1/auth/login`: Login with username and password to get access token
- `POST /api/v1/auth/register`: Register a new user (requires username, email, and password)
- `GET /api/v1/auth/me`: Get current user information
### Users
- `GET /api/v1/users/`: List all users (admin only)
- `POST /api/v1/users/`: Create a new user (admin only)
- `GET /api/v1/users/me`: Get current user profile
- `PUT /api/v1/users/me`: Update current user profile
- `GET /api/v1/users/{user_id}`: Get user by ID
- `PUT /api/v1/users/{user_id}`: Update user (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 products (filter by category optional)
- `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)
### Cart
- `GET /api/v1/cart/`: Get current user's cart
- `POST /api/v1/cart/items`: Add item to cart
- `PUT /api/v1/cart/items/{item_id}`: Update cart item quantity
- `DELETE /api/v1/cart/items/{item_id}`: Remove item from cart
- `DELETE /api/v1/cart/`: Clear cart
### Orders
- `GET /api/v1/orders/`: List user's orders
- `POST /api/v1/orders/`: Create new order from cart
- `GET /api/v1/orders/{order_id}`: Get order by ID
- `PUT /api/v1/orders/{order_id}`: Update order status (admin only)
- `POST /api/v1/orders/{order_id}/cancel`: Cancel order (if pending)
### Health Check
- `GET /health`: Application health check
## Installation and Setup
### Prerequisites
- Python 3.8+
- pip (Python package installer)
### Installation
1. Clone the repository
```bash
git clone <repository-url>
cd ecommerceapplication
```
2. Install dependencies
```bash
pip install -r requirements.txt
```
3. Run database migrations
```bash
alembic upgrade head
```
4. Start the application
```bash
uvicorn main:app --reload
```
The API will be available at http://localhost:8000.
### Authentication Note
This API uses username-based authentication rather than email-based. When registering and logging in:
- Username is the primary identifier for authentication
- Email is still required during registration but is not used for login
- Password must meet security requirements (minimum 8 characters)
## Documentation
FastAPI provides automatic API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
## Development
### Database Migrations
Create a new migration after model changes:
```bash
alembic revision --autogenerate -m "Description of changes"
```
Apply migrations:
```bash
alembic upgrade head
```
#### Recent Migrations
- **2a3b4c5d6e7f_add_username_field**: Added username field to User model and migrated from email-based to username-based authentication
### Running Tests
```bash
pytest
```
## License
This project is licensed under the MIT License - see the LICENSE file for details.