-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
956 lines (806 loc) · 22.6 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const validator = require("validator");
const shortid = require('shortid');
const moment = require('moment');
const path = require('path');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//mongoose conn
mongoose.connect('mongodb+srv://Hotel:Hotel@cluster0.qqo0way.mongodb.net/Retvens', { useNewUrlParser: true });
//hotel owner's properties schema
const PropertySchema = new mongoose.Schema({
owner_id: {
type: String,
required: false
},
trip_advisor_review: {
type: Number,
required: false
},
Google_review: {
type: Number,
required: false
},
hotel_id: {
type: String,
default: shortid.generate
},
profileImage: {
type: String,
},
backgroundImage: {
type: String,
},
hotel_name: {
type: String,
required: false
},
hotel_location: [{
Latitude: String,
Longitude: String
}],
hotel_stars: {
type: Number,
required: false
},
About: {
type: String,
required: false
},
Address: {
type: String,
required: false
},
Country: {
type: String,
required: false
}
});
//function to validate email
var validateEmail = function (email) {
var re = /^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/;
return re.test(email)
};
//hotel owner's schema
const HotelSchema = new mongoose.Schema({
Name: {
type: String,
required: false
},
Email: {
type: String, required: false, validate: [validateEmail, 'Pls Enter valid email address'],
match: [/^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/, 'Please fill a valid email address']
},
Password: {
type: String,
required: false
},
Phone: {
type: Number,
required: false
},
owner_id: {
type: String,
default: shortid.generate,
required: false
},
token: {
type: String,
},
Country: {
type: String,
required: false
},
profileImage: { type: String, required: false },
backgroundImage: { type: String, required: false },
Service_type: { type: String, required: false }
});
const Hotel_owner = mongoose.model('Hotel_owner', HotelSchema);
const Property = mongoose.model('Property', PropertySchema);
// get hotel owner's data
app.get('/hotelowner', (req, res) => {
Hotel_owner.find({}, (error, blogposts) => {
if (error) {
res.status(500).send(error);
} else {
res.send(blogposts);
}
});
});
//get hotel owner's properties or hotels/restuarants
app.get('/property', (req, res) => {
Property.find({}, (error, blogposts) => {
if (error) {
res.status(500).send(error);
} else {
res.send(blogposts);
}
});
});
//post hotel owner data
// app.post('/hotelowner', (req, res) => {
// const blogpost = new Hotel_owner(req.body);
// if (req.body.token == "abc123") {
// blogpost.save()
// res.send({ message: "Owner added succesfully" })
// } else {
// res.send({ message: "pls enter correct token" })
// }
// });.
// app.post('/property', (req, res) => {
// const blogpost = new Property(req.body);
// blogpost.save((error) => {
// if (error) {
// res.status(500).send({ message: error.message });
// } else {
// res.send({ message: "Property added successfully" });
// }
// });
// });
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname)
}
});
const upload = multer({ storage: storage });
app.post('/property', upload.fields([{ name: 'profileImage', maxCount: 1 }, { name: 'backgroundImage', maxCount: 1 }]), async (req, res) => {
const { owner_id, trip_advisor_review, Google_review, hotel_name, hotel_stars, About, Address, Country } = req.body;
const { profileImage, backgroundImage } = req.files;
const user = new Property({
owner_id,
trip_advisor_review,
Google_review,
hotel_name,
hotel_stars,
About,
Address,
Country,
profileImage: profileImage[0].filename,
backgroundImage: backgroundImage[0].filename
});
try {
const savedUser = await user.save();
res.status(201).json({message: "property added successfully"});
} catch (err) {
res.status(400).json({ message: err.message });
}
});
app.post('/hotelowner', upload.fields([{ name: 'profileImage', maxCount: 1 }, { name: 'backgroundImage', maxCount: 1 }]), async (req, res) => {
const { Name, Email, Password, Phone, Country, Service_type } = req.body;
const { profileImage, backgroundImage } = req.files;
const user = new Hotel_owner({
Name,
Email,
Password,
Phone,
Country,
Service_type,
profileImage: profileImage[0].filename,
backgroundImage: backgroundImage[0].filename
});
try {
const savedUser = await user.save();
res.status(201).json({message: "Owner added successfully"});
} catch (err) {
res.status(400).json({ message: err.message });
}
});
const Social_media = new mongoose.Schema({
owner_id: {
type: String,
required: true
},
hotel_id: {
type: String,
required: true
},
social_media: {
facebook: String,
Twitter: String,
Instagram: String,
Linkedin: String,
Pinterest: String,
Whatsapp: String,
Youtube: String,
GMB: String
}
});
const Social = mongoose.model('Social', Social_media)
module.exports = Social;
// social media api
app.post('/social', (req, res) => {
const sm = new Social(req.body);
sm.save((error) => {
if (error) {
res.status(500).send({ message: error.message });
} else {
res.send({ message: "social media added successfully" });
}
});
});
app.get('/social', (req, res) => {
Social.find({}, (error, sm) => {
if (error) {
res.status(500).send(error);
} else {
res.send(sm);
}
});
});
const adminSchema = new mongoose.Schema({
Name: {
type: String,
required: true
},
Email: {
type: String, required: true, validate: [validateEmail, 'Pls Enter valid email address'],
match: [/^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/, 'Please fill a valid email address']
},
Password: {
type: String,
required: true
},
Phone_no: {
type: Number,
required: true
},
Profile_photo: {
type: String,
required: true
},
User_id: {
type: String, required: true,
},
});
//admin model
const Admin = mongoose.model('Admin', adminSchema);
//admin data apis
app.post('/adminsignup', (req, res) => {
const al = new Admin(req.body);
al.save((error) => {
if (error) {
res.status(500).send({ message: error.message })
}
else {
res.send({ message: "Registration Successful" });
}
})
});
app.get('/adminsignup', (req, res) => {
Admin.find({}, (error, al) => {
if (error) {
res.status(500).send(error)
}
else {
res.send(al);
}
})
});
const Restroschema = new mongoose.Schema({
owner_id: {
type: Number
},
restro_id: {
type: Number,
required: true
},
restraurant_profile_photo: {
type: String,
required: true
},
restaurant_name: {
type: String,
required: true
},
restaurant_location: {
type: String,
required: true
},
restaurant_type: {
type: String,
required: true
},
no_of_pax: {
type: Number,
required: true
},
})
const restro_property = mongoose.model('restro_property', Restroschema);
module.exports = restro_property;
//Restaurant Post
app.post('/restro_property', (req, res) => {
const blogpost = new restro_property(req.body);
blogpost.save((error) => {
if (error) {
res.status(500).send({ message: error.message });
} else {
res.send({ message: "Restaurant added successfully" });
}
});
});
//Restaurant Get
app.get('/restro_property', (req, res) => {
restro_property.find({}, (error, blogposts) => {
if (error) {
res.status(500).send(error);
} else {
res.send(blogposts);
}
});
});
const Social_Media = new mongoose.Schema({
owner_id: {
type: Number,
required: true
},
restro_id: {
type: Number,
required: true
},
social_media: [{
facebook: String,
Twitter: String,
Instagram: String,
Linkedin: String,
Pinterest: String,
Youtube: String,
GMB: String
}],
});
const Social_restaurant = mongoose.model('Social_restaurant', Social_Media)
module.exports = Social_restaurant;
const companySchema = new mongoose.Schema({
token: {
type: Number,
required: true
},
Name: {
type: String,
required: true
},
Email: {
type: String, required: true, validate: [validateEmail, 'Pls Enter valid email address'],
match: [/^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/, 'Please fill a valid email address']
},
Password: {
type: String,
required: true
},
Phone_no: {
type: Number,
required: true
},
Profile_photo: {
type: String,
required: true
},
User_id: {
type: String, required: true,
},
});
//company model
const Company = mongoose.model('Company', companySchema);
app.post("/company", (req, res) => {
const comp = new Company(req.body);
comp.save((error) => {
if (error) {
res.status(500).send({ message: error.message })
}
else {
res.send({ message: "Company added successfully" })
}
})
})
//company data
app.get('/company', (req, res) => {
Company.find({}, (error, comp) => {
if (error) {
res.status(500).send(error);
} else {
res.send(comp);
}
});
});
app.post('/company', async (req, res) => {
try {
const prevDoc = await Company.findOne({}).sort({ _id: -1 });
if (prevDoc.token !== req.body.token) {
return res.status(400).json({ message: 'invalid data' });
}
const myDoc = new Company({
token: req.body.token + 1,
Name: req.body.Name,
Email: req.body.Email,
Password: req.body.Password,
Phone_no: req.body.Phone_no,
Profile_photo: req.body.Profile_photo,
User_id: req.body.User_id
});
const newDoc = await myDoc.save();
res.status(201).json({ message: "Company added successfully" });
} catch (error) {
res.status(400).json({ message: error.message });
}
});
const Hotel_work = new mongoose.Schema({
owner_id: {
type: Number,
required: true
},
hotel_id: {
type: Number,
required: true
},
created_at: {
type: Date,
required: true,
default: Date.now
},
post_upload: {
type: String,
required: true
},
total_number_of_post: {
type: Number,
required: true
},
total_number_of_reels: {
type: Number,
required: true
},
total_number_of_stories: {
type: Number,
required: true
},
Google_reviews: {
type: String,
required: true
},
Tripadvisor_reviews: {
type: String,
required: true
},
Month_calender: {
type: String,
required: true
}
});
const Hotel_Work = mongoose.model('Hotel_Work', Hotel_work);
app.get("/hotelwork", (req, res) => {
Hotel_Work.find({}, (error, hm) => {
if (error) {
res.status(500).send(error)
}
else {
res.send(hm)
}
})
})
app.post("/hotelwork", (req, res) => {
const hm = new Hotel_Work(req.body);
hm.save((error) => {
if (error) {
res.status(500).send({ message: error })
}
else {
res.send({ message: "Hotel works added" })
}
})
})
app.get('/hotelwork/:hotel_id', (req, res) => {
Hotel_Work.findOne({ hotel_id: req.params.hotel_id })
.then(document => res.json(document))
.catch(err => res.status(404).json({ success: false }));
});
const Restro_work = new mongoose.Schema({
restro_id: {
type: Number,
required: true
},
created_at: {
type: Date,
required: true,
default: Date.now
},
post_upload: {
type: String,
required: true
},
total_number_of_post: {
type: Number,
required: true
},
total_number_of_reels: {
type: Number,
required: true
},
total_number_of_stories: {
type: Number,
required: true
},
Google_reviews: {
type: String,
required: true
},
Tripadvisor_reviews: {
type: String,
required: true
}
});
const Restroant_Work = mongoose.model('Restroant_Work', Restro_work);
app.get("/restrowork", (req, res) => {
Restroant_Work.find({}, (error, rw) => {
if (error) {
res.status(500).send(error)
}
else {
res.send(rw)
}
})
})
app.post("/restrowork", (req, res) => {
const rw = new Restroant_Work(req.body);
rw.save((error) => {
if (error) {
res.status(500).send({ message: error })
}
else {
res.send({ message: "restaurant works added successfully" })
}
})
});
//apis for updating
app.patch("/:resource/:id", async (req, res) => {
if (req.params.resource === 'tasks') {
const id = req.params.id;
const updatetask = await All_task.findByIdAndUpdate({ _id: id }, req.body, { new: true });
if (updatetask) {
res.send({ message: "successfully updated" });
}
else {
res.send({ message: "something wrong" });
}
} else if (req.params.resource === 'property') {
const id = req.params.id;
const updateproperty = await Property.findByIdAndUpdate({ _id: id }, req.body, { new: true });
if (updateproperty) {
res.send({ message: "successfully updated" });
} else {
res.send({ message: "something wrong" });
}
} else if (req.params.resource === 'hotelowner') {
const id = req.params.id;
const updatehotel = await Hotel_owner.findByIdAndUpdate({ _id: id }, req.body, { new: true });
if (updatehotel) {
res.send({ message: "successfully updated" });
} else {
res.send({ message: "something wrong" });
}
} else if (req.params.resource === 'social') {
const id = req.params.id;
const updatesocial = await Social.findByIdAndUpdate({ _id: id }, req.body, { new: true });
if (updatesocial) {
res.send({ message: "successfully updated" });
} else {
res.send({ message: "something wrong" });
}
} else {
res.send({ message: "pls enter data for update" });
}
})
const Task = new mongoose.Schema({
hotel_name: {
type: String,
required: true
},
hotel_id: {
type: String,
required: true
},
owner_id: {
type: String,
required: true
},
favourite: {
type: Boolean,
required: true
},
Date: {
type: String,
default: moment().format('DD-MM-YY')
},
facebook: {
type: String,
required: true
},
Linkedin: {
type: String,
required: true,
},
instagram: {
type: String,
required: true
},
twitter: {
type: String,
required: true
},
Pinterest: {
type: String,
required: true
},
GMB: {
type: String,
required: true
},
Google_reviews: {
type: String,
required: true
},
owner_pic: {
type: String,
required: true
},
Status: {
type: String,
required: true
}
})
const All_task = mongoose.model('All_task', Task)
module.exports = All_task;
//Post All_task
app.post('/tasks', (req, res) => {
const comp = new All_task(req.body);
comp.save((error) => {
if (error) {
res.status(500).send({ message: error });
} else {
res.send({ message: "task added successfully" });
}
});
});
//Get All_task
app.get('/tasks', (req, res) => {
All_task.find({}, (error, comp) => {
if (error) {
res.status(500).send(error);
} else {
res.send(comp);
}
});
});
//get tasks using date or hotelid or status
app.get('/tasks/:key', async (req, res) => {
let data = await All_task.find({
"$or": [
{ "Date": { $eq: req.params.key } },
{ "hotel_id": { $eq: req.params.key } },
{ "Status": { $eq: req.params.key } },
{ "owner_id": { $eq: req.params.key } }]
})
res.send(data)
});
const countries = new mongoose.Schema({
Name: {
type: String,
required: true
},
Country_photo: {
type: String,
required: true
}
});
const Country = mongoose.model('Country', countries)
module.exports = Country;
app.get('/Country', (req, res) => {
Country.find({}, (error, blogposts) => {
if (error) {
res.status(500).send(error);
} else {
res.send(blogposts);
}
});
});
//Post country
app.post("/Country", (req, res) => {
const hm = new Country(req.body);
hm.save((error) => {
if (error) {
res.send(500).send(error)
}
else {
res.send({ message: "Country added successfully" })
}
})
});
app.get('/restrowork/:restro_id', (req, res) => {
Restroant_Work.findOne({ restro_id: req.params.restro_id })
.then(document => res.json(document))
.catch(err => res.status(404).json({ success: false }));
});
//get specific restarant property
app.get('/restro_property/:owner_id', (req, res) => {
restro_property.findOne({ owner_id: req.params.owner_id })
.then(document => res.json(document))
.catch(err => res.status(404).json({ success: false }));
});
//get specific data of admin from userid
app.get('/adminsignup/:User_id', (req, res) => {
Admin.findOne({ User_id: req.params.User_id })
.then(document => res.json(document))
.catch(err => res.status(404).json({ success: false }));
});
app.get('/hotelowner/:key', async (req, res) => {
let data = await Hotel_owner.find({
"$or": [
{ "Country": { $eq: req.params.key } },
{ "owner_id": { $eq: req.params.key } }]
})
res.send(data)
});
//api for login
app.post("/login", (req, res) => {
const { Email, Password } = req.body
Hotel_owner.findOne({ Email: Email }, (error, user) => {
if (user) {
if (Password === user.Password) {
var owner_id = user.owner_id;
res.send({ message: "Owner login successful", owner_id })
} else {
res.send({ message: "pls check your email and password" });
}
} else if (!user) {
const { Email, Password } = req.body
Admin.findOne({ Email: Email }, (error, data) => {
if (data) {
if (Password === data.Password) {
res.send({ message: "Admin login successful" })
} else {
res.send({ message: "pls check your email and password" })
}
} else if (!data) {
const { Email, Password } = req.body
Company.findOne({ Email: Email }, (error, com) => {
if (com) {
if (Password === com.Password) {
res.send({ message: "Company login successful" })
} else {
res.send({ message: "pls check your email and password" })
}
} else {
res.send({ message: "pls check your email and password" })
}
})
}
})
} else {
res.send({ message: "incorrect data" })
}
})
})
//get property using hotelif and oener id
app.get('/property/:key', async (req, res) => {
let data = await Property.find({
"$or": [
{ "hotel_id": { $eq: req.params.key } },
{ "owner_id": { $eq: req.params.key } }]
})
res.send(data)
});
//get specific data from social media using owner_id of hotel/restuarant owner's hotel
app.get('/social/:hotel_id', (req, res) => {
Social.findOne({ hotel_id: req.params.hotel_id })
.then(document => res.json(document))
.catch(err => res.status(404).json({ success: false }));
});
const PORT = 3000;
app.listen(process.env.PORT || 3000, () => {
console.log('Listening on port 3000');
});