-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.js
64 lines (55 loc) · 1.29 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const log = require('./log-util')();
var id = 0;
/**
* 生成自增id
*/
function generateId() {
id++;
return 'p' + id;
}
/**
* 构造函数
* @param {*} name
* @param {*} company
* @param {*} price
*/
function Product(name, company, price) {
this.id = generateId();
this.name = name;
this.company = company;
this.price = price;
}
/**
* 模拟数据库数据源
*/
var products = [new Product('iphone7', "Apple", 6800), new Product('mi6', 'XiaoMi', 2399), new Product('imoo', 'BBK', 1699)];
module.exports = {
getProducts: () => {
return products;
},
getProduct: (id) => {
return products.find((element, index, arr) => {
return element.id == id;
});
},
createProduct: (name, company, price) => {
var product = new Product(name, company, price);
products.push(product);
return product;
},
deleteProduct: (id) => {
var flag = -1;
for (var index = 0; index < products.length; index++) {
var element = products[index];
if (element.id === id) {
flag = index;
return;
}
}
if (flag != -1) {
return products.splice(flag, 1)[0];
}
return null;
}
}