problem.js
1.53 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const mongoose=require('mongoose');
const {Schema}=mongoose;
const ProblemSchema=new Schema({
problemNum: {type: Number, required: true, unique: true},
problemTitle: {type: String, required: true},
solvedacLevel: {type: Number},
sumbitNum: {type: Number, required: true},
correctNum: {type: Number, required: true},
count: { type: Number },
category: [{ type:String }],
},{
collection: 'problem'
});
ProblemSchema.statics.findByProblemNum=function(problemNum){
return this.findOne({problemNum:problemNum});
}
ProblemSchema.methods.addCategory=function(category){
this.category.push(category);
return this.save();
}
ProblemSchema.methods.removeCategory=function(category){
const idx=this.category.findIndex(item=>item===category);
this.splice(idx,1);
return this.save();
}
ProblemSchema.methods.getProblemNum=function(){
return this.problemNum;
}
ProblemSchema.methods.getProblemTitle=function(){
return this.problemTitle;
}
ProblemSchema.methods.getSolvedacLevel=function(){
return this.solvedacLevel;
}
ProblemSchema.methods.getSumbitNum=function(){
return this.sumbitNum;
}
ProblemSchema.methods.getCorrectNum=function(){
return this.correctNum;
}
ProblemSchema.methods.getCount=function(){
return this.count;
}
ProblemSchema.methods.getCategory=function(){
return this.category;
}
ProblemSchema.methods.serialize=function(){
let problemJSON=this.toJSON();
return problemJSON;
}
const Problem = mongoose.model('Problem',ProblemSchema);
module.exports = Problem;