ecomm/README.md

252 lines
3.5 KiB
Markdown
Raw Normal View History

2023-06-11 10:01:50 +00:00
# Ecomm Backend
This repository contains the backend implementation for the Ecomm e-commerce application.
## Description
2023-06-11 10:17:55 +00:00
This is a simple e-commerce backend application built with Node.js, TypeScript, Express.js, MongoDB, and Docker. It provides API endpoints to manage users, products and cart of user.
2023-06-11 10:01:50 +00:00
2023-06-11 10:29:39 +00:00
## My Approach
Creating a simple Express.js application with TypeScript.
added the required dependencies and dev dependencies.
added best ORM to connect with MongoDB and Node.js.
created the required models and controllers for the application.
added the required routes for the application.
added the required environment variables for the application.
added the required middleware for the application.
added the required error handling for the application.
2023-06-11 10:01:50 +00:00
## Table of Contents
2023-06-11 10:09:27 +00:00
- [Technologies Used]
- [How to Run]
- [API Documentation]
- [Users]
- [Create a New User]
- [Login]
- [Products]
- [Get All Products]
2023-06-11 10:01:50 +00:00
## Technologies Used
- Node.js
- TypeScript
- Express.js
- MongoDB
- Mongoose ORM
- Docker (docker-compose)
- bcrypt
- JWT
2023-06-11 10:43:02 +00:00
- deep-email-validator
2023-06-11 10:01:50 +00:00
## How to Run
To run the Ecomm backend application, follow these steps:
1. Clone the repository.
2. Ensure that you have Docker and Docker Compose installed.
3. Implement the required environment variables by creating an `.env` file.
4. Run the following command in the root directory:
2023-06-11 10:43:38 +00:00
```
docker-compose up
```
2023-06-11 10:01:50 +00:00
The application will be running on port 3000, and the database will be running on port 27017.
API Documentation
2023-06-11 10:05:40 +00:00
# Users
2023-06-11 10:01:50 +00:00
Create a New User - POST /users
2023-06-11 10:09:27 +00:00
Creates a new user.
2023-06-11 10:01:50 +00:00
Request Body
```
{
"name": "string",
"email": "string",
"password": "string"
}
```
Response Body
2023-06-11 10:15:20 +00:00
status code 200
2023-06-11 10:01:50 +00:00
```
{
2023-06-11 10:15:20 +00:00
message:
"User created successfully"
}
```
status code 400
```
{
message:
"User already exists"
}
```
status code 500
```
{
message:
"Internal server error"
}
2023-06-11 10:01:50 +00:00
```
2023-06-11 10:15:20 +00:00
Login - POST /users/login
Logs in a user.
2023-06-11 10:01:50 +00:00
2023-06-11 10:15:20 +00:00
Request Body
```
{
"email": "string",
"password": "string"
}
```
Response Body
status code 200
```
{
access-token:
"TOKEN"
}
```
# Products
Get All Products - GET /products
Gets all products.
Parameters
```
page: number(default: 0)
limit: number(default: 50)
```
Response Body
status code 200
```
{
products: [
{
_id: "string",
name: "string",
description: "string",
price: number,
image: "string",
createdAt: "string",
updatedAt: "string"
}
]
}
```
status code 404
```
{
message:
"Product not found."
}
````
2023-06-11 10:17:55 +00:00
# Cart
2023-06-11 10:29:39 +00:00
2023-06-11 10:17:55 +00:00
Add Product to Cart - POST /cart
Adds a product to cart.
(authentication required)
Request Body
```
{
"productId": "string"
}
```
Response Body
status code 200
```
{
Cart :{
"{ProductId}": Quantity
}
}
```
2023-06-11 10:29:39 +00:00
status code 500
```
{
message:
"An error occurred while adding the product to the cart."
}
```
List Cart Items - GET /cart
Lists all cart items.
(authentication required)
2023-06-11 10:17:55 +00:00
2023-06-11 10:29:39 +00:00
Response Body
status code 200
```
{
Cart :{
"{ProductId}": Quantity
}
}
```
status 404
```
{
message:
"Cart not found."
}
```
status code 500
```
{
message:
"An error occurred while listing the cart."
}
```
checkout - POST /cart/checkout
checkout the cart.
(authentication required)
status 200
```
{
order: {
{productId}: quantity
}
}
```
status 404
```
{
message:
"Cart not found."
}
2023-06-11 10:35:09 +00:00
```
# Database Schema
## User
```
{
name: string,
email: string,
password: string,
cart: {
productId: number
}
}
2023-06-11 10:45:29 +00:00
```
## Product
```
{
name: string,
description: string,
price: number
userId: string
}
```
## Cart
```
{
userId: string,
2023-06-11 10:49:15 +00:00
products: Map<string, number>
2023-06-11 10:45:29 +00:00
}
```