-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
106 lines (98 loc) · 3.33 KB
/
models.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
/**
* @fileoverview Mongoose schema definitions and model creations for the application.
* @requires mongoose
* @requires bcrypt
*/
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
// Movie schema definition
/**
* @typedef {Object} Movie
* @property {string} Title - The title of the movie.
* @property {string} Description - A description of the movie.
* @property {Object} Genre - The genre information of the movie.
* @property {string} Genre.Name - The name of the genre.
* @property {string} Genre.Description - A description of the genre.
* @property {Object} Director - The director information of the movie.
* @property {string} Director.Name - The name of the director.
* @property {string} Director.Bio - A biography of the director.
* @property {Date} Director.Birth - The birth date of the director.
* @property {Date} Director.Death - The death date of the director (if applicable).
* @property {string[]} Actors - An array of actor names.
* @property {string} ImagePath - The path to an image representing the movie.
* @property {boolean} Featured - Whether the movie is featured or not.
*/
const movieSchema = mongoose.Schema({
Title: { type: String, required: true },
Description: { type: String, required: true },
Genre: {
Name: String,
Description: String,
},
Director: {
Name: String,
Bio: String,
Birth: Date,
Death: Date,
},
Actors: [String],
ImagePath: String,
Featured: Boolean,
});
// User schema definition
/**
* @typedef {Object} User
* @property {string} Username - The username of the user.
* @property {string} Password - The hashed password of the user.
* @property {string} Email - The email address of the user.
* @property {Date} Birthday - The birthday of the user.
* @property {mongoose.Types.ObjectId[]} FavoriteMovies - Array of references to favorite movies.
*/
const userSchema = mongoose.Schema({
Username: { type: String, required: true, unique: true },
Password: { type: String, required: true },
Email: { type: String, required: true, unique: true },
Birthday: Date,
FavoriteMovies: [{ type: mongoose.Schema.Types.ObjectId, ref: "Movie" }],
});
// Hash password middleware
/*
userSchema.pre("save", function (next) {
let user = this;
if (!user.isModified("Password")) return next();
bcrypt.hash(user.Password, 10, function (err, hash) {
if (err) return next(err);
user.Password = hash;
next();
});
});
*/
/**
* Hashes a given password using bcrypt.
*
* @function
* @param {string} password - The password to hash.
* @returns {string} The hashed password.
*/
userSchema.statics.hashPassword = (password) => {
return bcrypt.hashSync(password, 10);
};
/**
* Validates a given password against the stored hashed password.
*
* @function
* @param {string} password - The password to validate.
* @returns {boolean} True if the password is valid, otherwise false.
*/
userSchema.methods.validatePassword = function (password) {
return bcrypt.compareSync(password, this.Password);
};
// Models creation
/**
* @constant {mongoose.Model} Movie - The Movie model based on the movieSchema.
* @constant {mongoose.Model} User - The User model based on the userSchema.
*/
const Movie = mongoose.model("Movie", movieSchema);
const User = mongoose.model("User", userSchema);
module.exports.Movie = Movie;
module.exports.User = User;