-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
82 lines (75 loc) · 2.57 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
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
/**
* Mongoose schema for movies.
* @typedef {Object} movieSchema
* @property {string} Title - The title of the movie.
* @property {string} Description - The description of the movie.
* @property {Object} Genre - The genre of the movie.
* @property {string} Genre.Name - The name of the genre.
* @property {string} Genre.Description - The description of the genre.
* @property {Object} Director - The director of the movie.
* @property {string} Director.Name - The name of the director.
* @property {string} Director.Bio - The biography of the director.
* @property {string[]} Actors - List of actors in the movie.
* @property {string} ImagePath - Path to the movie's image.
* @property {boolean} Featured - Flag to indicate if the movie is featured.
*/
let movieSchema = mongoose.Schema({
Title: { type: String, required: true },
Description: { type: String, required: true },
Genre: {
Name: String,
Description: String
},
Director: {
Name: String,
Bio: String
},
Actors: [String],
ImagePath: String,
Featured: Boolean
});
/**
* Mongoose schema for users.
* @typedef {Object} userSchema
* @property {string} Name - The name of the user.
* @property {string} Password - The user's password.
* @property {string} Email - The user's email address.
* @property {Date} Birthday - The user's birthday.
* @property {Array.<ObjectId>} FavoriteMovies - List of favorite movies (referencing Movie model).
*/
let userSchema = mongoose.Schema({
Name: { type: String, required: true },
Password: { type: String, required: true },
Email: { type: String, required: true },
Birthday: Date,
FavoriteMovies: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Movie' }]
});
/**
* Hashes a password using bcrypt.
* @param {string} password - The password to hash.
* @returns {string} The hashed password.
*/
userSchema.statics.hashPassword = (password) => {
return bcrypt.hashSync(password, 10);
};
/**
* Validates a password against the user's stored password.
* @param {string} password - The password to validate.
* @returns {boolean} True if the password matches, false otherwise.
*/
userSchema.methods.validatePassword = function (password) {
return bcrypt.compareSync(password, this.Password);
};
/**
* Movie model, created using the movieSchema.
*/
let Movie = mongoose.model('Movie', movieSchema);
/**
* User model, created using the userSchema.
*/
let User = mongoose.model('User', userSchema);
// Export the Movie and User models
module.exports.Movie = Movie;
module.exports.User = User;