Update db model

parent d047e697
module.exports.OBJECT_STATUS = {
REMOVED: 0, // can be restored by teacher
ACTIVE: 1,
DELETED: 2, //can be watched and restore by admin
LOCKED: 3 //can not be edited
ACTIVE: 0,
LOCKED: 1, //can not be edited
REMOVED: 2, //can be restored by teacher
DELETED: 3 //can be watched and restore by admin
};
module.exports.COURSE_TYPES = {
......
......@@ -183,7 +183,7 @@ module.exports.add = task => {
module.exports.getActive = () => {
return Task.find({
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
......@@ -194,21 +194,21 @@ module.exports.get = id => {
module.exports.getMany = (ids) => {
return Task.find({
_id: {$in: ids},
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
module.exports.getManyShort = ids => {
return Task.find({
_id: {$in: ids},
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
}).select({name: 1, brief: 1, logo: 1});
};
module.exports.getForContest = contest => {
return Task.find({
contest,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
}).sort({index: 1});
};
......@@ -219,7 +219,7 @@ module.exports.getShortForContest = contest => {
module.exports.getCountForContest = contest => {
return Task.countDocuments({
contest,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
......@@ -227,14 +227,14 @@ module.exports.getCountForContestAndVariant = (contest, variant) => {
return Task.countDocuments({
contest,
variant,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
module.exports.getForContests = contests => {
return Task.find({
contest: {$in: contests},
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
......@@ -242,7 +242,7 @@ module.exports.aggregateForContests = contests => {
return Task.aggregate()
.match({
contest: {$in: contests},
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
})
.group({
_id: {contest: "$contest", variant: {$ifNull: ["$variant", 1]}},
......@@ -260,7 +260,7 @@ module.exports.aggregateWeightForContests = contests => {
.match({
contest: {$in: contests},
difficulty: defines.TASK_DIFFICULTY.USUAL,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
})
.group({
_id: "$contest",
......@@ -273,7 +273,7 @@ module.exports.aggregateWeightForContest = contest => {
.match({
contest,
difficulty: defines.TASK_DIFFICULTY.USUAL,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
})
.group({
_id: "$variant",
......@@ -287,7 +287,7 @@ module.exports.aggregateWeightForContestAndVariant = (contest, variant) => {
contest,
variant,
difficulty: defines.TASK_DIFFICULTY.USUAL,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
})
.group({
_id: "$variant",
......@@ -299,7 +299,7 @@ module.exports.getForVariantAndContest = (contest, variant, hard) => {
return Task.find({
contest,
variant,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
}).sort({index: 1});
};
......@@ -307,7 +307,7 @@ module.exports.getNearForVariantAndContestAndIndex = (contest, variant, index) =
return Task.find({
contest,
variant,
status: defines.OBJECT_STATUS.ACTIVE,
status: {$lte: defines.OBJECT_STATUS.LOCKED },
index: {$in: [index - 1, index + 1]}
}).select({_id: 1, index: 1, contest: 1, variant: 1, difficulty: 1}).sort({index: 1});
};
......@@ -501,15 +501,19 @@ module.exports.removeForContest = contest => {
};
module.exports.removeManyForContest = (contest, ids) => {
return Task.updateMany({_id: {$in: ids}, contest}, { status: defines.OBJECT_STATUS.REMOVED });
return Task.updateMany({_id: {$in: ids}, contest}, { status: defines.OBJECT_STATUS.REMOVED });
};
module.exports.activate = id => {
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ACTIVE});
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.LOCKED });
};
module.exports.lock = id => {
return Task.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.LOCKED });
};
module.exports.activateForContest = contest => {
return Task.updateMany({contest}, {status: defines.OBJECT_STATUS.ACTIVE});
return Task.updateMany({contest}, {status: defines.OBJECT_STATUS.ACTIVE });
};
module.exports.setStatusForContests = (contests, status) => {
......
......@@ -65,9 +65,15 @@ const TestSchema = new mongoose.Schema({
status: {
type: Number,
default: defines.OBJECT_STATUS.ACTIVE
},
isExample: {
type: Boolean,
default: false
}
});
TestSchema.index({ index: 1 });
const Test = mongoose.connection.model('Test', TestSchema);
module.exports.schema = TestSchema;
module.exports.model = Test;
......@@ -85,28 +91,44 @@ module.exports.getManyShort = ids => {
module.exports.getForContest = contest => {
return Test.find({
contest,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
}).sort({index: 1});
};
module.exports.getForTask = task => {
return Test.find({
task,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
}).sort({index: 1});
};
module.exports.getExamplesForTask = task => {
return Test.find({
task,
status: {$lte: defines.OBJECT_STATUS.LOCKED },
isExample: true
}).sort({index: 1});
};
module.exports.getExamplesForContest = contest => {
return Test.find({
contest,
status: {$lte: defines.OBJECT_STATUS.LOCKED },
isExample: true
}).sort({index: 1});
};
module.exports.getCountForContest = contest => {
return Test.countDocuments({
contest,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
module.exports.getCountForTask = task => {
return Test.countDocuments({
task,
status: defines.OBJECT_STATUS.ACTIVE
status: {$lte: defines.OBJECT_STATUS.LOCKED }
});
};
......@@ -126,6 +148,10 @@ module.exports.getRemoved = task => {
return Test.find({task, status: defines.OBJECT_STATUS.REMOVED})
};
module.exports.lockForTask = task => {
return Test.findOneAndUpdate({task}, {status: defines.OBJECT_STATUS.LOCKED });
};
module.exports.remove = id => {
return Test.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.REMOVED});
};
......@@ -135,7 +161,11 @@ module.exports.delete = id => {
};
module.exports.activate = id => {
return Test.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ACTIVE});
return Test.findOneAndUpdate({_id: id}, {status: defines.OBJECT_STATUS.ACTIVE });
};
module.exports.activateForTask = task => {
return Test.findOneAndUpdate({task}, {status: defines.OBJECT_STATUS.ACTIVE });
};
module.exports.erase = id => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment