diff --git a/backend/models/movie.js b/backend/models/movie.js new file mode 100644 index 0000000000000000000000000000000000000000..4517a040c2261b7c42f499fb7a9ef47f535a002a --- /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: String }, + poster_path: { type: String }, + release_date: { type: String }, + title: { type: String }, + video: { type: Boolean }, + vote_average: { type: String }, + 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");