JJuOn

Add participation, session and group schema and Edit challenge and user schema

1 const mongoose = require("mongoose"); 1 const mongoose = require("mongoose");
2 2
3 -const { Schema }=mongoose; 3 +const { Schema } = mongoose;
4 -
5 -const GroupSchema=new Schema({
6 - members:{type:[String]},
7 -});
8 4
9 const ChallengeSchema=new Schema({ 5 const ChallengeSchema=new Schema({
10 challengeName: {type: String, required: true}, 6 challengeName: {type: String, required: true},
...@@ -12,24 +8,13 @@ const ChallengeSchema=new Schema({ ...@@ -12,24 +8,13 @@ const ChallengeSchema=new Schema({
12 endDate: {type: Object, required: true}, 8 endDate: {type: Object, required: true},
13 durationPerSession: {type: String, required: true}, // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session. 9 durationPerSession: {type: String, required: true}, // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session.
14 goalPerSession: {type: Number, required:true}, // number of problems for one session 10 goalPerSession: {type: Number, required:true}, // number of problems for one session
15 - groups:{type: [GroupSchema], required:true}, // groups attending challenge, group of only one member supposed to be single 11 + isOpen: { type: Boolean }
16 }); 12 });
17 13
18 ChallengeSchema.statics.findByChallengeName=function(challengeName){ 14 ChallengeSchema.statics.findByChallengeName=function(challengeName){
19 return this.findOne({challengeName:challengeName}); 15 return this.findOne({challengeName:challengeName});
20 } 16 }
21 17
22 -ChallengeSchema.methods.addNewGroup=function(group){
23 - this.groups.push(group);
24 - return this.save();
25 -}
26 -
27 -ChallengeSchema.methods.removeGroup=function(group_id){
28 - const idx=this.groups.findIndex((item)=>item._id===group_id);
29 - this.groups.splice(idx,1);
30 - return this.save();
31 -}
32 -
33 ChallengeSchema.methods.getChallengeName=function(){ 18 ChallengeSchema.methods.getChallengeName=function(){
34 return this.challengeName; 19 return this.challengeName;
35 } 20 }
...@@ -50,13 +35,9 @@ ChallengeSchema.methods.getGoalPerSession=function(){ ...@@ -50,13 +35,9 @@ ChallengeSchema.methods.getGoalPerSession=function(){
50 return this.goalPerSession; 35 return this.goalPerSession;
51 } 36 }
52 37
53 -ChallengeSchema.methods.getGroups=function(){
54 - return this.groups;
55 -}
56 -
57 ChallengeSchema.methods.serialize=function(){ 38 ChallengeSchema.methods.serialize=function(){
58 return this.toJSON(); 39 return this.toJSON();
59 } 40 }
60 41
61 -const Challenge=mongoose.model('Challenge',ChallengeSchema);
62 -module.exports=Challenge;
...\ No newline at end of file ...\ No newline at end of file
42 +const Challenge = mongoose.model('Challenge', ChallengeSchema);
43 +module.exports = Challenge;
...\ No newline at end of file ...\ No newline at end of file
......
1 +const mongoose = require("mongoose");
2 +
3 +const { Schema } = mongoose;
4 +
5 +const GroupSchema = new Schema({
6 + members: [{ type: Schema.Types.ObjectId, ref: 'User' }]
7 +});
8 +
9 +const Group = mongoose.model('Group',GroupSchema);
10 +module.exports = Group;
...\ No newline at end of file ...\ No newline at end of file
1 +const mongoose = require("mongoose");
2 +
3 +const { Schema } = mongoose;
4 +
5 +const ParticipationSchema = new Schema({
6 + sessionId: { type: Schema.Types.ObjectId, ref: 'Session' },
7 + groupId: { type: Schema.Types.ObjectId, ref: 'Group' }
8 +});
9 +
10 +const Participation = mongoose.model('Participation', ParticipationSchema);
11 +module.exports = Participation;
...\ No newline at end of file ...\ No newline at end of file
1 +const mongoose = require("mongoose");
2 +
3 +const { Schema } = mongoose;
4 +
5 +const SessionSchema = new Schema({
6 + challengeId: { type: Schema.Types.ObjectId, ref: 'Challenge' },
7 + sessionStartDate: { type: Object },
8 + sessionEndDate: { type: Object }
9 +});
10 +
11 +const Session = mongoose.model('Session', SessionSchema);
12 +module.exports = Session;
...\ No newline at end of file ...\ No newline at end of file
...@@ -10,7 +10,7 @@ const UserSchema = new Schema({ ...@@ -10,7 +10,7 @@ const UserSchema = new Schema({
10 userBJID: String, 10 userBJID: String,
11 sovledBJ: Object, 11 sovledBJ: Object,
12 solvedBJ_date: Object, 12 solvedBJ_date: Object,
13 - friendList: [String], 13 + friendList: [{ type: Schema.Types.ObjectId, ref: 'User' }],
14 slackWebHookURL: String, 14 slackWebHookURL: String,
15 goalNum: Number, 15 goalNum: Number,
16 }); 16 });
......