JJuOn

Fix model methods to omit _id

1 const mongoose = require("mongoose"); 1 const mongoose = require("mongoose");
2 +const challenge = require("../api/challenge");
2 3
3 const { Schema } = mongoose; 4 const { Schema } = mongoose;
4 5
...@@ -42,7 +43,9 @@ ChallengeSchema.methods.getStatus=function(){ ...@@ -42,7 +43,9 @@ ChallengeSchema.methods.getStatus=function(){
42 } 43 }
43 44
44 ChallengeSchema.methods.serialize=function(){ 45 ChallengeSchema.methods.serialize=function(){
45 - return this.toJSON(); 46 + let challengeJSON = this.toJSON();
47 + delete challenge._id;
48 + return challengeJSON;
46 } 49 }
47 50
48 const Challenge = mongoose.model('Challenge', ChallengeSchema); 51 const Challenge = mongoose.model('Challenge', ChallengeSchema);
......
...@@ -3,12 +3,17 @@ const mongoose = require("mongoose"); ...@@ -3,12 +3,17 @@ const mongoose = require("mongoose");
3 const { Schema } = mongoose; 3 const { Schema } = mongoose;
4 4
5 const GroupSchema = new Schema({ 5 const GroupSchema = new Schema({
6 + groupName: { type: String },
6 members: [{ type: Schema.Types.ObjectId, ref: 'User' }] 7 members: [{ type: Schema.Types.ObjectId, ref: 'User' }]
7 },{ 8 },{
8 collection: 'group' 9 collection: 'group'
9 }); 10 });
10 11
11 -GroupSchema.methods.addGroupMemeber=function(user){ 12 +GroupSchema.statics.findByGroupName=function(groupName){
13 + return this.find({groupName:groupName});
14 +}
15 +
16 +GroupSchema.methods.addGroupMember=function(user){
12 this.members.push(user._id); 17 this.members.push(user._id);
13 return this.save(); 18 return this.save();
14 } 19 }
...@@ -18,7 +23,9 @@ GroupSchema.methods.getMembers=function(){ ...@@ -18,7 +23,9 @@ GroupSchema.methods.getMembers=function(){
18 } 23 }
19 24
20 GroupSchema.methods.serialize=function(){ 25 GroupSchema.methods.serialize=function(){
21 - return this.toJSON(); 26 + let groupJSON=this.toJSON();
27 + delete groupJSON._id;
28 + return groupJSON;
22 } 29 }
23 30
24 const Group = mongoose.model('Group',GroupSchema); 31 const Group = mongoose.model('Group',GroupSchema);
......
...@@ -29,5 +29,11 @@ ParticipationSchema.methods.addProblem=function(problem){ ...@@ -29,5 +29,11 @@ ParticipationSchema.methods.addProblem=function(problem){
29 this.problems.push({problemNum:problem.problemNum,isSolved:problem.isSolved}); 29 this.problems.push({problemNum:problem.problemNum,isSolved:problem.isSolved});
30 } 30 }
31 31
32 +ParticipationSchema.methods.serialize=function(){
33 + let participationJSON=this.toJSON();
34 + delete participationJSON._id;
35 + return participationJSON;
36 +}
37 +
32 const Participation = mongoose.model('Participation', ParticipationSchema); 38 const Participation = mongoose.model('Participation', ParticipationSchema);
33 module.exports = Participation; 39 module.exports = Participation;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -58,7 +58,9 @@ ProblemSchema.methods.getCategory=function(){ ...@@ -58,7 +58,9 @@ ProblemSchema.methods.getCategory=function(){
58 } 58 }
59 59
60 ProblemSchema.methods.serialize=function(){ 60 ProblemSchema.methods.serialize=function(){
61 - return this.toJSON(); 61 + let problemJSON=this.toJSON();
62 + delete problemJSON._id;
63 + return problemJSON;
62 } 64 }
63 65
64 const Problem = mongoose.model('Problem',ProblemSchema); 66 const Problem = mongoose.model('Problem',ProblemSchema);
......
...@@ -28,7 +28,9 @@ SessionSchema.methods.getStatus=function(){ ...@@ -28,7 +28,9 @@ SessionSchema.methods.getStatus=function(){
28 } 28 }
29 29
30 SessionSchema.methods.serialize=function(){ 30 SessionSchema.methods.serialize=function(){
31 - return this.toJSON(); 31 + let sessionJSON=this.toJSON();
32 + delete sessionJSON._id;
33 + return sessionJSON;
32 } 34 }
33 35
34 const Session = mongoose.model('Session', SessionSchema); 36 const Session = mongoose.model('Session', SessionSchema);
......
...@@ -38,13 +38,14 @@ UserSchema.methods.checkPassword = async function (password) { ...@@ -38,13 +38,14 @@ UserSchema.methods.checkPassword = async function (password) {
38 UserSchema.methods.serialize = function () { 38 UserSchema.methods.serialize = function () {
39 const data = this.toJSON(); 39 const data = this.toJSON();
40 delete data.hashedPassword; 40 delete data.hashedPassword;
41 + delete data._id;
41 return data; 42 return data;
42 }; 43 };
43 44
44 UserSchema.methods.generateToken = function () { 45 UserSchema.methods.generateToken = function () {
45 const token = jwt.sign( 46 const token = jwt.sign(
46 { 47 {
47 - _id: this.id, 48 + _id: this._id,
48 username: this.username, 49 username: this.username,
49 }, 50 },
50 process.env.JWT_SECRET, 51 process.env.JWT_SECRET,
......