The first stage which involved building the necessary APIs for user creation and management is complete. All parties interested in testing are welcome to do so.
- NodeJs version v18.13.0 or higher
- MongoDB
db version v6.0.4
Build Info: {
"version": "6.0.4",
"gitVersion": "44ff59461c1353638a71e710f385a566bcd2f547",
"openSSLVersion": "OpenSSL 1.1.1f 31 Mar 2020",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "ubuntu2004",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
- Project was built on Ubuntu 22.04.1 LTS x86_64, Host: 81H7 Lenovo ideapad 130-15IKB, Kernel: 5.15.0-58-generic
To get started, run npm run server
from the root directory
The base URL is
Localhost:PORT/api
To test the User API, use the path
Localhost:PORT/api/user
POST /user/register
Content-Type: application/json
{
"firstname": "John",
"lastname": "Doe",
"email": "[email protected]",
"mobile": "1234567890",
"password": "password123"
}
POST /user/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "password123"
}
Response includes JWT token for authentication.
GET /user/logout
Authorization: Bearer <token>
Requires admin privileges:
GET /user/all-users # Get all users
GET /user/:id # Get specific user
PUT /user/edit-user # Update user
PUT /user/block-user/:id # Block user
PUT /user/unblock-user/:id # Unblock user
DELETE /user/:id # Delete user
POST /product
Authorization: Bearer <admin_token>
Content-Type: application/json
{
"title": "iPhone 13",
"description": "Latest iPhone model",
"price": 999,
"category": "Smartphone",
"brand": "Apple",
"quantity": 100,
"color": "Midnight Blue",
"images": ["image_url1", "image_url2"]
}
GET /product/products # Get all products (sorted)
GET /product/:id # Get specific product
GET /product/dev # Get all products (development)
GET /product/products?brand=Apple
GET /product/products?category=Smartphone
GET /product/products?price[gte]=500&price[lte]=1000
GET /product/products?sort=price
GET /product/products?sort=-price # Descending order
GET /product/products?sort=title,price
GET /product/products?fields=title,price,brand
GET /product/products?page=1&limit=10
PUT /product/:id
Authorization: Bearer <admin_token>
Content-Type: application/json
{
"price": 899,
"quantity": 50
}
DELETE /product/:id
Authorization: Bearer <admin_token>
POST /cart/add
Authorization: Bearer <token>
Content-Type: application/json
{
"productId": "product_id",
"quantity": 1
}
PUT /cart/update
Authorization: Bearer <token>
Content-Type: application/json
{
"productId": "product_id",
"action": "increment" # or "decrement"
}
This deletes a product entirely from the cart, to reduce quantity, refer to 3.2 above.
DELETE /cart/remove/:productId
Authorization: Bearer <token>
GET /cart
Authorization: Bearer <token>
- Initial Setup
- Install dependencies: npm install
- Set up MongoDB locally or update connection string
- Create .env file with required variables:
PORT=5000 MONGODB_URL=mongodb://localhost:27017/Ecommerce JWT_SECRET=your_secret_key
- User Authentication
- Register new user
router.post("/register" ,createUser);//new user creation
- Login to get token
- Test protected routes with token
- Admin Operations
- Create admin user
- Test product management
- Test user management
- Product Operations
- Create products (admin)
- Test product filtering
- Test sorting
- Test pagination
- Cart Operations
- Add items to cart
- Update quantities
- Remove items
- View cart
Check the user model under User Model for more info on the requirements for a user/admin
router.post("/login",loginUserCtrl);// user login
router.get("/logout",logout);// user logout
router.get("/refresh",handleRefreshToken);//handle Refresh Token
router.get("/all-users",getAllUsers);// get all users
router.get("/:id",authMiddleware,isAdmin,getAUser);// get a user - only admin can get user
router.delete("/:id",deleteAUser);// delete a user
router.put("/edit-user",authMiddleware,isAdmin,updateAUser);// update a user - only admin can update user
router.put("/block-user/:id",authMiddleware,isAdmin,blockAUser);// block a user - only admin can block/unblock user
router.put("/unblock-user/:id",authMiddleware,isAdmin,unblockAUser);// ubblock a user - only admin can block/unblock user
The product API is now set for testing. To test the API, use the route /api/product
Below is some more information
router.post("/", authMiddleware, isAdmin, createProduct);//create a new product
router.get("/dev", getAllProducts);//get all products, no fields ommited: for development purposes
router.get("/products/", getAllProductsSorted);//get all products and sort accordingly
router.get("/:id", getAProduct);//get a product
router.put("/:id", authMiddleware, isAdmin, updateProduct);//update a product
router.delete("/:id", authMiddleware, isAdmin, deleteAProduct);//delete a product
The cart API is now set for testing. To test the API, use the route /api/cart
To add a product to the cart, send a POST request to
Localhost:PORT/api/cart/add
with the following body:
// POST localhost:5000/api/cart/add
// Headers:
{
"Authorization": "Bearer your-token-here",
"Content-Type": "application/json"
}
// Body:
{
"productId": "ID OF THE PRODUCT",
"quantity": 1
}
To get the cart, send a GET request to
Localhost:PORT/api/cart
Up to this point, the project has been built without the assistance of AI technologies. Date: 02/11/2024. Preceding updates will be built using AI technologies. Specifically, I use Cursor AI to build the project.
- Node.js v20.18.0
- MongoDB v7.0.4
- Express v4.18.2
- Mongoose v8.0.3
- VS Code v1.85.1
- Postman v10.21.9
- Git v2.43.0