This project implements a RESTful API for an online shopping platform. It allows users to register, login, view products, add products to cart, place orders, and manage their orders.
-
Clone the repository:
git clone https://github.com/black-sheepp/triveous-backend.git
-
Navigate to the project directory:
cd triveous-backend
-
Install dependencies:
npm install
-
Set up environment variables:
- Create a
.env
file in the root directory. - Define the following environment variables:
PORT=8000 MONGODB_URL=<your_mongodb_connection_string> JWT_SECRET=<your_jwt_secret_key>
- Create a
-
Start the server:
npm start
-
User Authentication:
POST /register
: Create a new user account.POST /login
: Authenticate user and generate authentication token.GET /logout
: Logout the user.
-
Product Management:
POST /create-category
: Create a new product category.POST /create-product
: Create a new product.GET /get-productListBycategory/:categoryId
: Get a list of products by category ID.GET /get-product/:productId
: Get details of a specific product by its ID.
-
Cart Management:
POST /add-to-cart
: Add a product to the user's cart.GET /view-cart
: View the user's cart.PUT /update-cart-items/:itemId
: Update the quantity of a product in the user's cart.DELETE /remove-cart-items/:itemId
: Remove a product from the user's cart.
-
Order Management:
GET /place-order
: Place an order using the products in the user's cart.GET /order-history
: Get the order history for the authenticated user.GET /order-details/:orderId
: Get detailed information of a specific order by its ID.
To test the API endpoints, you can use tools like Postman or write unit tests using libraries like Mocha and Chai.
-
Register User
- Method: POST
- Endpoint: /register
- Test Case:
- Input:
{ "username": "Shivam Gupta", "email": "[email protected]", "password": "test123", "phone": "1234567890", "address": "New Delhi, India" }
- Expected Output:
- Status Code: 200
- Response Body:
{ "username": "Shivam Gupta", "email": "[email protected]", "phone": "1234567890", "address": "New Delhi, India", "role": "user", "token": "<authentication_token>" }
- Input:
-
Login User
- Method: POST
- Endpoint: /login
- Test Case:
- Input:
{ "email": "[email protected]", "password": "test123" }
- Expected Output:
- Status Code: 200
- Response Body:
{ "username": "Shivam Gupta", "email": "[email protected]", "phone": "1234567890", "address": "New Delhi, India", "token": "<authentication_token>" }
- Input:
-
Logout User
- Method: GET
- Endpoint: /logout
- Test Case:
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "User logged out successfully" }
- Expected Output:
-
Create Product Category
- Method: POST
- Endpoint: /create-category
- Test Case:
- Input:
{ "name": "Electronics" }
- Expected Output:
- Status Code: 201
- Response Body:
{ "message": "Category created successfully", "category": { "_id": "<category_id>", "name": "Electronics", "createdAt": "<timestamp>", "updatedAt": "<timestamp>", "__v": 0 } }
- Input:
-
Create Product
- Method: POST
- Endpoint: /create-product
- Test Case:
- Input:
{ "title": "Laptop", "price": 1000, "description": "High-performance laptop", "category": "<category_id>" }
- Expected Output:
- Status Code: 201
- Response Body:
{ "message": "Product created successfully", "product": { "_id": "<product_id>", "title": "Laptop", "price": 1000, "description": "High-performance laptop", "availability": true, "category": "<category_id>", "createdAt": "<timestamp>", "updatedAt": "<timestamp>", "__v": 0 } }
- Input:
-
Get Products By Category ID
- Method: GET
- Endpoint: /get-productListBycategory/:categoryId
- Test Case:
- Input: categoryId
- Expected Output:
- Status Code: 200
- Response Body:
[ { "_id": "<product_id>", "title": "Laptop", "price": 1000, "description": "High-performance laptop", "availability": true } ]
-
Get Product By ID
- Method: GET
- Endpoint: /get-product/:productId
- Test Case:
- Input: productId
- Expected Output:
- Status Code: 200
- Response Body:
{ "_id": "<product_id>", "title": "Laptop", "price": 1000, "description": "High-performance laptop", "availability": true }
-
Add Product to Cart
- Method: POST
- Endpoint: /add-to-cart
- Test Case:
- Input:
{ "productId": "<product_id>", "quantity": 1 }
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Product added to cart successfully" }
- Input:
-
View Cart
-
Method: GET
-
Endpoint: /view-cart
-
Test Case:
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Cart retrieved successfully", "cart": [ { "product": { "title": "Laptop", "price": 1000, "description": "High-performance laptop" }, "quantity": 1 } ] }
- Expected Output:
-
-
Update Cart Item
- Method: PUT
- Endpoint: /update-cart-items/:itemId
- Test Case:
- Input: itemId, updated quantity
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Cart item updated successfully" }
-
Remove Cart Item
- Method: DELETE
- Endpoint: /remove-cart-items/:itemId
- Test Case:
- Input: itemId
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Cart item removed successfully" }
-
Place Order
- Method: GET
- Endpoint: /place-order
- Test Case:
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Order placed successfully", "order": { "_id": "<order_id>", "user": "<user_id>", "items": [ { "product": "<product_id>", "quantity": 1 } ], "createdAt": "<timestamp>", "updatedAt": "<timestamp>", "__v": 1 } }
- Expected Output:
-
Get Order History
- Method: GET
- Endpoint: /order-history
- Test Case:
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Order history retrieved successfully", "orders": [ { "_id": "<order_id>", "user": "<user_id>", "items": [ { "product": "<product_id>", "quantity": 1 } ], "createdAt": "<timestamp>", "updatedAt": "<timestamp>", "__v": 1 } ] }
- Expected Output:
-
Get Order Details
- Method: GET
- Endpoint: /order-details/:orderId
- Test Case:
- Input: orderId
- Expected Output:
- Status Code: 200
- Response Body:
{ "message": "Order details retrieved successfully", "order": { "_id": "<order_id>", "user": "<user_id>", "items": [ { "product": "<product_id>", "quantity": 1 } ], "createdAt": "<timestamp>", "updatedAt": "<timestamp>", "__v": 1 } }