The Task is Building a RESTful APIs from scratch using Express - Node.js.
- The Entities are a “Categories” and “Products”.
- Category can have multiple child categories.
- Child category can have further child categories.
- Category can have multiple products and product can have a multiple categories.
- The Entities must get saved in MongoDb and be retrieved via POST and GET Methods respectively.
-
You need to design a proper Mongodb data model and create APIs to
-
Add a category
Request
POST http://localhost:3000/categories/ Content-Type: application/json { "Name": "Category {{$randomInt 0 1000}}", "Description": "test category description", "ParentCategory": { "_id": "5f8881e0a0a9797f277597f8" } }
-
Add Product mapped to a category or categories.
Request
POST http://localhost:3000/products/ Content-type: application/json { "Name": "X Product {{$randomInt 0 1000}}", "Brand": "xyz", "Sku": "123", "Price": 1200, "Categories": [ { "Name": "test category {{$randomInt 0 1000}}", "Description": "this is test category" }, { "Name": "test category 11 {{$randomInt 0 1000}}", "Description": "this is test category" } ] }
-
Get all categories with all its child categories mapped to it. Note : Each category object should look something like this {Id : 1 , child_categories: [], ...}
Request
GET http://localhost:3000/categories/
-
Get all products by a category.
Request
GET http://localhost:3000/products/?category[_id]=5f89299c4e29a3c1970bb9cf
-
Update product details (name,price,etc)
Request
PUT http://localhost:3000/products/5f892b6bfedb75cf01460ca3 Content-Type: application/json { "Name": "X Product v {{$randomInt 0 1000}}", "Brand": "xyz", "Sku": "123", "Price": 1200, "Categories": [ { "Name": "test category x {{$randomInt 0 1000}}", "Description": "this is test category" }, { "Name": "test category x update {{$randomInt 0 1000}}", "Description": "this is test category" } ] }
Use "REST Client" extension for VS Code to emulate request for development
"Request.rest" File has implemented requests for quick start
https://github.com/Huachao/vscode-restclient
- docker-compose
- docker-compose.yml file at root has development mongo instance listening on 27017
- If using docker-compose you might need to stop local mongodb instance or change port
-
Clone this repo
-
Install dependencies by
npm install
-
Run service in development mode using nodemon watching for file changes
npm run dev