diff --git a/controls/admin.js b/controls/admin.js index 697bbc81..e1f7f0fb 100644 --- a/controls/admin.js +++ b/controls/admin.js @@ -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 { res.render("admin", { data: data }); }) .catch((err) => { @@ -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) => { @@ -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); diff --git a/models/contests.js b/models/contests.js index 6d315d01..cc42e755 100644 --- a/models/contests.js +++ b/models/contests.js @@ -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); diff --git a/models/problems.js b/models/problems.js index e93fda2f..97964772 100644 --- a/models/problems.js +++ b/models/problems.js @@ -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; diff --git a/views/admin.ejs b/views/admin.ejs index cdb7af83..8cf05847 100644 --- a/views/admin.ejs +++ b/views/admin.ejs @@ -58,8 +58,8 @@ <% } %> <% for(var j = 0; j < data[i].contests.length; j++) { %> - - <%= data[i].contests[j] %>, + + <%= data[i].contests[j].code %>, <% } %>