-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.js
More file actions
74 lines (69 loc) · 2.01 KB
/
Copy pathmodels.js
File metadata and controls
74 lines (69 loc) · 2.01 KB
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
const mongoose = require("mongoose"),
Schema = mongoose.Schema;
const AnswerSchema = new Schema({
text: {
type: String,
required: [true, 'Answer must contain some text'],
min: [20, 'Answer must be at least 20 characters long']
},
votes: {
type: Number,
default: 0
},
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
});
AnswerSchema.method({
//**Do not** use arrow functions when you need 'this' to refer to the calling object
"update": function updateAnswer(updates, callback) {
Object.assign(this, updates, {
updatedAt: new Date()
});
if (callback) {
this.parent().save(callback);
} else {
return this.parent().save();
}
},
"vote": function voteAnswer(vote, callback) {
if (vote === "up") {
this.votes++;
} else if (vote === "down") {
this.votes--;
}
if (callback) {
this.parent().save(callback);
} else {
return this.parent().save(); //model#save returns a promise, so return this function call to send a promise out when called; see routes.js:84
}
}
});
const QuestionSchema = new Schema({
text: {
type: String,
required: [true, 'Question must contain some text'],
min: [20, 'Question must be at least 20 characters long']
},
createdAt: {
type: Date,
default: Date.now
},
answers: [AnswerSchema]
}).pre("save", function sanitizeInput(next) {
next();
}).pre("save", function sortAnswers(next) { //no arrow function!
if (this.answers) {
//Prevent the server from pooping itself if the question has no answers
this.answers.sort((a, b) => {
return b.votes === a.votes ? b.updatedAt - a.updatedAt : b.votes - a.votes;
});
}
next();
});
module.exports = mongoose.model("Question", QuestionSchema);