forked from cmda-minor-web/API-2324
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
290 lines (243 loc) · 8.5 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
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
import dotenv from 'dotenv';
import express from 'express';
import session from 'express-session';
import { MongoClient, ServerApiVersion } from 'mongodb';
import { ObjectId } from 'mongodb';
dotenv.config();
// Connecting mongoDB
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri, {
serverApi: ServerApiVersion.v1,
});
const dbName = 'api';
client
.connect()
.then(() => {
console.log('Connected to MongoDB\'s API database!');
})
.catch((err) => {
console.error('Error connecting to MongoDB:', err);
});
const db = client.db(dbName);
const users = db.collection('users');
const posts = db.collection('posts');
const app = express();
let userIdObject;
// Set up middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
// Configure sessions
app.use(
session({
secret: process.env.SESSION_SECRET || 'secret',
resave: false,
saveUninitialized: true,
cookie: { secure: false },
})
);
app.set('view engine', 'ejs');
// Middleware function to check if user is authenticated
const requireLogin = (req, res, next) => {
if (req.session && req.session.userId) {
return next();
}
res.redirect('/login');
};
// Apply authentication middleware to all routes except for login
app.use((req, res, next) => {
if (req.path === '/login') {
return next(); // Exclude login route from authentication requirement
}
requireLogin(req, res, next);
});
app.use((req, res, next) => {
res.locals.userId = req.session.userId;
next();
});
// Define fetch function
async function fetchMovies (apiToken) {
const url = `https://api.themoviedb.org/3/discover/movie?api_key=${apiToken}&include_adult=false&include_video=false&language=en-US&page=1&primary_release_year=2010&sort_by=popularity.desc`;
const response = await fetch(url);
const data = await response.json();
// Loop through each movie and round the vote_average attribute to one decimal place
data.results.forEach(movie => {
movie.vote_average = Math.round(movie.vote_average * 10) / 10; // Round to one decimal place
// Format vote_count to display in thousands
movie.vote_count = formatVoteCount(movie.vote_count);
// set the data as day- month- year
movie.release_date = movie.release_date.split('-').reverse().join('-');
});
return data.results;
}
// Define the function to fetch movie details from TMDb
async function fetchMovieDetails (movieId, apiToken) {
const url = `https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiToken}&include_adult=false&include_video=false&language=en-US&page=1&primary_release_year=2010&sort_by=popularity.desc`;
const response = await fetch(url);
const data = await response.json();
return data;
}
function formatVoteCount (voteCount) {
if (voteCount >= 1000) {
return (voteCount / 1000).toFixed(0) + 'k';
}
return voteCount;
}
// Define routes
// login page route
app.get('/login', (req, res) => {
res.render('pages/login', { error: null });
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
users
.findOne({ username, password })
.then((user) => {
if (user) {
req.session.userId = user._id; // Set user session
req.session.username = user.username; // Set username session
console.log('User logged in:', user);
res.redirect('/');
} else {
res.render('pages/login', { error: 'Invalid username or password' });
}
})
.catch((err) => {
console.error('Error finding user:', err);
res.status(500).send('Internal Server Error');
});
});
// Logout route
app.post('/logout', (req, res) => {
req.session.destroy((err) => {
if (err) {
console.error('Error destroying session:', err);
res.status(500).send('Internal Server Error');
} else {
res.redirect('/login');
}
});
});
// home page route
app.get('/', async function (req, res) {
try {
const movies = await fetchMovies(process.env.API_TOKEN);
const userId = req.session.userId;
userIdObject = new ObjectId(userId);
const userLoggedIn = await users.findOne({ _id: userIdObject });
res.render('pages/index', { movies, user: userLoggedIn, message: '' });
} catch (error) {
console.error('Fetching movies failed:', error);
res.status(500).send('Failed to fetch movies');
}
});
// handle bookmarking
app.post('/', async (req, res) => {
try {
const movies = await fetchMovies(process.env.API_TOKEN);
const { movieId, title, releaseDate, overview, voteAverage, voteCount, popularity, poster } = req.body;
const userId = req.session.userId;
userIdObject = new ObjectId(userId);
const userLoggedIn = await users.findOne({ _id: userIdObject });
if (!userId) {
res.status(401).json({ error: 'User not authenticated' });
return;
}
// Check if the post already exists for the current user
const existingPost = await posts.findOne({ movieId, userId });
if (existingPost) {
// If the post exists, delete it (unbookmark the movie)
await posts.deleteMany({ movieId, userId });
// Now use userIdObject in your update query
if (userIdObject) {
await users.updateOne(
{ _id: userIdObject },
{ $pull: { bookmarkedMovies: parseInt(movieId) } }
);
} else {
console.error('userId is not a valid ObjectId:', userId);
}
res.render('pages/index', { movies, message: 'verwijderd', userId: userId, user: userLoggedIn });
} else {
// If the post doesn't exist, save it (bookmark the movie)
const postData = {
movieId: movieId,
userId: userId,
title: title,
releaseDate: releaseDate,
overview: overview,
voteAverage: voteAverage,
voteCount: voteCount,
popularity: popularity,
poster: poster,
bookmarked: true,
};
await posts.insertOne(postData);
// Update user document to include the bookmarked movie
await users.updateOne(
{ _id: userIdObject },
{
$addToSet: { bookmarkedMovies: parseInt(movieId) }, // Add movieId to the bookmarkedMovies array
},
{ upsert: true } // Creates the field if it doesn't exist
);
res.render('pages/index', { movies, message: 'toegevoegd', userId: userId, user: userLoggedIn });
}
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Failed to process request' });
}
});
// account page route
app.get('/account', async function (req, res) {
try {
const userName = req.session.username;
const userLoggedIn = await users.findOne({ username: userName });
const bookmarkedMovies = await posts.find({ userId: userLoggedIn._id.toString() }).toArray();
res.render('pages/account', { user: userLoggedIn, bookmarkedMovies });
} catch (error) {
console.error('Fetching movies failed:', error);
res.status(500).send('Failed to fetch movies');
}
});
// details popup
app.get('/details', async (req, res) => {
try {
const movieId = req.query.movieId;
const movieDetails = await fetchMovieDetails(movieId, process.env.API_TOKEN);
// Send the movie details back to the client
res.json(movieDetails);
} catch (error) {
console.error('Error fetching movie details:', error);
res.status(500).json({ error: 'Failed to fetch movie details' });
}
});
// Search page route
app.get('/search', async (req, res) => {
let movies = [];
let { search } = req.query;
const userName = req.session.username;
const userLoggedIn = await users.findOne({ username: userName });
try {
const apiToken = process.env.API_TOKEN;
if (search) {
// If there is a search query, fetch movies based on the query
const url = `https://api.themoviedb.org/3/search/movie?api_key=${apiToken}&query=${encodeURIComponent(search)}&include_adult=false&include_video=false&language=en-US&page=1&primary_release_year=2010&sort_by=popularity.desc`;
const response = await fetch(url);
const data = await response.json();
movies = data.results;
} else {
// If no search query, show default movies
console.log('No search query, fetching default movies');
movies = await fetchMovies(apiToken);
}
res.render('pages/search', { movies, searchQuery: search, user: userLoggedIn });
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});