-
Notifications
You must be signed in to change notification settings - Fork 0
/
sale_server.js
156 lines (148 loc) · 4.5 KB
/
sale_server.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
module.exports = {
client : require('./data'),
getSale : function(userData, callback){
var response = {valid:false};
this.client.dbCall(function (result){
var db = result.value;
var col = db.collection('sale');
col.find({authenticate:1}).sort({modified_date:1}).toArray(function(err, data) {
if(err){
response.message = 'no post information';
}
else if (data.length){
response.valid = true;
var size = 10;
if(data.length < 100){
size = data.length;
}
var sale_info = [];
for (var i = 0; i < size; i++) {
sale_info.unshift(data[i]);
};
response.sale_info = sale_info;
}
else {
response.message = 'no post information';
}
db.close();
callback(response);
});
})
},
deleteSale : function(userData, callback){
var response = {valid:false};
ObjectId = this.client.mongoId;
var oid = new ObjectId(userData._id);
this.client.dbCall(function (result) {
var db = result.value;
var col = db.collection('sale');
col.deleteOne({_id:oid, userID:userData.user}, function(err, result) {
if(err || result.deletedCount == 0) {
response.message = 'Error in deleting sale post';
response.valid = false;
} else {
response.message = 'Sale post removed successfully';
response.valid = true;
}
db.close();
callback(response);
});
});
},
updateSale : function(userData, callback) {
var response = {valid:false};
ObjectId = this.client.mongoId;
var oid = new ObjectId(userData._id);
this.client.dbCall(function (result) {
var db = result.value;
var col = db.collection('sale');
var modified_date = new Date();
col.updateOne({_id:oid, userID:userData.user}, { $set:{_title:userData._title,
_content:userData._content, modified_date:modified_date}}, function(err, info){
if(err || info === 0){
response.valid = false;
response.message = 'Error in updating sale post';
} else {
response.valid = true;
response.modified_date = modified_date;
response.message = 'Sale post updated successfully';
}
db.close();
callback(response);
});
});
},
getSaleContent : function(userData, callback){
var response = {valid:false};
ObjectId = this.client.mongoId;
var oid = new ObjectId(userData._id);
this.client.dbCall(function (result){
var db = result.value;
var col = db.collection('sale');
col.find({_id:oid}).toArray(function(err, data) {
if(err){
response.message = 'error';
}
else if (data.length){
var d = data[0];
response.valid = true;
response.fName = d.fName;
response.lName = d.lName;
response._title = d._title;
response._content = d._content;
response.modified_date = d.modified_date;
response.message = "post content found";
}
else {
response.message = 'no post';
}
db.close();
callback(response);
});
})
},
createSale : function(userData, callback){
var response = {valid:false};
this.client.dbCall(function (result) {
var db = result.value;
var userCol = db.collection('user');
userCol.find({_id:userData._id, pass:userData.pass, authenticate:1}).toArray(function(err, info) {
if(err) {
response.message = 'Invalid user id or password';
db.close();
callback(response);
}
else if (info.length){
var saleCol = db.collection('sale');
userData.create_date = new Date();
userData.modified_date = new Date();
userData.authenticate = 1;
userData.userID = userData._id;
userData.fName = info[0].fName;
userData.lName = info[0].lName;
delete userData._id;
delete userData.pass;
saleCol.insert(userData, function(err, data){
if(err){
response.message = 'Post failed.';
}
else {
var json = data.ops[0];
response.modified_date = json.modified_date;
response._id = json._id;
response.valid = true;
response.message = 'Post successed.';
}
db.close();
callback(response);
});
}
else {
response.message = 'Invalid user id or password';
db.close();
callback(response);
}
});
});
},
};