-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
183 lines (174 loc) · 5.99 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const session = require('express-session');
const config = require('config');
const connectDB = require('./config/db');
const bodyParser = require("body-parser");
const fetch=require("node-fetch");
const fs=require('fs');
// Connect to database
connectDB();
const app = express();
// Set view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static("public"));
app.use("/uploads", express.static("uploads"));
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(session({
secret: config.get('sessionSecret'),
resave: false,
saveUninitialized: true
}));
app.use(express.static(path.join(__dirname, 'public')));
// Routes
const authRoutes = require('./routes/authRoutes');
const bookingRoutes = require('./routes/bookingRoutes');
const flightRoutes = require('./routes/flightRoutes');
const hotelRoutes = require('./routes/hotelRoutes');
const rentalCarRoutes = require('./routes/rentalCarRoutes');
const reviewRoutes = require('./routes/reviewRoutes');
const trainRoutes=require('./routes/trainRoutes');
const busRoutes=require('./routes/busRoutes');
const rapidoRoutes=require('./routes/rapidoRoutes');
const searchRoutes=require('./routes/searchRoutes');
const locations=require('./utils/locations');
const stations=require('./utils/stations');
const busstations=require('./utils/busstations');
const hotellocations=require('./utils/hotelloactions');
const airports=require('./utils/airports');
app.use('/auth', authRoutes);
app.use('/bookings', bookingRoutes);
app.use('/flights', flightRoutes);
app.use('/hotels', hotelRoutes);
app.use('/rental-cars', rentalCarRoutes);
app.use('/reviews', reviewRoutes);
app.use('/trains',trainRoutes);
app.use('/buses',busRoutes);
app.use('/rapidos',rapidoRoutes);
app.use('/allsearch',searchRoutes);
// Home route
app.get('/home', (req, res) => {
res.render('home', { title: 'Home' });
});
app.get('/',async (req,res)=>{
try {
res.render('intro',{stations:stations,airports:airports,busstations:busstations,locations:locations,hotellocations:hotellocations});
} catch (err) {
console.error('Error parsing JSON data:', err);
}
})
// Profile route (protected)
app.get('/profile', (req, res) => {
const user = req.session.user;
if (user) {
res.render('profile', { title: 'Profile', user });
} else {
res.redirect('/auth/login');
}
});
app.get('/flight',(req,res)=>{
res.render('flights/flightDetails',{flight});
})
app.get('/createFlight',(req,res)=>{
res.render('admin/CreateFlight',{message:'',airports:airports});
})
app.get('/createHotel',(req,res)=>{
res.render('admin/CreateHotel',{message:''});
})
app.get('/createCar',(req,res)=>{
res.render('admin/CreateRentalCar',{message:''});
})
app.get('/createTrain',(req,res)=>{
res.render('admin/CreateTrain',{message:''});
})
app.get('/createBus',(req,res)=>{
res.render('admin/CreateBus',{message:''});
})
app.get('/createRapido',(req,res)=>{
res.render('admin/CreateRapido',{message:''});
})
app.get('/bookingForm',(req,res)=>{
const flight=req.query;
console.log(flight);
res.render('booking/bookingForm',{flight:flight});
})
const FlightBooking=require('./models/FlightBooking');
app.post('/api/book-flight',async(req,res)=>{
try {
const bookingDetails=req.body;
const newBooking = new FlightBooking(bookingDetails);
await newBooking.save();
console.log('Booking successfully stored in MongoDB');
res.json({ success: true, message: 'Booking successful!' });
}catch (error) {
console.error('Error storing booking in MongoDB:', error);
res.status(500).json({ success: false, message: 'Booking failed. Please try again later.' });
}
})
app.post('/flightbookingpage',async(req,res)=>{
try{
const flight=req.body.flight;
console.log(flight);
res.status(200).json({success:'success'});
}catch(error){
console.log(error);
}
})
app.get('/trainbookingForm',(req,res)=>{
res.render('booking/trainbookingForm');
})
app.post('/trainbookingpage',async(req,res)=>{
try{
const train=req.body.train;
console.log(train);
res.status(200).json({success:'success'});
}catch(error){
console.log(error);
}
})
app.post('/bookTickets', (req, res) => {
const bookingPayload = req.body;
console.log("Received booking payload:", bookingPayload);
// Mock processing of booking
const isSuccess = processBooking(bookingPayload);
if (isSuccess) {
res.json({ success: true, message: "Tickets booked successfully!" });
} else {
res.json({ success: false, message: "Failed to book tickets. Please try again." });
}
});
// Mock function to process booking
function processBooking(payload) {
// This is where you would normally handle the booking logic, such as saving to a database
// For this example, we'll just log the payload and return success
console.log("Processing booking for:", payload);
// Simulating booking logic
const allSeatsAvailable = checkSeatAvailability(payload.seats);
if (allSeatsAvailable) {
// Logic to save booking to the database would go here
return true; // Simulate a successful booking
} else {
return false; // Simulate a failed booking due to seat unavailability
}
}
// Mock function to check seat availability
function checkSeatAvailability(seats) {
// Normally, this would query a database to check if the seats are available
// For this example, we'll assume all requested seats are available
return true;
}
app.post('/auth/logout',(req,res)=>{
const user=req.session.user;
if(user){
res.redirect('/');
req.session.destroy();
}
})
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));