diff --git a/backend/models/movie.js b/backend/models/movie.js new file mode 100644 index 0000000000000000000000000000000000000000..c50fb0d877121af35d27ba64fab655f2e8d80b4c --- /dev/null +++ b/backend/models/movie.js @@ -0,0 +1,50 @@ +const mongoose = require("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: 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( + "MovieModel", + MovieSchema, + "movies_populated" +); + +module.exports = MovieModel; diff --git a/backend/models/user.js b/backend/models/user.js index 1f70ae7101b636b12a6ede29a240dc691013d9ec..825eb3f1727cb5ef65f5e24a02c86f3209c7075c 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -1,10 +1,21 @@ 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 }, -}); +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 + toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals + } +); const UserModel = mongoose.model("UserModel", UserSchema, "users"); diff --git a/backend/populate.js b/backend/populate.js new file mode 100644 index 0000000000000000000000000000000000000000..4c5284a84da35e2b391dacd5294696bc00e22ff9 --- /dev/null +++ b/backend/populate.js @@ -0,0 +1,85 @@ +const mongoose = require("mongoose"); +const MovieModel = require("./models/movie"); +const axios = require("axios"); + +async function fetchMoviesFromTheMovieDatabase(n) { + // TODO: fetch movies from the The Movie Database API + try { + // Do something if call succeeded + const movieFetch = await axios.get( + `https://api.themoviedb.org/3/movie/popular?api_key=522d421671cf75c2cba341597d86403a&page=` + + n + ); + // console.log(movieFetch.data.results); + return movieFetch.data.results; + } catch (error) { + // Do something if call failed + console.error(error); + } +} + +async function populateMovies(movies) { + // TODO: populate movies into the database + try { + for (const movie of movies) { + const newMovie = await new MovieModel({ + // Movie attributes + adult: movie.adult, + backdrop_path: movie.backdrop_path, + genre_ids: movie.genre_ids, + id: movie.id, + original_language: movie.original_language, + original_title: movie.original_title, + overview: movie.overview, + popularity: movie.popularity, + poster_path: movie.poster_path, + release_date: movie.release_date, + title: movie.title, + video: movie.video, + vote_average: movie.vote_average, + vote_count: movie.vote_count, + }); + + // Create a new movie instance + const createdMovie = await newMovie.save(); + + // What to do after movie has been saved ! + console.log("Movie Saved !"); + console.log(createdMovie.title); + } + } catch (error) { + console.log(error); + } +} + +async function dropDataBase() { + // TODO: Drop the collections + try { + await MovieModel.deleteMany({}); + } catch (error) { + console.log(error); + } +} + +async function populate(N) { + // Connect mongoose client + const client = await mongoose.connect(process.env.MONGO_DB_URL); + await dropDataBase(); + + for (let n = 1; n < N; n++) { + const movies = await fetchMoviesFromTheMovieDatabase(n); + await populateMovies(movies); + console.log(n); + } + + // disconnect mongoose client + await client.disconnect(); +} + +populate(300) + .then(() => { + console.log("All done !"); + }) + .catch((error) => { + console.error(error); + });