-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
105 lines (94 loc) · 3.09 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
/**
* @file models.js
* @description Defines Mongoose schemas and models for Users, Movies, Genres, Directors, and Actors.
*/
// Import mongoose to create the schemas and models
const mongoose = require('mongoose');
// Password hashing using bcrypt
const bcrypt = require('bcrypt');
/**
* Define SCHEMAS for Users, Movies, Genres, Directors, Actors
*/
let userSchema = mongoose.Schema({
username: { type: String, required: true },
password: { type: String, required: true },
email: { type: String, required: true },
birthday: { type: Date, required: true },
favorite_movies: [{ type: mongoose.Schema.Types.String, ref: 'Movie' }]
});
/**
* Hashes the password
* @param {string} password - The password to hash.
* @returns {string} - The hashed password.
*/
userSchema.statics.hashPassword = (password) => {
return bcrypt.hashSync(password, 10); // 10 is the number of rounds to process the data for
};
/**
* Compares hashed password with the entered password
* @param {string} password - The password to validate.
* @returns {boolean} - True if passwords match, false otherwise.
*/
userSchema.methods.validatePassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
/**
* Movie schema
*/
let movieSchema = mongoose.Schema({
_id: { type: String, required: true },
Title: { type: String, required: true },
ReleaseYear: String,
Rating: String,
Runtime: String,
Description: { type: String, required: true },
Genres: [{ type: mongoose.Schema.Types.String, ref: 'Genre' }],
Director: String,
Actors: [{ type: mongoose.Schema.Types.String, ref: 'Actor' }],
ImagePath: String,
Featured: Boolean
});
/**
* Genre schema
*/
let genreSchema = mongoose.Schema({
_id: { type: String, required: true },
name: { type: String, required: true },
description: { type: String, required: true }
});
/**
* Director schema
*/
let directorSchema = mongoose.Schema({
_id: { type: String, required: true },
name: { type: String, required: true },
birthDate: Date,
deathDate: Date,
bio: { type: String, required: true },
movies: [{ type: mongoose.Schema.Types.String, ref: 'Movie' }],
imagePath: { type: String, required: true }
});
/**
* Actor schema
*/
let actorSchema = mongoose.Schema({
_id: { type: String, required: true },
name: { type: String, required: true },
birthDate: Date,
deathDate: Date,
bio: { type: String, required: true },
movies: [{ type: mongoose.Schema.Types.String, ref: 'Movie' }],
imagePath: { type: String, required: true }
});
// Create the MODELS which use the SCHEMAS we just defined
let Movie = mongoose.model('Movie', movieSchema);
let User = mongoose.model('User', userSchema);
let Genre = mongoose.model('Genre', genreSchema);
let Director = mongoose.model('Director', directorSchema);
let Actor = mongoose.model('Actor', actorSchema);
// Export the MODELS so they may be used in the index.js file
module.exports.Movie = Movie;
module.exports.User = User;
module.exports.Genre = Genre;
module.exports.Director = Director;
module.exports.Actor = Actor;