-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·1286 lines (1181 loc) · 42.1 KB
/
index.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// compatible API routes.
require('dotenv').config();
var express = require('express');
var multer = require('multer');
var ParseServer = require('parse-server').ParseServer;
// var csrf = require('csurf')
var bodyParser = require('body-parser');
var path = require('path');
var Parse = require('parse/node');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var moment = require('moment');
var ejs = require('ejs');
const formatCurrency = require('format-currency');
var configDB = require('./config/database.js');
var redis = require('redis');
var rclient = redis.createClient(process.env.REDIS_PORT,process.env.REDIS_HOST);
r2client = rclient.duplicate();
rclient.subscribe("otobox:notifications",(err,reply)=>{
if(!err){
console.log("parse server connected to redis server!");
}
});
//multer configs
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
var upload = multer({ storage: storage })
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
}
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
//configure parse client
Parse.initialize(process.env.APP_ID);
Parse.serverURL = String(process.env.SERVER_URL)
var app = express();
//creating fromnow function
app.locals.fromNow = function(date){
return moment(date).fromNow();
}
//creating ejs template function to parse string in jsonable object
app.locals.jsonparse = function(data){
return JSON.parse(data);
}
//creating format_currency function
// include the currency code 'USD'
let opts = { format: '%v %c', code: 'RWF' }
app.locals.format_currency = function(money){
return formatCurrency(money, opts);
}
//setting template engine
app.set('view engine','ejs');
app.set('views', path.join(__dirname,'views'));
//Body Parser middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//other middleware
// configuration ======================
mongoose.connect(configDB.url); // connect to our database
require('./config/passport')(passport); // pass passport for configuration
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
// app.use(csrf({ cookie: true }))
// required for passport
app.use(session({ secret: 'heykotlinkotlinhaha' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes =============================
require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
// route middleware to make sure
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/login');
}
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
// Parse Server plays nicely with the rest of your web routes
app.get('/',isLoggedIn, function(req, res) {
res.render('index');
});
app.get('/profile2',function(req,res){
res.render('profile');
});
app.get('/brand',isLoggedIn,function(req,res){
if(req.query.action){
res.render('brand',{query:req.query.action});
}
else{
var Brand = Parse.Object.extend("Brand");
var query = new Parse.Query(Brand);
query.ascending("name");
query.find({
success: function(Brand) {
// The object was retrieved successfully.
// Brand.forEach(function(element) {
// console.log(element.get("url"));
// });
res.render('brand',{Brands: Brand});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
app.post('/brand/add',isLoggedIn,upload.single('brandpic'),(req,res)=>{
var Brand = Parse.Object.extend("Brand");
// Create a new instance of that class.
var mBrand = new Brand();
mBrand.set("name",req.body.brandname);
if(req.file !== undefined){
mBrand.set("url",req.file.path);
}
mBrand.save(null, {
success: function(mBrand) {
// Execute any logic that should take place after the object is saved.
console.info('New object created with objectId: ' + mBrand.id);
},
error: function(mBrand, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error('Failed to create new object, with error code: ' + error.message);
}
});
res.redirect('/brand');
});
//brand remove
app.get('/brand/remove/:id',isLoggedIn,(req,res)=>{
var Brand = Parse.Object.extend("Brand");
// Create a new instance of that class.
var mBrand = new Brand();
mBrand.id = req.params.id;
mBrand.destroy({
success: function(mBrand) {
// The object was deleted from the Parse Cloud.
console.log("Brand object delete it's id is"+mBrand.id);
req.flash('message',req.flash('Successfully deleted'));
},
error: function(mBrand, error) {
// The delete failed.
// error is a Parse.Error with an error code and message.
req.flash('message',req.flash('error occured!'));
}
});
res.redirect('/brand');
});
//brand edit
app.get('/brand/edit/:id',isLoggedIn,(req,res)=>{
if(req.query.action){
var Brand = Parse.Object.extend("Brand");
var query = new Parse.Query(Brand);
query.get(req.params.id, {
success: function(brand) {
// The object was retrieved successfully.
res.render('brand',{query:req.query.action,Brand:brand});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
app.post('/brand/edit/:id',isLoggedIn,upload.single('brandpic'),(req,res)=>{
var Brand = Parse.Object.extend("Brand");
var query = new Parse.Query(Brand);
query.get(req.params.id, {
success: function(brand) {
// The object was retrieved successfully.
// Now let update it
brand.set("name", req.body.brandname);
if(req.file !== undefined){
brand.set("url", req.file.path);
}
brand.save();
res.redirect('/brand');
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
});
app.get('/model',isLoggedIn,function(req,res){
if(req.query.action){
var Brand = Parse.Object.extend("Brand");
var query = new Parse.Query(Brand);
query.find({
success: function(Brand) {
res.render('model',{query:req.query.action,Brands: Brand});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
else{
if(req.query.brand){
//paginations vars
let page = req.query.page ? (parseInt(req.query.page) -1) : 0;
//page must be 0 and greater
page = page < 0 ? 0 : page;
let per_page_display = req.query.plimit ? parseInt(req.query.plimit) : 50;
let total_result;
var Model = Parse.Object.extend("Model");
var Brand = Parse.Object.extend("Brand");
var mBrand = Brand.createWithoutData(req.query.brand);
var query = new Parse.Query(Model);
query.equalTo("parent",mBrand);
//counts result before
query.count().then((count)=>{
total_result = count;
});
query.ascending("name");
query.include("parent");
//limit & skip
let limit = per_page_display > total_result ? 100 : per_page_display;
query.limit(limit);
query.skip(page * per_page_display);
query.find({
success: function(Model) {
res.render('model',{Models: Model,total_result:total_result,page:page+1,plimit:per_page_display});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}else
{
res.render('model',{query: "model"});
}
}
});
app.post('/model/add',isLoggedIn,(req,res)=>{
var Brand = Parse.Object.extend("Brand");
// Create a new instance of that class.
var mBrand = Brand.createWithoutData(req.body.brandid);
console.log(JSON.stringify(mBrand.get("name")))
var Model = Parse.Object.extend("Model");
var mModel = new Model();
mModel.set("name",req.body.modelname);
mModel.set("parent", mBrand);
mModel.save(null, {
success: function(mModel) {
// Execute any logic that should take place after the object is saved.
console.info('New object created with objectId: ' + mModel.id);
},
error: function(mModel, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error('Failed to create new object, with error code: ' + error.message);
}
});
res.redirect('/model');
});
//model remove
app.get('/model/remove/:id',isLoggedIn,(req,res)=>{
var Model = Parse.Object.extend("Model");
// Create a new instance of that class.
var mModel = new Model();
mModel.id = req.params.id;
mModel.destroy({
success: function(mModel) {
// The object was deleted from the Parse Cloud.
console.log("Model object delete it's id is"+mModel.id);
req.flash('message',req.flash('Successfully deleted'));
},
error: function(mModel, error) {
// The delete failed.
// error is a Parse.Error with an error code and message.
req.flash('message',req.flash('error occured!'));
}
});
res.redirect('/model');
});
//model edit
app.get('/model/edit/:id',isLoggedIn,(req,res)=>{
if(req.query.action){
var Brand = Parse.Object.extend("Brand");
var mBrand = new Parse.Query(Brand);
var myBrand = mBrand.find().then((Brand)=> {
return Brand;
});
var Model = Parse.Object.extend("Model");
var query = new Parse.Query(Model);
var myModel = query.get(req.params.id).then((model)=>{
return model;
});
Promise.all([myModel,myBrand]).then(([model,brand])=>{
res.render('model',{query:req.query.action,Model:model,Brands:brand});
});
}
});
app.post('/model/edit/:id',isLoggedIn,(req,res)=>{
var Model = Parse.Object.extend("Model");
var query = new Parse.Query(Model);
query.get(req.params.id, {
success: function(model) {
// The object was retrieved successfully.
var Brand = Parse.Object.extend("Brand");
// Create a new instance of that class.
var mBrand = new Brand();
mBrand.id = req.body.brandid;
model.set("name",req.body.modelname);
model.set("parent", mBrand);
// Now let update it
model.save();
res.redirect('/model');
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
});
//generations
app.get('/generation/:id',isLoggedIn,function(req,res){
if(req.query.action){
var Model = Parse.Object.extend("Model");
var mModel = new Model();
mModel.id = req.params.id;
var Generation = Parse.Object.extend("Generation");
var query = new Parse.Query(Generation);
query.equalTo("model",mModel);
query.descending("released");
query.find({
success: function(Generation) {
res.render('generation',{query:req.query.action,Generations: Generation,Model: mModel});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
else{
var Model = Parse.Object.extend("Model");
var mModel = new Model();
var Generation = Parse.Object.extend("Generation");
mModel.id = req.params.id;
var query = new Parse.Query(Generation);
query.equalTo("model",mModel);
query.descending("released");
query.find({
success: function(Generation) {
res.render('generation',{Generations: Generation, Model: mModel});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
app.post('/generation/add',isLoggedIn,upload.single('generationpic'),(req,res)=>{
var Model = Parse.Object.extend("Model");
var mModel = new Model();
mModel.id = req.body.modelid;
var Generation = Parse.Object.extend("Generation");
var mGeneration = new Generation();
mGeneration.set("name",req.body.generationname);
mGeneration.set("released", req.body.released);
if(req.file !== undefined){
mGeneration.set("url",req.file.path);
}
mGeneration.set("model", mModel);
mGeneration.save(null, {
success: function(mGeneration) {
// Execute any logic that should take place after the object is saved.
console.info('New object created with objectId: ' + mGeneration.id);
},
error: function(mGeneration, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error('Failed to create new object, with error code: ' + error.message);
}
});
res.redirect('/generation/'+req.body.modelid);
});
app.get('/generation/remove/:id',isLoggedIn,(req,res)=>{
var Generation = Parse.Object.extend("Generation");
// Create a new instance of that class.
var mGeneration = new Generation();
mGeneration.id = req.params.id;
mGeneration.destroy({
success: function(mGeneration) {
// The object was deleted from the Parse Cloud.
console.log("Generation object delete it's id is"+mGeneration.id);
req.flash('message',req.flash('Successfully deleted'));
},
error: function(mGeneration, error) {
// The delete failed.
// error is a Parse.Error with an error code and message.
req.flash('message',req.flash('error occured!'));
}
});
res.redirect('back');
});
//generation edit
app.get('/generation/edit/:id',isLoggedIn,(req,res)=>{
if(req.query.action){
var Generation = Parse.Object.extend("Generation");
var mGeneration = new Parse.Query(Generation);
var mCurrentGeneration = mGeneration.get(req.params.id).then((gen)=>{
return gen;
});
return Promise.all([mCurrentGeneration]).then(([current])=>{
// console.log(model);
res.render('generation',{query:req.query.action,Generation:current});
});
}
});
app.post('/generation/edit/:id',isLoggedIn,upload.single('generationpic'),(req,res)=>{
var Generation = Parse.Object.extend("Generation");
var query = new Parse.Query(Generation);
query.get(req.params.id, {
success: function(generation) {
// The object was retrieved successfully.
generation.set("name",req.body.generationname);
generation.set("released", req.body.released);
if (req.file !== undefined) {
generation.set("url",req.file.path);
}
// Now let update it
generation.save();
res.redirect('/generation/'+req.body.modelid);
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
});
//spares
app.get('/spare',isLoggedIn,function(req,res){
if(req.query.action == "edit" || req.query.action == "new"){
var Spare = Parse.Object.extend("Spare");
var mSpare = new Parse.Query(Spare);
var Generation = Parse.Object.extend("Generation");
var mGeneration = new Parse.Query(Generation);
var Model = Parse.Object.extend("Model");
var mModel = new Parse.Query(Model);
var Category = Parse.Object.extend("Category");
var mCategory = new Parse.Query(Category);
var mySpare = mSpare.find().then((Spare)=> {
return Spare;
});
var myModel = mModel.find().then((Model)=>{
return Model;
})
var myGeneration = mGeneration.find().then((Generation)=>{
return Generation;
});
var myCategory = mCategory.find().then((Category)=>{
return Category;
});
return Promise.all([mySpare,myModel,myGeneration,myCategory]).then(([spare,model,generation,category])=>{
// console.log(model);
res.render('spare',{query:req.query.action,Spares: spare,Models:model,Generations:generation,Categories:category});
});
}
else{
//paginations vars
let page = req.query.page ? (parseInt(req.query.page) -1) : 0;
//page must be 0 and greater
page = page < 0 ? 0 : page;
let per_page_display = req.query.plimit ? parseInt(req.query.plimit) : 50;
let total_result;
var Spare = Parse.Object.extend("Spare");
var query = new Parse.Query(Spare);
//counts result before
//we will optimize it later using redis to store counts instead of pulling from db each time query executed!
query.count().then((count)=>{
total_result = count;
});
//limit & skip
let limit = per_page_display > total_result ? 100 : per_page_display;
query.limit(limit);
query.skip(page * per_page_display);
//filter is available
//category
if(req.query.category){
let Category = Parse.Object.extend("Category");
let mCategory = Category.createWithoutData(req.query.category);
query.equalTo("category",mCategory);
}
//generation
if(req.query.generation){
let Generation = Parse.Object.extend("Generation");
let mGeneration = Generation.createWithoutData(req.query.generation);
query.equalTo("generation",mGeneration);
}
query.descending("createdAt");
query.include("generation");
query.include("generation.model");
query.include("category");
query.find({
success: function(Spare) {
res.render('spare',{Spares: Spare,total_result:total_result,page:page+1,plimit:per_page_display});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
//get single spare Details
app.get('/spare/item/:itemId',isLoggedIn,(req,res)=>{
if(req.query.action == "single" && req.params.itemId){
var Spare = Parse.Object.extend("Spare");
var query = new Parse.Query(Spare);
query.descending("createdAt");
query.include("generation");
query.include("generation.model");
query.include("category");
query.get(req.params.itemId,{
success: function(Spare) {
res.render('spare',{query:req.query.action,spare: Spare});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}else
{
res.json({"status":false});
}
});
app.post('/spare/add',isLoggedIn,upload.single('sparepic'),(req,res)=>{
var Spare = Parse.Object.extend("Spare");
var mSpare = new Spare();
var Generation = Parse.Object.extend("Generation");
var mGeneration =Generation.createWithoutData(req.body.generationid);
var Category = Parse.Object.extend("Category");
var mCategory = Category.createWithoutData(req.body.categoryid);
mSpare.set("name",req.body.sparename);
mSpare.set("quality", req.body.sparequality);
mSpare.set("quantity", req.body.sparequantity);
if (req.file !== undefined) {
mSpare.set("url",req.file.path);
}
mSpare.set("price",req.body.spareprice);
mSpare.set("warranty",req.body.sparewarranty);
if(req.body.generationid !== undefined){
mSpare.set("generation", mGeneration);
}else
{
mSpare.set("generation",null);
}
mSpare.set("category",mCategory);
if(req.body.sparedesc){
//nevermind store it as string
//we will parse it at front ends!
mSpare.set("description", req.body.sparedesc);
}
mSpare.save(null, {
success: function(mSpare) {
// Execute any logic that should take place after the object is saved.
console.info('New object created with objectId: ' + mSpare.id);
},
error: function(mSpare, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error('Failed to create new object, with error code: ' + error.message);
}
});
res.redirect('/spare');
});
//spare remove
app.get('/spare/remove/:id',isLoggedIn,(req,res)=>{
var Spare = Parse.Object.extend("Spare");
// Create a new instance of that class.
var mSpare = new Spare();
mSpare.id = req.params.id;
mSpare.destroy({
success: function(mSpare) {
// The object was deleted from the Parse Cloud.
console.log("Spare object delete it's id is"+mSpare.id);
req.flash('message',req.flash('Successfully deleted'));
},
error: function(mSpare, error) {
// The delete failed.
// error is a Parse.Error with an error code and message.
req.flash('message',req.flash('error occured!'));
}
});
res.redirect('/spare');
});
//spare edit
app.get('/spare/edit/:id',isLoggedIn,(req,res)=>{
if(req.query.action){
var Spare = Parse.Object.extend("Spare");
var mSpare = new Parse.Query(Spare);
mSpare.include('generation');
mSpare.include('generation.model');
var mCurrentSpare = mSpare.get(req.params.id).then((spare)=>{
return spare;
});
var Generation = Parse.Object.extend("Generation");
var mGeneration = new Parse.Query(Generation);
var Model = Parse.Object.extend("Model");
var mModel = new Parse.Query(Model);
var Category = Parse.Object.extend("Category");
var mCategory = new Parse.Query(Category);
var mySpare = mSpare.find().then((Spare)=> {
return Spare;
});
var myModel = mModel.find().then((Model)=>{
return Model;
})
var myGeneration = mGeneration.find().then((Generation)=>{
return Generation;
});
var myCategory = mCategory.find().then((Category)=>{
return Category;
});
return Promise.all([mySpare,myModel,myGeneration,myCategory,mCurrentSpare]).then(([spare,model,generation,category,current])=>{
// console.log(model);
res.render('spare',{query:req.query.action,Spares: spare,Models:model,Generations:generation,Categories:category,Spare:current});
});
}
});
app.post('/spare/edit/:id',isLoggedIn,upload.single('sparepic'),(req,res)=>{
var Spare = Parse.Object.extend("Spare");
var query = new Parse.Query(Spare);
query.get(req.params.id, {
success: function(spare) {
// The object was retrieved successfully.
var Generation = Parse.Object.extend("Generation");
var mGeneration = Generation.createWithoutData(req.body.generationid);
var Category = Parse.Object.extend("Category");
var mCategory = Category.createWithoutData(req.body.categoryid);
spare.set("name",req.body.sparename);
spare.set("quality", req.body.sparequality);
spare.set("quantity", req.body.sparequantity);
if(req.file !== undefined){
spare.set("url",req.file.path);
}
spare.set("price",req.body.spareprice);
spare.set("warranty",req.body.sparewarranty);
//check if generation&model is available and set it
if(req.body.generationid !== undefined){
spare.set("generation", mGeneration);
}else
{
spare.set("generation",null);
}
spare.set("category",mCategory);
spare.set("description",req.body.sparedesc);
// Now let update it
spare.save();
res.redirect('/spare');
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
});
//supplier
app.get('/supplier',isLoggedIn,function(req,res){
if(req.query.action){
res.render('supply',{query:req.query.action});
}
else{
var Supplier = Parse.Object.extend("Supplier");
var query = new Parse.Query(Supplier);
query.find({
success: function(Supplier) {
res.render('supply',{Suppliers: Supplier});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
//suppliers add
app.post('/supplier/add',isLoggedIn,(req,res)=>{
var Supplier = Parse.Object.extend("Supplier");
// Create a new instance of that class.
var mSupplier = new Supplier();
mSupplier.set("name",req.body.suppliername);
mSupplier.set("address", req.body.supplieraddress);
mSupplier.set("phone",req.body.supplierphone);
mSupplier.save(null, {
success: function(mSupplier) {
// Execute any logic that should take place after the object is saved.
console.info('New object created with objectId: ' + mSupplier.id);
},
error: function(mSupplier, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error('Failed to create new object, with error code: ' + error.message);
}
});
res.redirect('/supplier');
});
//supplier remove
app.get('/supplier/remove/:id',isLoggedIn,(req,res)=>{
var Supplier = Parse.Object.extend("Supplier");
// Create a new instance of that class.
var mSupplier = Supplier.createWithoutData(req.params.id);
mSupplier.destroy({
success: function(mSupplier) {
// The object was deleted from the Parse Cloud.
console.log("Supplier object delete it's id is"+mSupplier.id);
req.flash('message',req.flash('Successfully deleted'));
},
error: function(mSupplier, error) {
// The delete failed.
// error is a Parse.Error with an error code and message.
req.flash('message',req.flash('error occured!'));
}
});
res.redirect('/supplier');
});
//supplier edit
app.get('/supplier/edit/:id',isLoggedIn,(req,res)=>{
if(req.query.action){
var Supplier = Parse.Object.extend("Supplier");
var query = new Parse.Query(Supplier);
query.get(req.params.id, {
success: function(supplier) {
// The object was retrieved successfully.
res.render('supply',{query:req.query.action,Supplier:supplier});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
app.post('/supplier/edit/:id',isLoggedIn,(req,res)=>{
var Supplier = Parse.Object.extend("Supplier");
var query = new Parse.Query(Supplier);
query.get(req.params.id, {
success: function(supplier) {
// The object was retrieved successfully.
// Now let update it
supplier.set("name", req.body.suppliername);
supplier.set("address", req.body.supplieraddress);
supplier.set("phone", req.body.supplierphone);
supplier.save();
res.redirect('/supplier');
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
});
//add contacts routes
//get contacts
app.get('/contacts',isLoggedIn,function(req,res){
if(req.query.action){
res.render('contacts',{query:req.query.action});
}
else{
var Contact = Parse.Object.extend("Contact");
var query = new Parse.Query(Contact);
query.find({
success: function(Contact) {
res.render('contacts',{Contacts: Contact});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
//suppliers add
app.post('/contacts/add',isLoggedIn,(req,res)=>{
var Contact = Parse.Object.extend("Contact");
// Create a new instance of that class.
var mContact = new Contact();
mContact.set("website",req.body.contactwebsite);
mContact.set("email", req.body.contactemail);
mContact.set("phone", req.body.contactphone);
mContact.set("lat",req.body.contactlatitude);
mContact.set("long",req.body.contactlongitude);
mContact.save(null, {
success: function(mContact) {
// Execute any logic that should take place after the object is saved.
console.info('New object created with objectId: ' + mContact.id);
},
error: function(mContact, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error('Failed to create new object, with error code: ' + error.message);
}
});
res.redirect('/contacts');
});
//supplier remove
app.get('/contacts/remove/:id',isLoggedIn,(req,res)=>{
var Contact = Parse.Object.extend("Contact");
// Create a new instance of that class.
var mContact = Contact.createWithoutData(req.params.id);
mContact.destroy({
success: function(mContact) {
// The object was deleted from the Parse Cloud.
console.log("Contact object delete it's id is"+mContact.id);
req.flash('message',req.flash('Successfully deleted'));
},
error: function(mContact, error) {
// The delete failed.
// error is a Parse.Error with an error code and message.
req.flash('message',req.flash('error occured!'));
}
});
res.redirect('/contacts');
});
//supplier edit
app.get('/contacts/edit/:id',isLoggedIn,(req,res)=>{
if(req.query.action){
var Contact = Parse.Object.extend("Contact");
var query = new Parse.Query(Contact);
query.get(req.params.id, {
success: function(contact) {
// The object was retrieved successfully.
res.render('contacts',{query:req.query.action,Contact:contact});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
app.post('/contacts/edit/:id',isLoggedIn,(req,res)=>{
var Contact = Parse.Object.extend("Contact");
var query = new Parse.Query(Contact);
query.get(req.params.id, {
success: function(contact) {
// The object was retrieved successfully.
// Now let update it
contact.set("website",req.body.contactwebsite);
contact.set("email", req.body.contactemail);
contact.set("phone", req.body.contactphone);
contact.set("long",req.body.contactlongitude);
contact.set("lat",req.body.contactlatitude);
contact.save();
res.redirect('/contacts');
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
});
//ends of contacts routes
//category
app.get('/category',isLoggedIn,function(req,res){
if(req.query.action){
res.render('category',{query:req.query.action});
}
else{
var Category = Parse.Object.extend("Category");
var query = new Parse.Query(Category);
query.find({
success: function(Category) {
res.render('category',{Categories: Category});
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
}
});
//suppliers add
app.post('/category/add',isLoggedIn,(req,res)=>{
var Category = Parse.Object.extend("Category");
// Create a new instance of that class.
var mCategory = new Category();
mCategory.set("name",req.body.categoryname);