-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.js
93 lines (85 loc) · 2.15 KB
/
product.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//npm install tingodb
var Db = require('tingodb')().Db,
assert = require('assert');
var db = new Db('database', {});
//npm install flow
var flow = require("flow");
var category = require('./category');
// product collection
var productCollection = db.collection("product");
//insertProduct({productname:'product 2', categoryid: 3});
//updateProduct({productid : 4, productname: "product 3", categoryid: 4});
//removeProduct(5);
/*getAllProduct(function(err,data) {
if(err) {
console.log("errror " , err);
return;
}
console.log(data);
});*/
/*getProduct(2,function(err,data) {
if(err) {
console.log("errror " , err);
return;
}
console.log(data);
});*/
module.exports =
{
insertProduct : function(productObj,callback) {
productCollection.insert(productObj,
function(err, results) {
if(callback)
callback(err,results);
});
},
updateProduct : function(productObj,callback) {
productCollection.update(
{ "_id" : productObj.productid },
{ $set: { productname: productObj.productname,
categoryid: productObj.categoryid} },
function(err, results) {
if(callback)
callback(err,results);
});
},
removeProduct : function(productid,callback) {
productCollection.remove(
{ "_id" : productid },
function(err, results) {
if(callback)
callback(err,results);
});
},
getAllProduct : function(callback){
productCollection.find().toArray(function(err, docs) {
var productCount = docs.length;
var returnList = new Array;
flow.serialForEach(docs, function(doc) {
var serialController = this;
category.getCategory(doc.categoryid, function(err, data){
doc.categoryname = data.categoryname;
returnList.push(doc);
serialController();
});
},
function () {
//for each item
},
function () {
//Final
if(callback)
callback(null,returnList);
});
});
},
getProduct : function(productid,callback){
productCollection.findOne({_id : productid }, function(err, doc) {
category.getCategory(doc.categoryid,function(err,dataCategory) {
doc.categoryname = dataCategory.categoryname;
if(callback)
callback(null,doc);
});
});
}
}