participation.js
1.08 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
const mongoose = require("mongoose");
const { Schema } = mongoose;
const SelectedProblemSchema=new Schema({
problemNum: {type: Number, required: true},
isSolved: {type:Boolean, default: false},
},{
_id: false
});
const ParticipationSchema = new Schema({
sessionId: { type: Schema.Types.ObjectId, ref: 'Session' },
groupId: { type: Schema.Types.ObjectId, ref: 'Group' },
problems: [{type:SelectedProblemSchema}]
},{
collection: 'particiaption'
});
ParticipationSchema.statics.findBySessionId=function(session){
return this.find({sessionId:session._id});
}
ParticipationSchema.statics.findByGroupId=function(group){
return this.find({groupId:group._id});
}
ParticipationSchema.methods.addProblem=function(problem){
this.problems.push({problemNum:problem.problemNum,isSolved:problem.isSolved});
}
ParticipationSchema.methods.serialize=function(){
let participationJSON=this.toJSON();
delete participationJSON._id;
return participationJSON;
}
const Participation = mongoose.model('Participation', ParticipationSchema);
module.exports = Participation;