Skip to content

Instantly share code, notes, and snippets.

@ThorstenHans
Created December 4, 2017 11:43
Show Gist options
  • Save ThorstenHans/6822c1315b4dc6629da0413c3ae558c1 to your computer and use it in GitHub Desktop.
Save ThorstenHans/6822c1315b4dc6629da0413c3ae558c1 to your computer and use it in GitHub Desktop.
restify demo - products.controller.js
class ProductsController {
constructor() {
this._storage = [];
}
_findProductById(id) {
const product = this._storage.find(product => product.id === id);
if (!product) {
throw new Error('Product not found.');
}
return product;
}
getAll() {
return this._storage;
}
getById(id) {
return this._findProductById(id);
}
create(id, name) {
const newProduct = {
id: id,
name: name
};
this._storage.push(newProduct);
}
update(id, name) {
const foundProduct = this._findProductById(id);
foundProduct.name = name;
return foundProduct;
}
del(id) {
const foundProduct = this._findProductById(id);
this._storage.splice(this._storage.indexOf(foundProduct), 1);
}
}
module.exports = new ProductsController();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment