Created
December 4, 2017 11:43
-
-
Save ThorstenHans/6822c1315b4dc6629da0413c3ae558c1 to your computer and use it in GitHub Desktop.
restify demo - products.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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