From fc112b685ea232d34596a0fcdb594b792b981157 Mon Sep 17 00:00:00 2001 From: El Yaagoubi Bilel <bilel.el-yaagoubi@student-cs.fr> Date: Wed, 8 Jun 2022 09:56:48 +0200 Subject: [PATCH] db_structure --- backend/models/movie.js | 50 +++++++++++++++++++++++++++++++++++++++++ backend/models/user.js | 21 ++++++++++++----- 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 backend/models/movie.js diff --git a/backend/models/movie.js b/backend/models/movie.js new file mode 100644 index 0000000..4517a04 --- /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 1f70ae7..825eb3f 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"); -- GitLab