-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
359 lines (314 loc) · 11.3 KB
/
server.js
File metadata and controls
359 lines (314 loc) · 11.3 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Module dependencies.
var application_root = __dirname,
express = require( 'express' ); //Web framework
path = require( 'path' ), //Utilities for dealing with file paths
mongoose = require( 'mongoose' ); //Used for accessing a MongoDB database
//Connect to database
mongoose.connect( 'mongodb://localhost/discount' );
//Create server
var app = express();
var async = require("async");
//Schema
var DiscountSchema = new mongoose.Schema({
title: String
, is_active :{ type: Number, default: 1}
, created_at : { type: Date }
, updated_at : { type: Date }
});
// Function
DiscountSchema.pre('save', function(next){
now = new Date();
this.updated_at = now;
if ( !this.created_at ) {
this.created_at = now;
}
next();
});
//Model
var DiscountModel = mongoose.model( 'Discount', DiscountSchema );
// MYSQL
var Sequelize = require('sequelize')
, sequelize = new Sequelize("invo", "root", "135790", {
dialect: "mysql",
port: 3306
});
sequelize
.authenticate()
.complete(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
});
var Discount = sequelize.define('discount', {
id: { type: Sequelize.INTEGER, primaryKey: true, unique: true},
title: Sequelize.STRING,
percent: {
type:Sequelize.DECIMAL(10,2)
/*,
get : function() {
return 100;
}*/
},
price: Sequelize.DECIMAL(10,2),
date_from: Sequelize.STRING,
date_to: Sequelize.STRING,
created_at: Sequelize.STRING,
updated_at: Sequelize.STRING,
user_id: Sequelize.INTEGER
}, {
underscored: true,
tableName: 'discount',
freezeTableName: true,
timestamps: false/*,
getterMethods : {
percentСonvert : function() {
return 100;
}
}*/
});
var Catalog = sequelize.define('catalog', {
id: { type: Sequelize.INTEGER, primaryKey: true, unique: true},
title: Sequelize.STRING,
lft: Sequelize.INTEGER,
rgt: Sequelize.INTEGER,
level: Sequelize.INTEGER
}, {
underscored: true,
tableName: 'catalog',
freezeTableName: true,
timestamps: false
});
var Vendor = sequelize.define('vendor', {
id: { type: Sequelize.INTEGER, primaryKey: true, unique: true},
title: Sequelize.STRING,
phone: Sequelize.STRING,
street: Sequelize.STRING,
user_id: Sequelize.INTEGER,
created_at: Sequelize.STRING
}, {
underscored: true,
tableName: 'vendor',
freezeTableName: true,
timestamps: false
});
var User = sequelize.define('users', {
id: { type: Sequelize.INTEGER, primaryKey: true, unique: true},
username: Sequelize.STRING,
password: Sequelize.STRING,
name: Sequelize.STRING,
email: Sequelize.STRING,
created_at: Sequelize.STRING,
active: Sequelize.STRING
}, {
underscored: true,
tableName: 'users',
freezeTableName: true,
timestamps: false
});
DiscountCatalog = sequelize.define('discount_catalog', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
underscored: true,
tableName: 'discount_catalog',
freezeTableName: true,
timestamps: false
});
DiscountVendor = sequelize.define('discount_vendor', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
underscored: true,
tableName: 'discount_vendor',
freezeTableName: true,
timestamps: false
});
Discount.hasMany(Catalog, { as: 'Categories', through: DiscountCatalog }/*, { as: 'Categories', through: 'discount_catalog' }*/);
Catalog.hasMany(Discount, { through: DiscountCatalog }/*, { as: 'Discounts', through: 'discount_catalog' }*/);
Discount.hasMany(Vendor, { as: 'Vendors', through: DiscountVendor }/*, { as: 'Categories', through: 'discount_catalog' }*/);
Vendor.hasMany(Discount, { through: DiscountVendor }/*, { as: 'Discounts', through: 'discount_catalog' }*/);
User.hasMany(Discount, { as: 'Users' });
Discount.belongsTo(User);
//sequelize.sync();
// Configure server
app.configure( function() {
//parses request body and populates request.body
app.use( express.bodyParser() );
//checks request.body for HTTP method overrides
app.use( express.methodOverride() );
//perform route lookup based on url and HTTP method
app.use( app.router );
//Show all errors in development
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));
});
//Router
//Get a list of all books
app.get( '/api/v1/discounts', function( request, response ) {
/* Discount.findAll()
.success(function(discounts) {
return response.send({'discounts':discounts});
})
.error(function(err) {
if (err) {
console.log(err);
return response.send('ERROR');
}
});*/
// var date_from = new Date(+request.param('date_from'));
var count = 0;
var discountList = [];
var currentDate = new Date();
Discount.findAll({where: ['date_to >= ?', currentDate]}).success(function(discounts) {
discounts.forEach(function (discount) {
async.series({
categories: function(cb){
discount.getCategories().success(function(categories) {
var categoryList = [];
for (category in categories) {
categoryList.push({'id':categories[category]['id'], 'title':categories[category]['title']});
}
cb(null, categoryList);
}).error(function(err) {
cb(err);
});
},
locations: function(cb){
discount.getVendors().success(function(vendors) {
var vendorList = [];
for (vendor in vendors) {
vendorList.push({'id':vendors[vendor]['id'],'title':vendors[vendor]['title'], 'phone':vendors[vendor]['phone'], 'street':vendors[vendor]['street']});
}
cb(null, vendorList);
}).error(function(err) {
cb(err);
});
},
vendor: function(cb){
discount.getUser().success(function(user) {
cb(null, user['name']);
}).error(function(err) {
cb(err);
});
}
},
function(err, result) {
count++;
if (err) {
console.log("ERROR : "+err);
} else {
result.title = discount.title;
result.date_from = discount.date_from;
result.date_to = discount.date_to;
result.created_at = discount.created_at;
result.percent = discount.percent*100;
discountList.push(result);
}
if(count == discounts.length) { // check if all callbacks have been called
Send(response, discountList);
}
});
});
});
/* sequelize
.query(
'SELECT d.*, c.title as title_catalog, d.title as title_discount, u.name as vendor from discount as d' +
' Left Join catalog as c on d.catalog_id = c.id' +
' Left Join users as u on d.user_id = u.id' +
' Where date_to >= ?', null,
{ raw: true }, [new Date()]
)
.success(function(discounts) {
var list = {};
var discountList = [];
if (discounts.length > 0) {
discounts.forEach(function (discount) {
if (list.hasOwnProperty(discount.title_catalog)) {
list[discount.title_catalog].push({is_active:1, title : discount.title_discount, percent: discount.percent*100, date_from : discount.date_from, date_to : discount.date_to, created_at:discount.created_at, vendor: discount.vendor});
} else {
list[discount.title_catalog] = [];
list[discount.title_catalog].push({is_active:1, title : discount.title_discount, percent: discount.percent*100, date_from : discount.date_from, date_to : discount.date_to, created_at:discount.created_at, vendor: discount.vendor});
}
});
for(var index in list) {
var tmp = {};
tmp[index] = list[index];
discountList.push(tmp);
}
}
return response.send({'discounts':discountList});
})*/
});
function Send(response, discountList) {
return response.send({'discounts':discountList});
}
//Insert a new book
app.post( '/api/v1/discounts', function( request, response ) {
var discount = new DiscountModel({
title: request.body.title,
price: request.body.price
});
console.log(request.body.title);
discount.save( function( err ) {
if( !err ) {
console.log( 'created' );
return response.send( discount );
} else {
console.log( err );
return response.send('ERROR');
}
});
});
//Get a single book by id
app.get( '/api/v1/discounts/:id', function( request, response ) {
return DiscountModel.findById( request.params.id, function( err, discount ) {
if( !err ) {
return response.send( discount );
} else {
console.log( err );
return response.send('ERROR');
}
});
});
//Update a book
app.put( '/api/v1/discounts/:id', function( request, response ) {
return DiscountModel.findById( request.params.id, function( err, discount ) {
discount.title = request.body.title;
discount.price = request.body.price;
return discount.save( function( err ) {
if( !err ) {
console.log( 'discount updated' );
return response.send( discount );
} else {
console.log( err );
return response.send('ERROR');
}
});
});
});
//Delete a book
app.delete( '/api/v1/discounts/:id', function( request, response ) {
DiscountModel.findById( request.params.id, function( err, discount ) {
return discount.remove( function( err ) {
if( !err ) {
console.log( 'Discount removed' );
return response.send( '' );
} else {
console.log( err );
return response.send('ERROR');
}
});
});
});
//Start server
var port = 4711;
app.listen( port, function() {
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});