diff --git a/backend/models/movie.js b/backend/models/movie.js index 6f93e0b6e226257056b7bb291c48a9a749e04b94..c50fb0d877121af35d27ba64fab655f2e8d80b4c 100644 --- a/backend/models/movie.js +++ b/backend/models/movie.js @@ -1,21 +1,44 @@ const mongoose = require("mongoose"); -const { Schema } = mongoose; -const MovieSchema = new mongoose.Schema({ - adult: { type: Boolean }, - backdrop_path: { type: String }, - genre_ids: [Number], - id: { type: Number, required: true, unique: true }, - original_language: { type: String }, - original_title: { type: String }, - overview: { type: String }, - popularity: { type: String }, - poster_path: { type: String }, - release_date: { type: String }, - title: { type: String }, - video: { type: Boolean }, - vote_average: { type: String }, - vote_count: { type: Number }, +const MovieSchema = new mongoose.Schema( + { + adult: { type: Boolean }, + backdrop_path: { type: String }, + genre_ids: [Number], + id: { type: Number, required: true, unique: true }, + original_language: { type: String }, + original_title: { type: String }, + overview: { type: String }, + popularity: { type: Number }, + poster_path: { type: String }, + release_date: { type: Date }, + title: { type: String }, + video: { type: Boolean }, + vote_average: { type: Number }, + vote_count: { type: Number }, + }, + { + toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals + toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals + } +); + +MovieSchema.virtual("liking_users", { + ref: "UserModel", + localField: "_id", // The user _id should match the viewers field in movies + foreignField: "liked_movies", +}); + +MovieSchema.virtual("later_watchers", { + ref: "UserModel", + localField: "_id", // The user _id should match the viewers field in movies + foreignField: "to_see_later", +}); + +MovieSchema.virtual("masking_users", { + ref: "UserModel", + localField: "_id", // The user _id should match the viewers field in movies + foreignField: "masked_movies", }); const MovieModel = mongoose.model( diff --git a/backend/models/user.js b/backend/models/user.js index f8b0dc63548530921acde194ac2a3aaa84ce904b..825eb3f1727cb5ef65f5e24a02c86f3209c7075c 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -1,10 +1,15 @@ const mongoose = require("mongoose"); +const { Schema } = mongoose; const UserSchema = new mongoose.Schema( { email: { type: String, required: true, unique: true }, firstName: { type: String }, lastName: { type: String }, + is_admin: { type: Boolean }, + liked_movies: [{ type: Schema.Types.ObjectId, ref: "MovieModel" }], + to_see_later: [{ type: Schema.Types.ObjectId, ref: "MovieModel" }], + masked_movies: [{ type: Schema.Types.ObjectId, ref: "MovieModel" }], }, { toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals @@ -12,12 +17,6 @@ const UserSchema = new mongoose.Schema( } ); -UserSchema.virtual("watchedMovies", { - ref: "MovieModel", - localField: "_id", // The user _id should match the viewers field in movies - foreignField: "viewers", -}); - const UserModel = mongoose.model("UserModel", UserSchema, "users"); module.exports = UserModel; diff --git a/backend/populate.js b/backend/populate.js index bd0564d853f7003994a4e89e37983630cadfd78c..79068d13577ef25229e975c89eeaf97f1aff18b8 100644 --- a/backend/populate.js +++ b/backend/populate.js @@ -61,22 +61,22 @@ async function dropDataBase() { } } -async function populate() { +async function populate(N) { // Connect mongoose client const client = await mongoose.connect(process.env.MONGO_DB_URL); await dropDataBase(); - for (let n = 1; n < 300; n++) { + for (let n = 1; n < N; n++) { const movies = await fetchMoviesFromTheMovieDatabase(n); await populateMovies(movies); console.log(n); } // disconnect mongoose client - // await client.disconnect(); + // await client.disconnect(); } -populate(2) +populate(N) .then(() => { console.log("All done !"); }) diff --git a/backend/routes/movies.js b/backend/routes/movies.js index 867ce0f6a45870cb77980ca98702f3fb7306eabf..9396b4e60963046233577afb5909e929ba2dee74 100644 --- a/backend/routes/movies.js +++ b/backend/routes/movies.js @@ -4,9 +4,22 @@ const router = express.Router(); module.exports = router; -router.get("/", async function (req, res) { +router.get("/popular/:number", async function (req, res) { try { - const getMovies = await MovieModel.find({}).populate("viewers"); + const filmNumber = await req.params["number"]; + const getMovies = await MovieModel.find({}) + .sort({ popularity: "desc" }) + .limit(filmNumber); + res.send(getMovies); + } catch (error) { + console.log(error); + } +}); + +router.get("/id/:id", async function (req, res) { + try { + const movieId = await req.params["id"]; + const getMovies = await MovieModel.findOne({ id: movieId }); res.send(getMovies); } catch (error) { console.log(error);