-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
55 lines (43 loc) · 1.2 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
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var sortAnswers = function(a,b){
// -ve if a before b
// 0 no change
// +ve a after b
if (a.votes === b.votes){
return b.votes - a.votes;
}
return b.votes - a.votes;
}
// creating our Answer schema
var AnswerSchema = new Schema({
text: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
votes: { type: Number, default:0 }
});
AnswerSchema.method("update", function(update,callback){
Object.assign(this,update,{updatedAt: new Date()});
this.parent().save(callback);
});
AnswerSchema.method("vote", function(vote,callback){
if(vote == "up"){
this.votes += 1;
} else {
this.votes -= 1;
}
this.parent().save(callback);
});
// creating our Question schema and nesting Answer schema inside
var QuestionSchema = new Schema({
text: String,
createdAt: { type: Date, default: Date.now },
answers: [AnswerSchema]
});
QuestionSchema.pre("save", function(next){
this.answers.sort(sortAnswers);
next();
});
var Question = mongoose.model("Question", QuestionSchema);
module.exports.Question = Question;