Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions controls/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,9 @@ var moment = require("moment");
*/
helper.displayAllProblems = async (req, res, next) => {
/**Finding all the problems sorted in descending order of the qID */
Question.find({}).sort({ qID: -1 })
.then(async (data) => {
var contestsData= await contests.find({});
console.log(contestsData);
for(var i = 0; i < data.length; i++) {

var contestsList = [];
for(var j = 0; j <contestsData.length; j++) {
if (contestsData[j].problemsID.includes(String(data[i].qID)))
contestsList.push(contestsData[j].code);
}
data[i].contests = contestsList;
}


Question.find({}).populate('contests','code').sort({ qID: -1 })
.then( (data) => {
res.render("admin", { data: data });
})
.catch((err) => {
Expand Down Expand Up @@ -148,7 +137,7 @@ helper.createContest = async (req, res, next) => {
problemsID: req.body.problemsID.split(",").map(qID => qID.trim())
};
newContest.endDate = moment(newContest.date).add(newContest.duration,'m').toDate();
console.log(newContest)
console.log(newContest);
var flag_contest = 0;
await contests.findOne({"code": newContest.code})
.then((data) => {
Expand All @@ -160,8 +149,27 @@ helper.createContest = async (req, res, next) => {

if(flag_contest == 0){
await contests.create(newContest)
.then((val) => {
.then(async (val) => {

console.log(val);

for(var i = 0; i < val.problemsID.length; i++) {

const refProb= await Question.findOne({qID:Number(val.problemsID[i])});
console.log(refProb);
if (!refProb) {
console.log(`No problem found with ID ${val.problemsID[i]}`);
}
else {

val.problems.push(refProb);
refProb.contests.push(val);
await refProb.save();
}

}
await val.save();

})
.catch((err) => {
console.log(err);
Expand Down
15 changes: 8 additions & 7 deletions models/contests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var mongoose = require('mongoose');

var contests = new mongoose.Schema({
code: String,
name: String,
date: Date,
endDate: Date,
duration: Number,
visible: Boolean,
problemsID: [String]
code: String,
name: String,
date: Date,
endDate: Date,
duration: Number,
visible: Boolean,
problemsID: [String],
problems:[{type: mongoose.Schema.ObjectId, ref: "Problems"}]
});

var Contests = mongoose.model('Contests', contests);
Expand Down
1 change: 1 addition & 0 deletions models/problems.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var prob = new mongoose.Schema({
memoryLimit: Number,
tags: [String],
editorial: String,
contests:[{type: mongoose.Schema.ObjectId, ref: "Contests"}]
});
var Problems = mongoose.model('Problems', prob);
module.exports = Problems;
4 changes: 2 additions & 2 deletions views/admin.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<% } %>

<% for(var j = 0; j < data[i].contests.length; j++) { %>
<a href="/contests/<%= data[i].contests[j]%>">
<%= data[i].contests[j] %>,
<a href="/contests/<%= data[i].contests[j].code%>">
<%= data[i].contests[j].code %>,
</a>
<% } %>
</td>
Expand Down