송용우
Committed by GitHub

Merge pull request #15 from FacerAin/feature/database

Feature/database
...@@ -8,5 +8,18 @@ const GroupSchema = new Schema({ ...@@ -8,5 +8,18 @@ const GroupSchema = new Schema({
8 collection: 'group' 8 collection: 'group'
9 }); 9 });
10 10
11 +GroupSchema.methods.addGroupMemeber=function(user){
12 + this.members.push(user._id);
13 + return this.save();
14 +}
15 +
16 +GroupSchema.methods.getMembers=function(){
17 + return this.members;
18 +}
19 +
20 +GroupSchema.methods.serialize=function(){
21 + return this.toJSON();
22 +}
23 +
11 const Group = mongoose.model('Group',GroupSchema); 24 const Group = mongoose.model('Group',GroupSchema);
12 module.exports = Group; 25 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});
22 +}
23 +
24 +ParticipationSchema.statics.findByGroupId=function(group){
25 + return this.find({groupId:group._id});
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
......
...@@ -9,7 +9,7 @@ const ProblemSchema=new Schema({ ...@@ -9,7 +9,7 @@ const ProblemSchema=new Schema({
9 sumbitNum: {type: Number, required: true}, 9 sumbitNum: {type: Number, required: true},
10 correctNum: {type: Number, required: true}, 10 correctNum: {type: Number, required: true},
11 count: { type: Number }, 11 count: { type: Number },
12 - category: {type:[String]} 12 + category: [{ type:String }],
13 },{ 13 },{
14 collection: 'problem' 14 collection: 'problem'
15 }); 15 });
......
...@@ -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});
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(friend._id);
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;
......