JJuOn

Add statics and methods to models

...@@ -8,5 +8,10 @@ const GroupSchema = new Schema({ ...@@ -8,5 +8,10 @@ const GroupSchema = new Schema({
8 collection: 'group' 8 collection: 'group'
9 }); 9 });
10 10
11 +GroupSchema.methods.addGroupMemeber=function(user){
12 + this.members.push({$oid:user._id.$oid});
13 + return this.save();
14 +}
15 +
11 const Group = mongoose.model('Group',GroupSchema); 16 const Group = mongoose.model('Group',GroupSchema);
12 module.exports = Group; 17 module.exports = Group;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,12 +2,32 @@ const mongoose = require("mongoose"); ...@@ -2,12 +2,32 @@ const mongoose = require("mongoose");
2 2
3 const { Schema } = mongoose; 3 const { Schema } = mongoose;
4 4
5 +const SelectedProblemSchema=new Schema({
6 + problemNum: {type: Number, required: true},
7 + isSolved: {type:Boolean, default: false},
8 +},{
9 + _id: false
10 +});
11 +
5 const ParticipationSchema = new Schema({ 12 const ParticipationSchema = new Schema({
6 sessionId: { type: Schema.Types.ObjectId, ref: 'Session' }, 13 sessionId: { type: Schema.Types.ObjectId, ref: 'Session' },
7 - groupId: { type: Schema.Types.ObjectId, ref: 'Group' } 14 + groupId: { type: Schema.Types.ObjectId, ref: 'Group' },
15 + problems: [{type:SelectedProblemSchema}]
8 },{ 16 },{
9 collection: 'particiaption' 17 collection: 'particiaption'
10 }); 18 });
11 19
20 +ParticipationSchema.statics.findBySessionId=function(session){
21 + return this.find({sessionId:session._id.$oid});
22 +}
23 +
24 +ParticipationSchema.statics.findByGroupId=function(group){
25 + return this.find({groupId:group._id.$oid});
26 +}
27 +
28 +ParticipationSchema.methods.addProblem=function(problem){
29 + this.problems.push({problemNum:problem.problemNum,isSolved:problem.isSolved});
30 +}
31 +
12 const Participation = mongoose.model('Participation', ParticipationSchema); 32 const Participation = mongoose.model('Participation', ParticipationSchema);
13 module.exports = Participation; 33 module.exports = Participation;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -11,5 +11,25 @@ const SessionSchema = new Schema({ ...@@ -11,5 +11,25 @@ const SessionSchema = new Schema({
11 collection: 'session' 11 collection: 'session'
12 }); 12 });
13 13
14 +SessionSchema.statics.findByChallengeId=function(challenge){
15 + return this.find({challengeId:challenge._id.$oid});
16 +}
17 +
18 +SessionSchema.methods.getSessionStartDate=function(){
19 + return this.sessionStartDate;
20 +}
21 +
22 +SessionSchema.methods.getSessionEndDate=function(){
23 + return this.sessionEndDate;
24 +}
25 +
26 +SessionSchema.methods.getIsOpen=function(){
27 + return this.isOpen;
28 +}
29 +
30 +SessionSchema.methods.serialize=function(){
31 + return this.toJSON();
32 +}
33 +
14 const Session = mongoose.model('Session', SessionSchema); 34 const Session = mongoose.model('Session', SessionSchema);
15 module.exports = Session; 35 module.exports = Session;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -21,6 +21,11 @@ UserSchema.statics.findByUsername = function (username) { ...@@ -21,6 +21,11 @@ UserSchema.statics.findByUsername = function (username) {
21 return this.findOne({ username }); 21 return this.findOne({ username });
22 }; 22 };
23 23
24 +UserSchema.methods.addFriend=function(friend){
25 + this.friendList.push({$oid:friend._id.$oid});
26 + return this.save();
27 +}
28 +
24 UserSchema.methods.setPassword = async function (password) { 29 UserSchema.methods.setPassword = async function (password) {
25 const hash = await bcrypt.hash(password, 10); 30 const hash = await bcrypt.hash(password, 10);
26 this.hashedPassword = hash; 31 this.hashedPassword = hash;
......