-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
216 lines (188 loc) · 6.4 KB
/
server.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
// Import required modules
const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require("bcryptjs");
// Import configuration files
const serverConfig = require('./configs/server.config');
const dbConfig = require('./configs/db.config');
// Import models
const Movie = require('./models/movie.models');
const Theatre = require('./models/theater.models');
const User = require('./models/user.models');
const Booking = require('./models/booking.models')
// Create an instance of the express application
const app = express();
// Middleware to parse JSON bodies
app.use(express.json()); // Used to parse JSON bodies
app.use(express.urlencoded({ extended: true })); // Used to parse URL-encoded bodies
/**
* DB Connection initialization
*/
// if(process.env.NODE_ENV !== 'production'){
// try{
// mongoose.connect(dbConfig.DB_URL,
// {useNewUrlParser : true, useUnifiedToplogy: true}
// ()=>{
// console.log("Connected to Mongo DB");
// init();
// }
// }catch(err){
// console.log("Couldnt connect with DB", err.message)
// }
// }
if (process.env.NODE_ENV !== 'production') {
const db = mongoose.connection;
db.on('error', (err) => {
console.error('Connection error:', err.message);
});
db.once('open', () => {
console.log('Connected to Mongo DB');
init();
});
mongoose.connect(dbConfig.DB_URL, { useNewUrlParser: true, useUnifiedTopology: true });
}
/**
* This function will initialize the initial state of the movie booking application
*/
async function init() {
await Movie.collection.drop();
try {
movie1 = await Movie.create({
name: "Bachhan Pandey",
description: "Comedy Masala Movie",
casts: ["Akshay Kumar", "Jacqueline Fernandiz"],
director: "Farhad Samji",
trailerUrl: "http://bacchanpandey/trailers/1",
posterUrl: "http://bacchanpandey/posters/1",
language: "Hindi",
releaseDate: "18-03-2022",
releaseStatus: "RELEASED"
});
movie2 = await Movie.create({
name: "Jalsa",
description: "Intense Drama Movie",
casts: ["Vidya Balan", "Shefali Shah"],
director: "Suresh Triveni",
trailerUrl: "http://jalsa/trailers/1",
posterUrl: "http://jalsa/posters/1",
language: "Hindi",
releaseDate: "18-03-2022",
releaseStatus: "RELEASED"
});
movie3 = await Movie.create({
name: "Jhund",
description: "Comedy Drama Movie",
casts: ["Amitabh Bachchan", "Abhinay Raj"],
director: "Nagraj Manjule",
trailerUrl: "http://jhund/trailers/1",
posterUrl: "http://jhund/posters/1",
language: "Hindi",
releaseDate: "04-03-2022",
releaseStatus: "RELEASED"
});
movie4 = await Movie.create({
name: "Radhe Shyam",
description: "Comedy Drama Movie",
casts: ["Prabhas", "Pooja Hegde"],
director: "Radha Krishna Kumar",
trailerUrl: "http://RadheShyam/trailers/1",
posterUrl: "http://RadheShyam/posters/1",
language: "Hindi",
releaseDate: "11-03-2022",
releaseStatus: "RELEASED"
});
movie5 = await Movie.create({
name: "The Kashmir Files",
description: "Intense Movie",
casts: ["Mithun Chakraborty", "Anupam Kher"],
director: "Vivek Agnihotri",
trailerUrl: "http://TheKashmirFiles/trailers/1",
posterUrl: "http://TheKashmirFiles/posters/1",
language: "Hindi",
releaseDate: "11-03-2022",
releaseStatus: "RELEASED"
});
console.log("Movies inserted in the db");
} catch (err) {
console.error(err.message);
}
await Theatre.collection.drop();
try{
await Theatre.create({
name: "FunCinemas",
city: "Bangalore",
description: "Top class theatre",
pinCode: 560052,
movies : [movie1._id, movie2._id, movie3._id]
});
await Theatre.create({
name: "PVR Cinemas - Kormangala",
city: "Bangalore",
description: "PVR franchise theatre",
pinCode: 560095,
movies : [movie1._id, movie2._id, movie4._id]
});
await Theatre.create({
name: "IMax",
city: "Bangalore",
description: "IMax franchise theatre",
pinCode: 560095,
movies : [movie1._id, movie4._id]
});
await Theatre.create({
name: "Vaibhav Theatre",
city: "Bangalore",
description: "Economical theatre",
pinCode: 560094,
movies : [movie5._id, movie4._id]
});
await Theatre.create({
name: "Inox",
city: "Pune",
description: "Top class theatre",
pinCode: 411001,
movies : [movie5._id, movie2._id]
});
await Theatre.create({
name: "Sonmarg Theatre",
city: "Pune",
description: "Economical theatre",
pinCode: 411042,
movies : [movie3._id, movie2._id]
});
console.log("Theatres created");
}catch (err) {
console.error(err.message);
}
/**
* Creating one ADMIN user at the server boot time
*/
await User.collection.drop();
try {
user = await User.create({
name: "Admin",
userId: "1", // It should be atleat 16, else will throw error
email: "[email protected]", // If we don't pass this, it will throw the error
userType: "ADMIN",
password :bcrypt.hashSync("admin", 8) //this field should be hidden from the end user
});
console.log("ADMIN user created");
} catch (err) {
console.log(err.message);
}
}
/**
* Importing the routes
*/
require('./routes/movie.routes')(app);
require('./routes/theatre.routes')(app)
require('./routes/auth.routes')(app)
require('./routes/user.routes')(app)
require('./routes/booking.routes')(app)
require('./routes/payment.routes')(app)
/**
* Start the Server
*/
app.listen(serverConfig.PORT, () => {
console.log(`Application started on port num : ${serverConfig.PORT}`);
});