forked from Leveton/nodetuts
/
products.js
50 lines (45 loc) · 835 Bytes
/
products.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var products = [
{
id: 1,
name: 'Mac Book Pro',
description: 'Apple 13 inch Mac Book Pro Notebook',
price: 1000
},
{
id: 2,
name: 'iPad',
description: 'Apple 64GB 3G iPad',
price: 899
}
];
module.exports.all = products;
module.exports.find = function(id){
id = parseInt(id, 10);
var found = null;
prodductloop: for(product_index in products) {
var product = products[product_index];
if(product.id == id){
found = product;
break prodductloop;
}
};
return found;
}
module.exports.set = function(id, product){
id = parseInt(id, 10);
product.id = id;
products[id - 1] = product;
};
module.exports.new = function(){
return{
name: '',
description: '',
price: 0
};
}
module.exports.insert = function(product){
var id = products.length + 1;
product.id = id;
products[id - 1] = product;
return id;
}