-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
297 lines (266 loc) · 8.34 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
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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
app.use(express.json());
const cors = require("cors");
app.use(cors());
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
require('dotenv').config();
const uri = process.env.MONGODB_URI;
mongoose
.connect(uri, {
useNewUrlParser: true,
})
.then(() => {
console.log("Connected to database");
})
.catch((e) => console.log(e));
require("./imageDetails");
require("./movieDetails");
require("./userDetails");
require("./upcomingMovieDetails");
require("./movieFeedbackDetails");
const deleteImage = require('./deleteImage');
// Parse JSON request bodies
app.use(express.json());
const Images = mongoose.model("ImageDetails");
const Movie = mongoose.model("MovieDetails");
const User = mongoose.model("UserDetails");
const UpcomingMovie = mongoose.model("UpcomingMovieDetails");
const Feedback = mongoose.model("MovieFeedbackDetails")
const port = process.env.PORT || 3000;
// Listen on `port` and 0.0.0.0
app.listen(port, "0.0.0.0", function () {
console.log("Server Started");
});
// const port = process.env.PORT || 5000;
// app.listen(port, () => {
// console.log("Server Started");
// console.log(`App is running on port ${port}`);
// });
// Define the route for deleting an image
app.post('/delete-image', deleteImage);
// Get Single Movie Data
app.post("/singleMovieData/:_id", async (req, res) => {
const { _id } = req.params;
try {
const movie = await Movie.findOne({ _id: _id });
if (movie) {
res.send({ status: "ok", data: movie });
} else {
res.send({ status: "not found", data: null });
}
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Post the count of Movies, Web Series, and Anime by category
app.post("/categoryCounts", async (req, res) => {
const { userid } = req.body;
try {
const favouriteMovieIds = await User.distinct("movieid", { userid: userid });
const movieCount = await Movie.find({ _id: { $in: favouriteMovieIds } }).countDocuments({ category: "Movies" });
const webSeriesCount = await Movie.find({ _id: { $in: favouriteMovieIds } }).countDocuments({ category: "Web-Series" });
const animeCount = await Movie.find({ _id: { $in: favouriteMovieIds } }).countDocuments({ category: "Anime" });
res.send({
status: "ok",
data: {
movies: movieCount,
webSeries: webSeriesCount,
anime: animeCount,
},
});
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Get Web Series by Category
app.post("/webSeriesByCategory", async (req, res) => {
const { category } = req.body; // Assuming the category is sent in the request body
try {
const Movies = await Movie.find({ category: category });
if (Movies.length > 0) {
res.send({ status: "ok", data: Movies });
} else {
res.send({ status: "not found", data: null });
}
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Post All Movie Data
app.post("/movieData", async (req, res) => {
try {
const allMovies = await Movie.find().sort({ _id: -1 }); // Sort by _id in descending order
res.send({ status: "ok", data: allMovies });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Post All Movie Data in Alphabetical Order
app.post("/movieDataAlphabetical", async (req, res) => {
try {
const allMovies = await Movie.find().sort({ name: 1 }); // Sort by title in ascending order
res.send({ status: "ok", data: allMovies });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Get All Movie Data
app.get("/allMovieData", async (req, res) => {
try {
const allMovies = await Movie.find().sort({ _id: -1 }); // Sort by _id in descending order
res.send({ status: "ok", data: allMovies });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Upload Movie
app.post("/upload-movie", async (req, res) => {
const { Pid, name, information, trailerLink, downloadLink, language, image, genre, category, formattedDate } = req.body;
try {
await Movie.create({
Pid,
name,
information,
trailerLink,
downloadLink,
language,
image,
genre,
category,
formattedDate,
});
res.send({ status: "ok" });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Delete Movie
app.post("/deleteMovie", async (req, res) => {
const { movie_id } = req.body;
try {
Movie.deleteOne({ _id: movie_id }, function (err, res) {
console.log(err);
});
res.send({ status: "Ok", data: "Deleted" });
} catch (error) {
console.log(error);
}
});
//Update Movie Details
app.post("/movieUpdate", (req,res) => {
Movie.findByIdAndUpdate(req.body._id, {downloadLink:req.body.downloadLink},(err, data) =>{
if(err){
return res.status(500).send(err)
}else{
return res.status(200).send(data)
}
})
});
// API endpoint to add a movie to the user's favorites
app.post("/favourate-movie", async (req, res) => {
const { movieid, userid } = req.body;
try {
const existingFavorite = await User.findOne({ movieid, userid });
if (existingFavorite) {
res.send({ status: "already_in_favorites", data: existingFavorite });
} else {
await User.create({
movieid,
userid,
});
res.send({ status: "ok" });
}
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Get all favorites movie data of user
app.post("/allFavoritesMovie", async (req, res) => {
const { userid } = req.body;
try {
const favouriteMovieIds = await User.distinct("movieid", { userid: userid });
const favouriteMovies = await Movie.find({ _id: { $in: favouriteMovieIds } }).sort({ _id: -1 });
res.send({ status: "ok", data: favouriteMovies });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Remove movie from favourite list
app.post("/removeMovie", async (req, res) => {
const { movie_id, userid } = req.body;
try {
// Remove the movie from the User collection
await User.deleteOne({ movieid: movie_id, userid: userid });
res.send({ status: "ok", data: "Deleted" });
} catch (error) {
res.send({ status: "error", data: error.message });
}
});
// Upload Upcoming Movie
app.post("/upload-upcoming-movie", async (req, res) => {
const { Pid, name, image, formattedDate } = req.body;
try {
const oldestMovie = await UpcomingMovie.findOne({}, {}, { sort: { uploadDate: 1 } });
if (oldestMovie) {
await UpcomingMovie.findOneAndDelete({ _id: oldestMovie._id });
}
await UpcomingMovie.create({
Pid,
name,
image,
formattedDate,
});
res.send({ status: "ok" });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Post Upcoming Movie Data
app.post("/upcomingMovieData", async (req, res) => {
try {
const lastTwoMovies = await UpcomingMovie.find().sort({ _id: -1 }).limit(2); // Sort by _id in descending order and limit to 2 documents
res.send({ status: "ok", data: lastTwoMovies });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Upload User Feedback
app.post("/upload-movie-feedback", async (req, res) => {
const { feedback, rating} = req.body;
try {
await Feedback.create({
feedback,
rating,
});
res.send({ Status: "ok" })
} catch (error) {
res.send({ Status: "error", data: error });
}
})
// Get User Feedback
app.post("/feedbackData", async (req, res) => {
try {
const lastTwoMovies = await Feedback.find().sort({ _id: -1 }).limit(3); // Sort by _id in descending order and limit to 2 documents
res.send({ status: "ok", data: lastTwoMovies });
} catch (error) {
res.send({ status: "error", data: error });
}
});
// Get top 3 favorites movie data added by most users
app.post("/topFavoritesMovie", async (req, res) => {
try {
const topFavouriteMovies = await User.aggregate([
{ $group: { _id: "$movieid", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $limit: 4 },
]);
const topMovieIds = topFavouriteMovies.map((movie) => movie._id);
const topMoviesData = await Movie.find({ _id: { $in: topMovieIds } }).sort({ _id: -1 });
res.send({ status: "ok", data: topMoviesData });
} catch (error) {
res.send({ status: "error", data: error });
}
});