-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
309 lines (277 loc) · 11.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
//===============================================// INDEX.js \\======================================================
//===========================================// MuVies MOVIE API \\==================================================
//===================================================================================================================
//-------------------------------------------------------------------------------------------------// IMPORTS Require
const express = require('express');
const app = express();
const mongoose = require('mongoose');
mongoose.connect(process.env.CONNECTION_URI);
const port = process.env.PORT || 8080;
const morgan = require('morgan');
const passport = require('passport');
const { check, validationResult } = require('express-validator');
// =================================================================================================>// MODELS Schema
const Models = require('./models.js');
const Movies = Models.Movie;
const users = Models.users;
const Directors = Models.Directors;
const genres = Models.genres;
const favMovies = Models.favMovies;
const actors = Models.actors;
//===================================================================================================//BODY Parser
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const cors = require('cors');
app.use(cors());
let auth = require('./auth')(app);
require('./passport');
//-----------------------------------------------------------------------------------------------------// USE MORGAN (Not sure WHY?)
app.use(morgan('public'));
//-----------------------------------------------------------------------------------------------------// EXPRESS-IN PUBLIC FOLDER
app.use(express.static('public'));
app.use('/public', express.static('public'));
//--------------------------------------------------------------------------------------------------// GET ALL Movies
app.get('/Movies', passport.authenticate('jwt', { session: false }),(req, res) => {
Movies.find()
.then((Movies) => {
res.status(201).json(Movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
})
//---------------------------------------------------------------------------------------------// GET Movie By Title
app.get('/Movies/:Title',passport.authenticate('jwt', { session: false }),(req, res) => {
Movies.find({ Title: req.params.Title })
.then((Movies) => {
res.json(Movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//--------------------------------------------------------------------------------------// GET Movie by Genre Name
app.get('/Movies/Genre/:Name',passport.authenticate('jwt', { session: false }),(req, res) => {
Movies.findOne({ 'Genre.Name': req.params.Name })
.then((Movies) => {
res.json(Movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//------------------------------------------------------------------------------------// GET Movie by Director Name
app.get('/Movies/Actors/:Name',passport.authenticate('jwt', { session: false }),(req, res) => {
actors.findOne({ 'actor.Name': req.params.Name })
.then((actors) => {
res.json(actors);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//------------------------------------------------------------------------------------// GET Movie by Director Name
app.get('/Movies/Director/:Name',passport.authenticate('jwt', { session: false }),(req, res) => {
Movies.findOne({ 'Director.Name': req.params.Name })
.then((Movies) => {
res.json(Movies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//--------------------------------------------------------------------------------------------------// GET All Genres
app.get('/genres', passport.authenticate('jwt', { session: false }),(req, res) => {
genres.find()
.then((genres) => {
res.status(201).json(genres);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//----------------------------------------------------------------------------------------------------// GET One Genre
app.get('/genres/:Name',passport.authenticate('jwt', { session: false }),(req, res) => {
genres.findOne({ Name: req.params.Name })
.then((genres) => {
res.json(genres);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//----------------------------------------------------------------------------------------------// GET All Directors
app.get('/directors',passport.authenticate('jwt', { session: false }),(req, res) => {
Directors.find()
.then((Directors) => {
res.status(201).json(Directors);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//-------------------------------------------------------------------------------------------------// GET Director
app.get('/directors/:Name',passport.authenticate('jwt', { session: false }), (req, res) => {
Directors.find({ Name: req.params.Name })
.then((Directors) => {
res.json(Directors);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//----------------------------------------------------------------------------------------------------// GET FAV MOVIE
app.get('/favMovies/:UserName',passport.authenticate('jwt', { session: false }), (req, res) => {
favMovies.findOne({'_id.UserName': req.params.UserName })
.then((favMovie) => {
res.json(favMovie);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//--------------------------------------------------------------------------------------------------------// GET FAV MOVIE
app.get('/favMovies',passport.authenticate('jwt', { session: false }), (req, res) => {
favMovies.find()
.then((favMovies) => {
res.json(favMovies);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//---------------------------------------------------------------------------------------------// POST NEW FAV MOVIE
app.post('/favMovies/newFav/:UserName',passport.authenticate('jwt', { session: false }), (req, res) => {
favMovies.findOneAndUpdate({ '_id.UserName': req.params.UserName }, {
$push: {"FavoriteMovies":[{"_id": req.body._id,
"Title": req.body.Title,
"Genre": req.body.genres}]}
},
{ new: true }, // This line makes sure that the updated document is returned
(err, updatedUser) => {
if (err) {
console.error(err);
res.status(500).send('Error: ' + err);
} else {
res.json(updatedUser);
}
});
});
//------------------------------------------------------------------------------------------// DELETE Favorite Movie
app.delete('/favMovies/:UserName/delete/:id',passport.authenticate('jwt', { session: false }), (req, res) => {
favMovies.findOne({ '_id.UserName': req.params.UserName})
.then((favMovies) => {
if (!favMovies) {
res.status(400).send('ID: ' + req.params._id + ' was not found!!');
} else {
favMovies.deleteOne({"FavoriteMovies":[{"_id:": `ObjectId:${req.params._id}`}]});
res.status(200).send('ID: ' + req.params._id + ' was deleted!');
}
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//===================================================================================================// USER REGISTRY
//===================================================================================================//
//--------------------------------------------------------------------------------------------// GET Users By Username
app.get('/users/:UserName', passport.authenticate('jwt', { session: false }),(req, res) => {
users.findOne({ UserName: req.params.UserName })
.then((Users) => {
res.json(Users);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//--------------------------------------------------------------------------------------------------// GET All Users
app.get('/users', passport.authenticate('jwt', { session: false }), (req, res) => {
users.find()
.then((users) => {
res.status(201).json(users);
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//------------------------------------------------------------------------------------------------// CREATE NEW USER
app.post('/Users/NewUser/:UserName', (req, res) => {
[
check('Username', 'Username is required').isLength({min: 5}),
check('Username', 'Username contains non alphanumeric characters - not allowed.').isAlphanumeric(),
check('Password', 'Password is required').not().isEmpty(),
check('Email', 'Email does not appear to be valid').isEmail()
], (req, res) => {
// check the validation object for errors
let errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}}
let hashedPassword = users.hashPassword(req.body.Password);
users.findOne({ UserName: req.body.UserName }) // Search to see if a user with the requested username already exists
.then((user) => {
if (user) {
//If the user is found, send a response that it already exists
return res.status(400).send(req.body.UserName + ' already exists');
} else {
users
.create(
{ _id: req.body._id,
UserName: req.body.UserName,
Password: hashedPassword,
Email: req.body.Email,
Birthday: req.body.Birthday,
FavoriteMovies:[req.body.favMovies],
ImagePath: req.body.ImagePath,
})
.then((user) => { res.status(201).json(user) })
.catch((error) => {
console.error(error);
res.status(500).send('Error: ' + error);
});
}
})
.catch((error) => {
console.error(error);
res.status(500).send('Error: ' + error);
});
});
//----------------------------------------------------------------------------------------------------// DELETE User
app.delete('/users/remove/:UserName', passport.authenticate('jwt', { session: false }),(req, res) => {
users.deleteOne({ UserName: req.params.UserName })
.then((users) => {
if (!users) {
res.status(400).send(req.params.UserName + ' was not found');
} else {
res.status(200).send(req.params.UserName + ' was deleted.');
}
})
.catch((err) => {
console.error(err);
res.status(500).send('Error: ' + err);
});
});
//-------------------------------------------------------------------------------------------------// ERROR MESSAGE
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Oh No!!! Something broke!');
});
//-----------------------------------------------------------------------------------------------------// PORT CALL
app.listen(port, '0.0.0.0',() => {
console.log('Listening on Port ' + port);
});