JJuOn

Implement models/problem.js

1 +const mongoose=require('mongoose');
2 +
3 +const {Schema}=mongoose;
4 +
5 +const ProblemSchema=new Schema({
6 + problemNum: {type: Number, required: true, unique: true},
7 + problemTitle: {type: String, required: true},
8 + solvedacLevel: {type: Number},
9 + sumbitNum: {type: Number, required: true},
10 + correctNum: {type: Number, required: true},
11 + category: {type:[String]}
12 +});
13 +
14 +ProblemSchema.statics.findByProblemNum=function(problemNum){
15 + return this.findOne({problemNum:problemNum});
16 +}
17 +
18 +ProblemSchema.methods.addCategory=function(category){
19 + this.category.push(category);
20 + return this.save();
21 +}
22 +
23 +ProblemSchema.methods.removeCategory=function(category){
24 + const idx=this.category.findIndex(item=>item===category);
25 + this.splice(idx,1);
26 + return this.save();
27 +}
28 +
29 +ProblemSchema.methods.getProblemNum=function(){
30 + return this.problemNum;
31 +}
32 +
33 +ProblemSchema.methods.getProblemTitle=function(){
34 + return this.problemTitle;
35 +}
36 +
37 +ProblemSchema.methods.getSolvedacLevel=function(){
38 + return this.solvedacLevel;
39 +}
40 +
41 +ProblemSchema.methods.getSumbitNum=function(){
42 + return this.sumbitNum;
43 +}
44 +
45 +ProblemSchema.methods.getCorrectNum=function(){
46 + return this.correctNum;
47 +}
48 +
49 +ProblemSchema.methods.getCategory=function(){
50 + return this.category;
51 +}
52 +
53 +ProblemSchema.methods.serialize=function(){
54 + return this.toJSON();
55 +}
56 +
57 +const Problem=mongoose.model('Problem',ProblemSchema);
58 +module.exports=Problem;
...\ No newline at end of file ...\ No newline at end of file