Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
* create a `cors-middleware` module that will allow for public use of your API
* create the `deleteItem` and `availIDs` methods and add them to your `storage` module
* these methods should be used to delete a resource (`deleteItem`) and return an array of id's from persisted resource filenames (`availIDs`)
* create the `updateNote`, `fetchNote`, and `fetchIDs` static methods as part of your `Note` model
* create a series of `note-route-tests` to test your **GET**, **POST**, and **PUT** routes
* **hint:** *you'll want to use the `before` and `after` hooks provided by `mocha` in order to create a test note and delete the note after the test has completed*
* create the `updateCar`, `fetchCar`, and `fetchIDs` static methods as part of your `Car` model
* create a series of `car-route-tests` to test your **GET**, **POST**, and **PUT** routes
* **hint:** *you'll want to use the `before` and `after` hooks provided by `mocha` in order to create a test car and delete the car after the test has completed*
6 changes: 6 additions & 0 deletions lib/cors-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
module.exports = function (req, res, next){
res.append('Access-Control-Allow-Origin', '*');
res.append('Access-Control-Allow-Headers', '*');
next();
}
20 changes: 20 additions & 0 deletions lib/error-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const createError = require('http-errors');
const debug = require('debug')('car: error-middleware');

module.exports = function(err, req, res, next) {

if(err.status){
debug('user error');

res.status(err.status).send(err.name);
next();
return;
}

debug('server error');
err = createError(500, err.message);
res.status(err.status).send(err.name);
next();
};
54 changes: 54 additions & 0 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'), { suffix: 'Prom' });
const createError = require('http-errors');
const debug = require('debug')('car:storage');

module.exports = exports = {};

exports.createItem = function (schemaName, item) {
debug('createItem');

if (!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if (!item) return Promise.reject(createError(400, 'expected item'));

let json = JSON.stringify(item);
return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${item.id}.json`, json)
.then(() => item)
.catch(err => Promise.reject(createError(500, err.message)));
};

exports.fetchItem = function (schemaName, id) {
debug('fetchItem');

if (!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if (!id) return Promise.reject(createError(400, 'expected id'));

return fs.readFileProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.then(data => {
try {
let item = JSON.parse(data.toString())
return item;
} catch (err) {
return Promise.reject(createError(500, err.message));
}
})
.catch(err => Promise.reject(createError(404, err.message)));
};

exports.deleteItem = function (schemaName, id) {
debug('deleteItem');

if (!schemaName) return Promise.reject(createError(400, 'expected schema name'));
if (!id) return Promise.reject(createError(400, 'expected id'));

return fs.unlinkProm(`${__dirname}/../data/${schemaName}/${id}.json`)
.catch(err => Promise.reject(createError(404, err.message)));
}

exports.availIDs = function (schemaName) {
return fs.readdirProm(`${__dirname}/../data/${schemaName}`)
.then(files => files.map(name => name.split('.json')[0]))
.catch(err => Promise.reject(createError(404, err.message)));
}
57 changes: 57 additions & 0 deletions model/car.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const uuidv4 = require('uuid/v4');
const createError = require('http-errors');
const debug = require('debug')('car:car');
const storage = require('../lib/storage.js');

const Car = module.exports = function (name, brand) {
debug('car constructor');

if (!name) throw new Error('expected name');
if (!brand) throw new Error('expected brand');

this.id = uuidv4();
this.name = name;
this.brand = brand;
};

Car.createCar = function (_car) {
debug('createCar');

try {
let car = new Car(_car.name, _car.brand);
return storage.createItem('car', car);
} catch (err) {
return Promise.reject(err);
}
}

Car.fetchCar = function (id) {
debug('fetchCar');
return storage.fetchItem('car', id);
}

Car.updateCar = function (id, _car) {
debug('updateCar');

return storage.fetchItem('car', id)
.catch(err => Promise.reject(createError(404, err.message)))
.then(car => {
for (var prop in car) {
if (prop === 'id') continue;
if (_car[prop]) car[prop] = _car[prop];
}
return storage.createItem('car', car);
});
}

Car.deleteCar = function (id) {
debug('deleteCar');
return storage.deleteItem('car', id);
}

Car.fetchIDs = function () {
debug('fetchIDs');
return storage.availIDs('car');
}
1 change: 1 addition & 0 deletions node_modules/.bin/_mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mocha

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/uuid

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

212 changes: 212 additions & 0 deletions node_modules/accepts/HISTORY.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading