This repository contains a MERN (MongoDB, Express, React, Node.js) stack application for managing products, stores, and orders. The project provides a set of HTTP APIs for creating, searching, and deleting products, stores, and orders. Additionally, it includes unit tests and integration tests to ensure the correctness of the API functionality.
To get started with this project, you need to have Node.js and MongoDB installed on your machine.
- Clone the repository
git clone https://github.com/ankan-0610/store-api
cd store-api
- Install dependencies
npm install
- Set up environment variables: Create a .env file in the root directory and add the following environment variables:
MONGO_URI=YOUR_MONGODB_CLUSTER_CONNECTION_URI
PORT=5000
- Start the server
npm start
The server will start on http://localhost:5000.
You can use Postman or any other API client to interact with the API endpoints. Below is a detailed description of each endpoint and its usage.
- Endpoint: POST /api/products
- Description: Create a new product in a specified store.
- Request Body:
{
"name": "Product1",
"price": 10,
"storeName": "Store1"
}
- Endpoint: GET /api/products/search
- Description: Search for products by name within a specific store.
- Request Body:
{
"storeName": "Store1",
"productName": "Product"
}
- Example query:
GET /api/products/search?storeName=Store1&productName=Product
- Endpoint: DELETE /api/products/:productId
- Description: Delete a product by its ID.
- URL Parameter:
{
"productId": "60c72b2f9b1d8c1a4c8a4c8b"
}
- Endpoint: POST /api/stores
- Description: Create a new store.
- Request Body:
{
"name": "Store1",
"location": "Location1"
}
- Endpoint: DELETE /api/stores/:storeId
- Description: Delete a store by its ID.
- URL Parameter:
{
"storeId": "60c72b2f9b1d8c1a4c8a4c8a"
}
- Endpoint: POST /api/orders
- Description: Create a new order with specified products.
- Request Body:
{
"productIds": [
"60c72b2f9b1d8c1a4c8a4c8b",
"60c72b2f9b1d8c1a4c8a4c8c"
]
}
- Endpoint: DELETE /api/orders/:orderId
- Description: Delete an order by its ID.
- URL Parameter:
{
"orderId": "60c72b2f9b1d8c1a4c8a4c8b"
}
The project includes both unit tests and integration tests to ensure the correctness of the API endpoints.
Unit tests are written using Jest to test individual components and functions of the application. These tests ensure that each function behaves as expected in isolation.
To run unit tests, use the following command:
npm test
Integration tests are written to test the interaction between different components of the application, such as database operations and HTTP requests. These tests ensure that the API endpoints work correctly with the underlying data models and database.
To run integration tests, use the following command:
npm run test:integration
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (git checkout -b feature/your-feature).
- Make your changes and commit them (git commit -m 'Add new feature').
- Push to the branch (git push origin feature/your-feature).
- Create a new Pull Request.