Live server link : https://express-ts-ecommerce-assignment.vercel.app
This is an Express application developed using TypeScript, integrating MongoDB with Mongoose for effective data management. The application allows for managing products and orders, ensuring data integrity through validation using Joi. It includes endpoints for creating, retrieving, updating, and deleting products and orders.
- Product Management
- Create a new product
- Retrieve a list of all products
- Retrieve a specific product by ID
- Update product information
- Delete a product
- Search products by a search term
- Order Management
- Create a new order
- Retrieve all orders
- Retrieve orders by user email
- Inventory Management Logic
- Check available quantity in inventory when creating an order
- Return error if ordered quantity exceeds available quantity
- Update inventory quantity and inStock status based on ordered quantity
- Node.js (version 14 or later)
- MongoDB (local instance or MongoDB Atlas)
- Clone the repository:
git clone https://github.com/your-username/e-commerce-express-app.git
cd e-commerce-express-app
- Install dependencies:
npm install
- Create a
.env
file in the root directory and add the following environment variables:
DATABASE_URL=mongodb+srv://<username>:<password>@cluster0.iixzvov.mongodb.net/<dbname>?retryWrites=true&w=majority
PORT=5000
Replace <username>
, <password>
, and <dbname>
with your MongoDB credentials and database name.
- For development, start the application using:
npm run start:dev
- To build the application, use:
tsc
npm run build
- For production, start the application using:
npm run start:prod
The application will be running at http://localhost:5000
.
-
Create a New Product
-
Endpoint:
/api/products
-
Method: POST
-
Sample Request Body:
{ "name": "iPhone 13", "description": "A sleek and powerful smartphone with cutting-edge features.", "price": 999, "category": "Electronics", "tags": ["smartphone", "Apple", "iOS"], "variants": [ { "type": "Color", "value": "Midnight Blue" }, { "type": "Storage Capacity", "value": "256GB" } ], "inventory": { "quantity": 50, "inStock": true } }
-
-
Retrieve a List of All Products
- Endpoint:
/api/products
- Method: GET
- Endpoint:
-
Retrieve a Specific Product by ID
- Endpoint:
/api/products/:productId
- Method: GET
- Endpoint:
-
Update Product Information
- Endpoint:
/api/products/:productId
- Method: PUT
- Sample Request Body: Same as creating a new product
- Endpoint:
-
Delete a Product
- Endpoint:
/api/products/:productId
- Method: DELETE
- Endpoint:
-
Search a Product
- Endpoint:
/api/products?searchTerm=iphone
- Method: GET
- Endpoint:
-
Create a New Order
-
Endpoint:
/api/orders
-
Method: POST
-
Sample Request Body:
{ "email": "[email protected]", "productId": "5fd67e890b60c903cd8544a3", "price": 999, "quantity": 1 }
-
-
Retrieve All Orders
- Endpoint:
/api/orders
- Method: GET
- Endpoint:
-
Retrieve Orders by User Email
- Endpoint:
/api/[email protected]
- Method: GET
- Endpoint:
- Uses Joi for validation of incoming data for product and order creation and updating operations.
- Handles validation errors gracefully and provides meaningful error messages in the API responses.
- Custom error handling for scenarios like product not found, insufficient inventory, etc.
- Express
- TypeScript
- Mongoose
- Joi
- ESLint (for linting and coding style enforcement)