mirror of
https://github.com/jlengrand/sample-node-api.git
synced 2026-03-10 08:41:23 +00:00
initial commit
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
coverage/
|
||||
node_modules/
|
||||
.vscode/
|
||||
223
data.js
Normal file
223
data.js
Normal file
@@ -0,0 +1,223 @@
|
||||
module.exports = {
|
||||
Accounts: [
|
||||
{
|
||||
'_id': '0',
|
||||
'name': {
|
||||
'first': 'Deidre',
|
||||
'last': 'Hayes'
|
||||
},
|
||||
'email': 'deidre.hayes@undefined.me',
|
||||
'phone': '+1 (839) 577-3100',
|
||||
'address': '507 Church Avenue, Heil, Wyoming, 1754'
|
||||
},
|
||||
{
|
||||
'_id': '1',
|
||||
'name': {
|
||||
'first': 'Maldonado',
|
||||
'last': 'Sellers'
|
||||
},
|
||||
'email': 'maldonado.sellers@undefined.biz',
|
||||
'phone': '+1 (834) 573-2841',
|
||||
'address': '286 Hewes Street, Abiquiu, Maine, 2447'
|
||||
},
|
||||
{
|
||||
'_id': '2',
|
||||
'name': {
|
||||
'first': 'Elvia',
|
||||
'last': 'Aguilar'
|
||||
},
|
||||
'email': 'elvia.aguilar@undefined.io',
|
||||
'phone': '+1 (826) 486-2932',
|
||||
'address': '457 Buffalo Avenue, Caberfae, Connecticut, 2648'
|
||||
},
|
||||
{
|
||||
'_id': '3',
|
||||
'name': {
|
||||
'first': 'Chris',
|
||||
'last': 'Mullins'
|
||||
},
|
||||
'email': 'chris.mullins@undefined.org',
|
||||
'phone': '+1 (884) 425-2397',
|
||||
'address': '642 Vandalia Avenue, Driftwood, California, 213'
|
||||
},
|
||||
{
|
||||
'_id': '4',
|
||||
'name': {
|
||||
'first': 'Vargas',
|
||||
'last': 'Oneal'
|
||||
},
|
||||
'email': 'vargas.oneal@undefined.info',
|
||||
'phone': '+1 (893) 576-3106',
|
||||
'address': '413 Bedford Avenue, Bynum, Federated States Of Micronesia, 1793'
|
||||
}
|
||||
],
|
||||
Cars: [
|
||||
{
|
||||
'_id': 0,
|
||||
'Name': 'chevrolet chevelle malibu',
|
||||
'Miles_per_Gallon': 18,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 307,
|
||||
'Horsepower': 130,
|
||||
'Weight_in_lbs': 3504,
|
||||
'Acceleration': 12,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 1,
|
||||
'Name': 'buick skylark 320',
|
||||
'Miles_per_Gallon': 15,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 350,
|
||||
'Horsepower': 165,
|
||||
'Weight_in_lbs': 3693,
|
||||
'Acceleration': 11.5,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 2,
|
||||
'Name': 'plymouth satellite',
|
||||
'Miles_per_Gallon': 18,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 318,
|
||||
'Horsepower': 150,
|
||||
'Weight_in_lbs': 3436,
|
||||
'Acceleration': 11,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 3,
|
||||
'Name': 'amc rebel sst',
|
||||
'Miles_per_Gallon': 16,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 304,
|
||||
'Horsepower': 150,
|
||||
'Weight_in_lbs': 3433,
|
||||
'Acceleration': 12,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 4,
|
||||
'Name': 'ford torino',
|
||||
'Miles_per_Gallon': 17,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 302,
|
||||
'Horsepower': 140,
|
||||
'Weight_in_lbs': 3449,
|
||||
'Acceleration': 10.5,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 5,
|
||||
'Name': 'ford galaxie 500',
|
||||
'Miles_per_Gallon': 15,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 429,
|
||||
'Horsepower': 198,
|
||||
'Weight_in_lbs': 4341,
|
||||
'Acceleration': 10,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 6,
|
||||
'Name': 'chevrolet impala',
|
||||
'Miles_per_Gallon': 14,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 454,
|
||||
'Horsepower': 220,
|
||||
'Weight_in_lbs': 4354,
|
||||
'Acceleration': 9,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 7,
|
||||
'Name': 'plymouth fury iii',
|
||||
'Miles_per_Gallon': 14,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 440,
|
||||
'Horsepower': 215,
|
||||
'Weight_in_lbs': 4312,
|
||||
'Acceleration': 8.5,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 8,
|
||||
'Name': 'pontiac catalina',
|
||||
'Miles_per_Gallon': 14,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 455,
|
||||
'Horsepower': 225,
|
||||
'Weight_in_lbs': 4425,
|
||||
'Acceleration': 10,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
},
|
||||
{
|
||||
'_id': 9,
|
||||
'Name': 'amc ambassador dpl',
|
||||
'Miles_per_Gallon': 15,
|
||||
'Cylinders': 8,
|
||||
'Displacement': 390,
|
||||
'Horsepower': 190,
|
||||
'Weight_in_lbs': 3850,
|
||||
'Acceleration': 8.5,
|
||||
'Year': '1970-01-01',
|
||||
'Origin': 'USA'
|
||||
}
|
||||
],
|
||||
Accounts_Cars: [
|
||||
{
|
||||
'_id': 0,
|
||||
'Account_id': 0,
|
||||
'Car_id': 0
|
||||
},
|
||||
{
|
||||
'_id': 1,
|
||||
'Account_id': 0,
|
||||
'Car_id': 1
|
||||
},
|
||||
{
|
||||
'_id': 2,
|
||||
'Account_id': 1,
|
||||
'Car_id': 2
|
||||
},
|
||||
{
|
||||
'_id': 3,
|
||||
'Account_id': 3,
|
||||
'Car_id': 3
|
||||
},
|
||||
{
|
||||
'_id': 4,
|
||||
'Account_id': 3,
|
||||
'Car_id': 5
|
||||
},
|
||||
{
|
||||
'_id': 5,
|
||||
'Account_id': 3,
|
||||
'Car_id': 7
|
||||
},
|
||||
{
|
||||
'_id': 6,
|
||||
'Account_id': 4,
|
||||
'Car_id': 6
|
||||
},
|
||||
{
|
||||
'_id': 7,
|
||||
'Account_id': 4,
|
||||
'Car_id': 4
|
||||
},
|
||||
{
|
||||
'_id': 8,
|
||||
'Account_id': 4,
|
||||
'Car_id': 8
|
||||
}
|
||||
]
|
||||
}
|
||||
2067
package-lock.json
generated
Normal file
2067
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
package.json
Normal file
19
package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "sample-api",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node server/server.js",
|
||||
"test": "istanbul cover cucumber-js -- test/features/*feature --require test/support/ --report cobertura --report html --tags \"not @leave\""
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"cucumber": "^5.1.0",
|
||||
"express": "^4.16.4",
|
||||
"supertest": "^3.4.2",
|
||||
"assert": "^1.4.1",
|
||||
"istanbul": "^0.4.5"
|
||||
}
|
||||
}
|
||||
13
server/app.js
Normal file
13
server/app.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
const routes = require('./routes/index.route');
|
||||
|
||||
app.get('/', (req, res) => res.send('Hello World!'));
|
||||
|
||||
app.use(routes);
|
||||
|
||||
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
||||
|
||||
module.exports = { app };
|
||||
14
server/controllers/accounts.controller.js
Normal file
14
server/controllers/accounts.controller.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const accountsService = require('../services/accounts.service');
|
||||
|
||||
const get = function(req, res){
|
||||
res.send(accountsService.get(req.params._id));
|
||||
}
|
||||
|
||||
const getAll = function(req, res){
|
||||
res.send(accountsService.getAll());
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
getAll
|
||||
};
|
||||
14
server/controllers/accountsCars.controller.js
Normal file
14
server/controllers/accountsCars.controller.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const accountsCarsService = require('../services/accountsCars.service');
|
||||
|
||||
const get = function(req, res){
|
||||
res.send(accountsCarsService.get(req));
|
||||
}
|
||||
|
||||
const getAll = function(req, res){
|
||||
res.send(accountsCarsService.getAll(req));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
getAll
|
||||
};
|
||||
14
server/controllers/cars.controller.js
Normal file
14
server/controllers/cars.controller.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const carsService = require('../services/cars.service');
|
||||
|
||||
const get = function(req, res){
|
||||
res.send(carsService.get(req.params._id))
|
||||
}
|
||||
|
||||
const getAll = function(req, res){
|
||||
res.send(carsService.getAll())
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
getAll
|
||||
};
|
||||
15
server/routes/accounts.route.js
Normal file
15
server/routes/accounts.route.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const express = require('express');
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
const accountsController = require('../controllers/accounts.controller');
|
||||
const accountsCarsRoute = require('./accountsCars.route');
|
||||
|
||||
router.route('/')
|
||||
.get(accountsController.getAll);
|
||||
|
||||
router.route('/:_id')
|
||||
.get(accountsController.get);
|
||||
|
||||
router.use('/:Account_id', accountsCarsRoute);
|
||||
|
||||
module.exports = router;
|
||||
12
server/routes/accountsCars.route.js
Normal file
12
server/routes/accountsCars.route.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const express = require('express');
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
const accountsCarsController = require('../controllers/accountsCars.controller');
|
||||
|
||||
router.route('/cars')
|
||||
.get(accountsCarsController.getAll);
|
||||
|
||||
router.route('/cars/:_id')
|
||||
.get(accountsCarsController.get);
|
||||
|
||||
module.exports = router;
|
||||
12
server/routes/cars.route.js
Normal file
12
server/routes/cars.route.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const express = require('express');
|
||||
const router = express.Router({ mergeParams: true });
|
||||
|
||||
const carsController = require('../controllers/cars.controller');
|
||||
|
||||
router.route('/')
|
||||
.get(carsController.getAll);
|
||||
|
||||
router.route('/:_id')
|
||||
.get(carsController.get);
|
||||
|
||||
module.exports = router;
|
||||
10
server/routes/index.route.js
Normal file
10
server/routes/index.route.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const express = require('express');
|
||||
const cars = require('./cars.route');
|
||||
const accounts = require('./accounts.route');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/cars', cars);
|
||||
router.use('/accounts', accounts);
|
||||
|
||||
module.exports = router;
|
||||
7
server/server.js
Normal file
7
server/server.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const app = require("./app");
|
||||
|
||||
const port = 3000;
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`starter listening on http://localhost:${port}`)
|
||||
});
|
||||
14
server/services/accounts.service.js
Normal file
14
server/services/accounts.service.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const data = require('../../data');
|
||||
|
||||
const get = function(_id){
|
||||
return getAll().find(account => account._id == _id);
|
||||
}
|
||||
|
||||
const getAll = function(){
|
||||
return data.Accounts;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
getAll
|
||||
};
|
||||
25
server/services/accountsCars.service.js
Normal file
25
server/services/accountsCars.service.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const data = require('../../data');
|
||||
|
||||
const carsService = require('./cars.service');
|
||||
|
||||
const get = function(req){
|
||||
return getAll(req).find(car => car._id == req.params._id);
|
||||
}
|
||||
|
||||
const getAll = function(req){
|
||||
const cars = carsService.getAll();
|
||||
|
||||
const car_ids = data.Accounts_Cars
|
||||
.filter(
|
||||
row => row.Account_id == req.params.Account_id
|
||||
).map(
|
||||
row => row.Car_id
|
||||
);
|
||||
|
||||
return cars.filter(car => car_ids.includes(car._id));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
getAll
|
||||
};
|
||||
14
server/services/cars.service.js
Normal file
14
server/services/cars.service.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const data = require('../../data');
|
||||
|
||||
const get = function(_id){
|
||||
return getAll().find(car => car._id == _id);
|
||||
}
|
||||
|
||||
const getAll = function(){
|
||||
return data.Cars;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
getAll
|
||||
};
|
||||
61
test/features/endpoint.feature
Normal file
61
test/features/endpoint.feature
Normal file
@@ -0,0 +1,61 @@
|
||||
@1
|
||||
Feature: initial REST feature
|
||||
As a <role>
|
||||
I can <do something>
|
||||
So that <I can get some value>
|
||||
|
||||
Scenario: connect
|
||||
When I GET the endpoint "/"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
Hello World!
|
||||
"""
|
||||
|
||||
Scenario: get cars
|
||||
When I GET the endpoint "/cars"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
[{"_id":0,"Name":"chevrolet chevelle malibu","Miles_per_Gallon":18,"Cylinders":8,"Displacement":307,"Horsepower":130,"Weight_in_lbs":3504,"Acceleration":12,"Year":"1970-01-01","Origin":"USA"},{"_id":1,"Name":"buick skylark 320","Miles_per_Gallon":15,"Cylinders":8,"Displacement":350,"Horsepower":165,"Weight_in_lbs":3693,"Acceleration":11.5,"Year":"1970-01-01","Origin":"USA"},{"_id":2,"Name":"plymouth satellite","Miles_per_Gallon":18,"Cylinders":8,"Displacement":318,"Horsepower":150,"Weight_in_lbs":3436,"Acceleration":11,"Year":"1970-01-01","Origin":"USA"},{"_id":3,"Name":"amc rebel sst","Miles_per_Gallon":16,"Cylinders":8,"Displacement":304,"Horsepower":150,"Weight_in_lbs":3433,"Acceleration":12,"Year":"1970-01-01","Origin":"USA"},{"_id":4,"Name":"ford torino","Miles_per_Gallon":17,"Cylinders":8,"Displacement":302,"Horsepower":140,"Weight_in_lbs":3449,"Acceleration":10.5,"Year":"1970-01-01","Origin":"USA"},{"_id":5,"Name":"ford galaxie 500","Miles_per_Gallon":15,"Cylinders":8,"Displacement":429,"Horsepower":198,"Weight_in_lbs":4341,"Acceleration":10,"Year":"1970-01-01","Origin":"USA"},{"_id":6,"Name":"chevrolet impala","Miles_per_Gallon":14,"Cylinders":8,"Displacement":454,"Horsepower":220,"Weight_in_lbs":4354,"Acceleration":9,"Year":"1970-01-01","Origin":"USA"},{"_id":7,"Name":"plymouth fury iii","Miles_per_Gallon":14,"Cylinders":8,"Displacement":440,"Horsepower":215,"Weight_in_lbs":4312,"Acceleration":8.5,"Year":"1970-01-01","Origin":"USA"},{"_id":8,"Name":"pontiac catalina","Miles_per_Gallon":14,"Cylinders":8,"Displacement":455,"Horsepower":225,"Weight_in_lbs":4425,"Acceleration":10,"Year":"1970-01-01","Origin":"USA"},{"_id":9,"Name":"amc ambassador dpl","Miles_per_Gallon":15,"Cylinders":8,"Displacement":390,"Horsepower":190,"Weight_in_lbs":3850,"Acceleration":8.5,"Year":"1970-01-01","Origin":"USA"}]
|
||||
"""
|
||||
|
||||
Scenario: get car
|
||||
When I GET the endpoint "/cars/0"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
{"_id":0,"Name":"chevrolet chevelle malibu","Miles_per_Gallon":18,"Cylinders":8,"Displacement":307,"Horsepower":130,"Weight_in_lbs":3504,"Acceleration":12,"Year":"1970-01-01","Origin":"USA"}
|
||||
"""
|
||||
|
||||
Scenario: get accounts
|
||||
When I GET the endpoint "/accounts"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
[{"_id":"0","name":{"first":"Deidre","last":"Hayes"},"email":"deidre.hayes@undefined.me","phone":"+1 (839) 577-3100","address":"507 Church Avenue, Heil, Wyoming, 1754"},{"_id":"1","name":{"first":"Maldonado","last":"Sellers"},"email":"maldonado.sellers@undefined.biz","phone":"+1 (834) 573-2841","address":"286 Hewes Street, Abiquiu, Maine, 2447"},{"_id":"2","name":{"first":"Elvia","last":"Aguilar"},"email":"elvia.aguilar@undefined.io","phone":"+1 (826) 486-2932","address":"457 Buffalo Avenue, Caberfae, Connecticut, 2648"},{"_id":"3","name":{"first":"Chris","last":"Mullins"},"email":"chris.mullins@undefined.org","phone":"+1 (884) 425-2397","address":"642 Vandalia Avenue, Driftwood, California, 213"},{"_id":"4","name":{"first":"Vargas","last":"Oneal"},"email":"vargas.oneal@undefined.info","phone":"+1 (893) 576-3106","address":"413 Bedford Avenue, Bynum, Federated States Of Micronesia, 1793"}]
|
||||
"""
|
||||
|
||||
Scenario: get account
|
||||
When I GET the endpoint "/accounts/0"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
{"_id":"0","name":{"first":"Deidre","last":"Hayes"},"email":"deidre.hayes@undefined.me","phone":"+1 (839) 577-3100","address":"507 Church Avenue, Heil, Wyoming, 1754"}
|
||||
"""
|
||||
|
||||
Scenario: get cars with account
|
||||
When I GET the endpoint "/accounts/0/cars"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
[{"_id":0,"Name":"chevrolet chevelle malibu","Miles_per_Gallon":18,"Cylinders":8,"Displacement":307,"Horsepower":130,"Weight_in_lbs":3504,"Acceleration":12,"Year":"1970-01-01","Origin":"USA"},{"_id":1,"Name":"buick skylark 320","Miles_per_Gallon":15,"Cylinders":8,"Displacement":350,"Horsepower":165,"Weight_in_lbs":3693,"Acceleration":11.5,"Year":"1970-01-01","Origin":"USA"}]
|
||||
"""
|
||||
|
||||
Scenario: get cars with account
|
||||
When I GET the endpoint "/accounts/0/cars"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
[{"_id":0,"Name":"chevrolet chevelle malibu","Miles_per_Gallon":18,"Cylinders":8,"Displacement":307,"Horsepower":130,"Weight_in_lbs":3504,"Acceleration":12,"Year":"1970-01-01","Origin":"USA"},{"_id":1,"Name":"buick skylark 320","Miles_per_Gallon":15,"Cylinders":8,"Displacement":350,"Horsepower":165,"Weight_in_lbs":3693,"Acceleration":11.5,"Year":"1970-01-01","Origin":"USA"}]
|
||||
"""
|
||||
|
||||
Scenario: get account
|
||||
When I GET the endpoint "/accounts/0/cars/0"
|
||||
Then I should get a 200 with body
|
||||
"""
|
||||
{"_id":0,"Name":"chevrolet chevelle malibu","Miles_per_Gallon":18,"Cylinders":8,"Displacement":307,"Horsepower":130,"Weight_in_lbs":3504,"Acceleration":12,"Year":"1970-01-01","Origin":"USA"}
|
||||
"""
|
||||
21
test/support/steps.js
Normal file
21
test/support/steps.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const { When, Then } = require("cucumber")
|
||||
const assert = require("assert")
|
||||
|
||||
const request = require("supertest");
|
||||
const app = require(process.cwd() + "/server/app").app;
|
||||
|
||||
When('I GET the endpoint {string}', function (path) {
|
||||
this.request = request(app).get(path);
|
||||
});
|
||||
|
||||
Then('I should get a {int} with body', function (status, body, done) {
|
||||
this.request.expect(status).expect(body).end(done);
|
||||
});
|
||||
|
||||
Then('I should get the message', function (message) {
|
||||
if (this.response + "" !== message) {
|
||||
console.log("EXPECTED", message)
|
||||
console.log("ACTUAL", this.response + "")
|
||||
}
|
||||
assert(this.response + "" == message);
|
||||
});
|
||||
Reference in New Issue
Block a user