This is an e-commerce application built with Python and FastAPI.
It provides various APIs for managing products, creating orders, and retrieving order information. The application utilizes a MongoDB database to store product and order data.
The application uses a MongoDB database to store product and order data. Make sure you have MongoDB installed and running on your system. Update the database connection details in the .env
file.
- Clone the repository:
https://github.com/goodchai0/FastApi_MongoDb
- Navigate to the project directory:
cd FastApi_MongoDb
- Install the required dependencies:
pip install -r requirements.txt
- Ensure that MongoDB is installed and running on your system.
- Create a MongoDB database for the application and update the database connection details in the
.env
file.
- Start the FastAPI server:
python -m uvicorn main:app --reload
- Access the API endpoints in your preferred API testing tool or web browser.
The following APIs are available in the E-commerce Application:
You may use http://127.0.0.1:8000/docs#/ for swagger UI
- Endpoint:
api/products
- Method: GET
- Description: Retrieve all products from the db.
- Response:
- Status: 200 OK
- Body: List of products.
[
{
"id": 1,
"name": "TV",
"price": 500.0,
"available_quantity": 10,
"category": "Electronics"
},
{
"id": 2,
"name": "Laptop",
"price": 1000.0,
"available_quantity": 5,
"category": "Electronics"
}
]
Endpoint: api/orders
Method: POST
Example Request:
POST http://localhost:8000/api/orders
Content-Type: application/json
{
"timestamp": "2023-07-08T10:30:00",
"items": [
{
"productId": "1",
"boughtQuantity": 2
},
{
"productId": "2",
"boughtQuantity": 1
}
],
"totalAmount": 0.0,
"userAddress": {
"city": "Sample City",
"country": "Sample Country",
"zipCode": "12345"
}
}
Endpoint: api/orders
Method: GET
Example Request:
[
{
"timestamp": "2023-07-09T15:27:14.092011",
"items": [
{
"productId": 1,
"boughtQuantity": 2
},
{
"productId": 2,
"boughtQuantity": 1
}
],
"totalAmount": 1.0,
"userAddress": {
"city": "Sample City",
"country": "Sample Country",
"zipCode": "12345"
}
},
{
"timestamp": "2023-07-09T15:29:28.591415",
"items": [
{
"productId": 1,
"boughtQuantity": 2
},
{
"productId": 2,
"boughtQuantity": 1
}
],
"totalAmount": 3499.9700000000003,
"userAddress": {
"city": "Sample City",
"country": "Sample Country",
"zipCode": "12345"
}
}]
Endpoint: api/orders/<id>
Method: GET
Endpoint: api/products/<product_id:int>
Method: GET
Example Request:
PUT http://localhost:8000/api/products/1
Content-Type: application/json
{
"id": 1,
"name": "TV",
"price": 999.99,
"available_quantity": 20,
"category": "Electronics"
}