This is a simple REST API to handle products and producers.
At first, clone the repository:
git clone [email protected]:airtaki/frw.git
cd frw
Then, rename or copy the .env.sample
file to .env
:
mv .env.sample .env
Open your favourite editor and edit the .env
file. You can change the port, the debug mode, the environment etc. as you want, but the default values should work, except one entry. You have to modify the MONGODB_URI
, based on your own Mongo DB settings:
MONGODB_URI = 'mongodb://username:[email protected]:27017/dbname'
Now close the editor, go back to the frw
folder and install the packages:
npm install
Build the project:
npm run build
Now you can run it with node
:
node dist/index.js
Or, if you want, can be launch in developer mode too, using nodemon
package. Just simply type in:
npm start
Both cases, you should get the following message: Test task is listening on port 3000.
(If you set the APP_NAME
and/or the APP_PORT
, you'll see your entries instead of Test task
and 3000
.)
Now the API is ready to handle the requests.
GET
/producer/{id}
name type data type description id
required string the producer's ObjectId
http code content-type response 200
application/json
the producer object 404
application/json
producer not found
curl -X GET -H "Content-Type: application/json" http://localhost:3000/producer/655a3f745bb339041f09a4b8
POST
/producer
none
name type data type description name
required string the name of the producer country
optional string the country of the producer region
optional string the region of the producer
http code content-type response 201
application/json
the producer object
curl -X POST -H "Content-Type: application/json" http://localhost:3000/producer { "name": "Foo Bar", "country": "France", "region": "Bordeaux" }
PUT
/producer
name type data type description id
required string the producer's ObjectId
name type data type description name
optional string the name of the producer country
optional string the country of the producer region
optional string the region of the producer
http code content-type response 200
application/json
the updated producer object
curl -X PUT -H "Content-Type: application/json" http://localhost:3000/producer { "name": "Foo Bar", "country": "France", "region": "Bordeaux" }
DELETE
/producer
name type data type description id
required string the producer's ObjectId
http code content-type response 200
application/json
deleted count
curl -X DELETE -H "Content-Type: application/json" http://localhost:3000/producer
GET
/product/{id}
name type data type description id
required string the ObjectId of the product
http code content-type response 200
application/json
the product object 404
application/json
product not found
curl -X GET -H "Content-Type: application/json" http://localhost:3000/product/655a3f745bb339041f09a4cd
GET
/product/producer/{id}
name type data type description id
required string the ObjectId of the producer
http code content-type response 200
application/json
list of products 404
application/json
products not found with the given producer id
curl -X GET -H "Content-Type: application/json" http://localhost:3000/product/producer/655a3f745bb339041f09a4b8
POST
/product
none
name type data type description name
required string the name of the product vintage
required number the vintage of the product producer
required string the producer id of the producer
Instead of a single product, you can set an array of products too. In this case all the given products will be processed.
http code content-type response 201
application/json
list of the created product(s)
curl -X POST -H "Content-Type: application/json" http://localhost:3000/product { "name": "Foo Bar", "vintage": "2021", "producer": "655a3f745bb339041f09a4b8" } or curl -X POST -H "Content-Type: application/json" http://localhost:3000/product [{ "name": "Foo Bar", "vintage": "2021", "producer": "655a3f745bb339041f09a4b8" }, { "name": "Baz bar bar", "vintage": "2022", "producer": "655a3f745bb339041f09a53e" }]
POST
/product/csv
Keep in mind, when you call this method, it responses immediately a started: true
message with status code 202 Accepted
.
In the background, it forks a new instance, and starts to process the given CSV url. It takes 5 to 10 seconds to see the
output on the console.
name type data type description url
required string A link to a valid remote CSV file
none
http code content-type response 202
application/json
started: true
curl -X POST -H "Content-Type: application/json" http://localhost:3000/product/csv/?url=https://foobar.baz/path/to/a/valid.csv
PUT
/product
name type data type description id
required string the ObjectId of the product
name type data type description name
optional string the name of the product vintage
optional number the vintage of the product producer
optional string the producer id of the producer
http code content-type response 200
application/json
the updated product object
curl -X PUT -H "Content-Type: application/json" http://localhost:3000/product { "name": "Foo Bar", "vintage": "2021", "producer": "655a3f745bb339041f09a4b8" }
DELETE
/product
name type data type description id
required string the ObjectId of the product
http code content-type response 200
application/json
deleted count
curl -X DELETE -H "Content-Type: application/json" http://localhost:3000/product
DELETE
/product
none
name type data type description ids
required array an array, containing the Object ids of the deletable products
http code content-type response 200
application/json
deleted count
curl -X DELETE -H "Content-Type: application/json" http://localhost:3000/product { ids: ["655b3a8b5bb339041f0e4bf0", "655b3a8b5bb339041f0e4bf2"] }