A mock e-commerce API built with FastAPI that simulates a simple order management system.
- User authentication with JWT tokens
- Order management (create, view, cancel)
- Role-based access control (admin vs regular users)
- Mock product data
- In-memory database for testing
- Python 3.12+
- Poetry (for dependency management)
-
Clone the repository
-
Create a virtual environment:
On Unix-based systems:
python3 -m venv .venv
On Windows:
python -m venv .venv
-
Activate the virtual environment:
On Unix-based systems:
source .venv/bin/activateOn Windows:
.venv\Scripts\activate
-
Upgrade pip:
pip install --upgrade pip
-
Install Poetry:
pip install poetry
-
Configure environment variables:
# Copy the environment template file # Edit the .env file and replace XXXXXXXXXX with your actual API key
-
Install project dependencies using Poetry:
poetry install --with test
- Start the server:
uvicorn src.app:app --reload
The API will be available at http://localhost:8000
Once the server is running, you can access:
- Swagger UI documentation:
http://localhost:8000/docs - ReDoc documentation:
http://localhost:8000/redoc
- Click on the "Authorize" button (🔓) at the top right of the Swagger UI page
- In the login form:
- Enter any of the mock user credentials (e.g., username:
johndoe, password:password123) - Click "Authorize"
- Enter any of the mock user credentials (e.g., username:
- The system will automatically:
- Call the login endpoint
- Include the JWT token in all subsequent API requests
- You can now test any endpoint directly from the Swagger UI
POST /login- Authenticate user and get JWT token
GET /products- Get all available productsGET /products/{product_id}- Get a specific product by IDPOST /products- Create a new product (admin only)DELETE /products/{product_id}- Delete a specific product (admin only)
GET /orders- Get all orders for the current user (admin can see all orders)GET /orders/{order_id}- Get a specific order by IDPOST /orders- Create a new order for the current userPOST /orders/{order_id}/cancel- Cancel a specific orderGET /user_orders- Get all orders for a specific user (admin only)POST /create_order- Create an order for a specific user (admin only)
POST /chat- Chat interaction endpoint
The API comes with three pre-configured users:
-
Admin User
- Username: admin
- Password: admin123
-
Regular User 2
- Username: johndoe
- Password: password123
-
Regular User 3
- Username: sarahs
- Password: userpass456
The API comes with several products. Here are two examples:
-
Classic White T-Shirt
- ID: 1
- Price: $19.99
- Description: A comfortable cotton t-shirt perfect for everyday wear
-
Wireless Headphones
- ID: 5
- Price: $129.99
- Description: Bluetooth headphones with noise cancellation and 20-hour battery life
The candidate solution should be implemented in the candidate_solution/ directory following these sequential steps:
- Implement a Retrieval-Augmented Generation (RAG) system using the provided FAQ markdown file
- The system should:
- Process and index the FAQ content
- Match user queries with relevant FAQ sections
- Generate contextually appropriate responses based on the matched FAQ content
- Maintain conversation coherence while staying within the FAQ knowledge boundaries
- Implement functionality to:
- List all orders for the current user
- Display order details including status, items, and total
- List available products with their details
- Include proper error handling for invalid requests
- Maintain conversation context while switching between orders and products
- Implement order cancellation functionality:
- Verify order ownership and eligibility for cancellation
- Process cancellation requests
- Provide confirmation and status updates
- Handle edge cases (already cancelled orders, non-existent orders)
- Maintain proper error messaging
- Implement new order creation:
- Guide users through product selection
- Handle quantity specifications
- Process the order creation
- Provide order confirmation with details
- Include input validation and error handling
- Maintain conversation context throughout the interaction
- Implement proper error handling and user feedback
- Follow REST API best practices
- User query is first matched against FAQ content
- If the query requires specific actions (listing, cancellation, creation), the appropriate module handles it
- System maintains context and can handle follow-up questions
- All interactions should be natural and conversational while being accurate and efficient
-
First, get a JWT token by logging in:
curl -X POST "http://localhost:8000/login" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=user1&password=user123"
This will return a response like:
{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "token_type": "bearer" } -
Use the JWT token from the response to create a new order:
# Replace the JWT_TOKEN with the access_token from the login response export JWT_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." curl -X POST "http://localhost:8000/orders" \ -H "Authorization: Bearer $JWT_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "items": [ { "product_id": 1, "quantity": 2 } ] }'
-
View all orders using the same JWT token:
curl "http://localhost:8000/orders" \ -H "Authorization: Bearer $JWT_TOKEN"
-
Get details of a specific product:
curl "http://localhost:8000/products/1" \ -H "Authorization: Bearer $JWT_TOKEN"