This repository contains two APIs built with FastAPI and MongoDB. -First API returns product listings -Second API accecpts product orders
-
Clone the repository:
git clone https://github.com/shaunaksb/ecommerceapi.git -
Navigate to the project directory:
cd ecommerceapi -
Install the required packages:
pip install -r requirements.txt
To run the application, use the following command:
uvicorn listing:app --port 8000 --reload
uvicorn orders:app --port 8001 --reload
This API returns a list of products stored in a MongoDB collection according to the constraints provided in the request.
Retrieves all products from the MongoDB collection.
curl -X GET http://127.0.0.1:8000/products/Returns a list of all products and pagination information.
{
"data": [
{
"_id": "65be645dc9b6e43c8f7983cf",
"name": "Samsung Galaxy S21",
"price": 10,
"quantity": 5
},
{
"_id": "65be645dc9b6e43c8f7983d0",
"name": "Sony PlayStation 5",
"price": 20,
"quantity": 10
},
{
"_id": "65be645dc9b6e43c8f7983d1",
"name": "Microsoft Xbox Series X",
"price": 30,
"quantity": 15
},
{
"_id": "65be645dc9b6e43c8f7983d2",
"name": "Apple MacBook Pro",
"price": 40,
"quantity": 20
}
],
"page": {
"limit": 10,
"nextOffset": null,
"prevOffset": null,
"total": 4
}
}Retrieves the amount of products specified in the request from the MongoDB collection.
curl -X GET 'http://127.0.0.1:8000/products/?limit=20'Retrieves the amount of products specified in the request from the MongoDB collection, starting from the product which comes after the last product of the previous response.
curl -X GET 'http://127.0.0.1:8000/products/?limit=20&offset=20'Retrieves all products from the MongoDB collection with a price greater than or equal to specified min_price.
curl -X GET 'http://127.0.0.1:8000/products/?min_price=10'Retrieves all products from the MongoDB collection with a price less than or equal to specified max_price.
curl -X GET 'http://127.0.0.1:8000/products/?max_price=40'Retrieves all products from the MongoDB collection with a price between specified min_price and max_price (inclusive).
curl -X GET 'http://127.0.0.1:8000/products/?min_price=10&max_price=40'-
limit: This parameter is used to limit the number of products returned in the response. For example,limit=20will return only the first 20 products. -
offset: This parameter is used in conjunction withlimitto implement pagination. For example,limit=20&offset=20will return 20 products, starting from the 21st product. -
min_price: This parameter is used to filter the products by their price.min_price=10will return only the products with a price of 10 or more. -
max_price: This parameter is also used to filter the products by their price.max_price=40will return only the products with a price of 40 or less.
-
data: This is an array of product objects. Each object contains the following properties:_id: The unique identifier of the product in the MongoDB collection.name: The name of the product.price: The price of the product.quantity: The quantity of the product available.
-
page: This is an object that contains pagination information:limit: The number of products returned in the current page.nextOffset: The offset for the next page of products. If there are no more products, this will benull.prevOffset: The offset for the previous page of products. If this is the first page, this will benull.total: The total number of products in the MongoDB collection.
This API places orders for an ecommerce application built with FastAPI and MongoDB.
URL: /orders/
Method: POST
Request Parameters:
items: A list of items that the user is ordering. Each item in this list is an object with the following properties:productId: The unique identifier of the product that the user is ordering.boughtQuantity: The quantity of the product that the user is ordering.
totalAmount: The total cost of the order. It should be the sum of the cost of all items in the order.userAddress: An object containing the user's address. It has the following properties:city: The city where the user lives.country: The country where the user lives.zipCode: The zip code of the user's address.
Request Body:
{
"items": [
{
"productId": "65be645dc9b6e43c8f7983d4",
"boughtQuantity": 1
}
],
"totalAmount": 1,
"userAddress": {
"city": "Nagpur",
"country": "India",
"zipCode": "440010"
}
}Response Parameters:
_id: The unique identifier of the order. It is automatically generated by MongoDB when the order is inserted into the database.items: The list of items that the user ordered. It is the same as theitemsfield in the request.totalAmount: The total cost of the order. It is the same as thetotalAmountfield in the request.userAddress: The user's address. It is the same as theuserAddressfield in the request.createdOn: The date and time when the order was created. It is automatically set to the current date and time when the order is inserted into the database.
Response:
{
"_id": "65be6c4afc6ea551ac6f4892",
"items": [
{
"productId": "65be645dc9b6e43c8f7983d4",
"boughtQuantity": 1
}
],
"totalAmount": 1.0,
"userAddress": {
"city": "Nagpur",
"country": "India",
"zipCode": "440010"
},
"createdOn": "2024-02-03T22:09:38.168000"
}