-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
168 lines (168 loc) · 6.61 KB
/
app.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
const { static } = require('express');
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
let port = process.env.PORT || 1234;
const Car = require('./models/car');
const User = require('./models/user');
const app = express();
app.set('view engine', 'ejs');
// connect to mongoose db connection then listen
mongoose.connect('mongodb+srv://ahmed:[email protected]/cars?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false }).then((result) => app.listen(port));
// middleware
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
app.get('/', (req, res) => {
res.redirect('/home');
});
app.get('/home', (req, res) => {
global.message = 0;
res.render('index', { title: 'Home' });
});
/*app.get('/car/create', (req, res) => {
const car = new Car({
name: 'Renault Symbol',
cost: 102,
cartype: 'الاقتصادية',
fuletype: 'بنزين',
gertype: 'عادي',
capacity: 5,
from: new Date(2020, 0, 1),
to: new Date(2020, 0, 2),
available: true
});
car.save().then((result) => {
res.redirect('all_cars');
}).catch((err) => {
console.log(err);
});
});*/
app.get('/rent_info', (req, res) => {
global.message = 0;
res.render('rent_info', { title: 'Information', message: global.message });
});
app.get('/all_cars', (req, res) => {
global.dates = { from: '', to: '' };
global.message = 0;
Car.find().then((result) => {
res.render('all_cars', { title: 'All Cars', cars: result });
}).catch((err) => {
res.status(404).render('404', { title: 'Error' });
});
});
app.post('/rent_info_details', (req, res) => {
global.message = 0;
let em = req.body.email;
let idn = req.body.idnumber;
if (em.length >= 12 && idn.length >= 9) {
User.find({ email: req.body.email, idnumber: req.body.idnumber }).then((result) => {
res.render('rent_info_details', { title: 'Information Details', user: result[0] });
}).catch((err) => {
res.status(404).render('404', { title: 'Error' });
});
} else {
global.message = 1;
res.render('rent_info', { title: 'Information', message: global.message });
}
});
app.get('/user_info_page/:id', (req, res) => {
Car.findById(req.params.id).then(result => {
res.render('user_info_page', { title: 'User Information', car: result, globa: global.dates, message: global.message });
}).catch(err => {
res.status(404).render('404', { title: 'Error' });
});
});
app.post('/search_result', (req, res) => {
global.dates = { from: req.body.receive_date, to: req.body.arrive_date };
if ((Date.parse(req.body.receive_date) >= Date.parse(new Date())) && (Date.parse(req.body.arrive_date) > Date.parse(new Date())) && (Date.parse(req.body.arrive_date) > Date.parse(req.body.receive_date))){
global.message = 0;
Car.find({ available: true }).then((result) => {
res.render('search_result', { title: 'Search Result', cars: result });
}).catch((err) => {
res.status(404).render('404', { title: 'Error' });
});
} else {
global.message = 1;
res.render('index', { title: 'Home', message: global.message });
}
});
app.post('/search_result/filter', (req, res) => {
Car.find({ available: true, cartype: req.body.cartype, fuletype: req.body.fuletype, gertype: req.body.gertype }).then((result) => {
res.render('search_result', { title: 'Search Result', cars: result });
}).catch((err) => {
res.status(404).render('404', { title: 'Error' });
});
});
// add new user and car to mongodb
app.post('/user/new', (req, res) => {
global.message = 0;
if ((Date.parse(req.body.from) >= Date.parse(new Date())) && (Date.parse(req.body.to) > Date.parse(new Date())) && (Date.parse(req.body.to) > Date.parse(req.body.from))){
Car.findByIdAndUpdate(req.body.carid, { available: false, from: req.body.from, to: req.body.to }).then((result1) => {
const underdata = { id: req.body.carid, name: result1.name, cost: result1.cost, from: new Date(req.body.from), to: new Date(req.body.to) };
User.findOneAndUpdate({ email: req.body.email }, { $addToSet: { underrents: underdata } }).then(result2 => {
if (result2 == null) {
// add new user
const user = new User({
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.email,
phone: req.body.phone,
birthdate: req.body.birthdate,
idnumber: req.body.idnumber,
city: req.body.city,
underrents: [underdata],
lastrents: []
});
user.save().then((result) => {
res.render('rent_info_details', { title: 'Information Details', carid: req.body.carid, user: user });
}).catch((err) => {
res.status(404).render('404', { title: 'Error' });
});
} else {
User.findById(result2.id).then(result => {
res.render('rent_info_details', { title: 'Information Details', carid: req.body.carid, user: result });
});
}
}).catch(err => {
res.status(404).render('404', { title: 'Error' });
});
}).catch(err => {
res.status(404).render('404', { title: 'Error' });
});
} else {
global.message = 1;
res.redirect(`/user_info_page/${req.body.carid}`);
}
});
// move user underrent car to lastrent then delete it
app.post('/edit_car', (req, res) => {
User.findByIdAndUpdate(req.body.userid, {
$push: { lastrents: { id: req.body.carid, name: req.body.name, cost: req.body.cost, from: new Date(req.body.from), to: new Date(req.body.to) } },
$pull: { underrents: { id: req.body.carid } }
}).then(result => {
Car.findByIdAndUpdate(req.body.carid, { available: true }).then(result => {
User.findById(req.body.userid).then(result => {
res.render('rent_info_details', { title: 'Information Details', carid: req.body.carid, user: result });
});
}).catch(err => {
res.status(404).render('404', { title: 'Error' });
});
}).catch(err => {
res.status(404).render('404', { title: 'Error' });
});
});
// delete a car from lastrent
app.post('/delete_car', (req, res) => {
User.findByIdAndUpdate(req.body.userid, { $pull: { lastrents: { id: req.body.carid } } }).then(result1 => {
User.findById(req.body.userid).then(result => {
res.render('rent_info_details', { title: 'Information Details', carid: req.body.carid, user: result });
});
}).catch(err => {
res.status(404).render('404', { title: 'Error' });
});
});
// 404 page
app.use((req, res) => {
res.status(404).render('404', { title: 'Error' });
});