This is a simple e-commerce application built with Flask and SQLAlchemy for managing customers, products, and orders. The system includes customer registration, product and order management, order history viewing, and order status updating.
-
Clone the repository:
git clone https://github.com/your-repo/ecommerce-app.git cd ecommerce-app -
Install the required packages: Ensure you have Python 3 and pip installed, then run:
pip install -r requirements.txt
-
Configure Environment Variables: Create a
.envfile in the project root with your database credentials:username=your_mysql_username password=your_mysql_password
-
Set up the MySQL Database:
- Ensure you have a MySQL database named
ecommerce. - Configure your
.envfile with the correct credentials.
- Ensure you have a MySQL database named
-
Initialize the Database: In the project directory, run:
flask db init flask db migrate -m "Initial migration" flask db upgrade
- Customer Management: Register new customers with basic information.
- Product Management: Products with stock, price, and other details.
- Order Management: Place new orders for customers, with items and quantities.
- Order History: View the order history of each customer.
- Order Status Update: Change the status of an order (e.g., pending, shipped, delivered, canceled).
-
Homepage
Endpoint:GET /
Description: Displays the application homepage with forms to interact with the API. -
Register Customer
Endpoint:POST /register_customer
Description: Registers a new customer.
Request JSON:{ "name": "Customer Name", "email": "email@example.com", "phone_number": "1234567890" } -
Place Order
Endpoint:POST /place_order
Description: Places an order for a customer with multiple items.
Request JSON:{ "customer_id": 1, "items": [ { "product_id": 101, "quantity": 2 }, { "product_id": 102, "quantity": 5 } ] } -
Order History
Endpoint:GET /order_history/<customer_id>
Description: Streams the order history for a customer. -
Update Order Status
Endpoint:PATCH /update_order_status
Description: Updates the status of a specific order.
Request JSON:{ "order_id": 1, "status": "shipped" }
-
Start the Flask Application:
python app.py
The application will run on
http://localhost:5000. -
Access the Homepage: Open a web browser and navigate to
http://localhost:5000to view the main page with forms for registration, order placement, and status updating. -
Register a Customer:
- Fill in the name, email, and phone number in the "Register Customer" form.
- Submit to register a new customer.
-
Place an Order:
- Enter the customer ID, then specify multiple product IDs and quantities.
- Submit to place the order.
-
View Order History:
- Visit
/order_history/<customer_id>in the browser (replace<customer_id>with an actual ID) to view past orders.
- Visit
-
Update Order Status:
- Enter an order ID and select the new status in the "Update Order Status" form.
- Submit to update the order's status.
- Decorator for Stock Validation:
validate_stockdecorator verifies if there’s sufficient stock before placing an order. - Lambda for Discounts: Orders with more than 10 items receive a 10% discount using a lambda function.
- Generator for Order History: Streams order history with a generator function to efficiently handle large datasets.