From b1dfb985f791ae9d7eaa6b4c69662aaedb07cd82 Mon Sep 17 00:00:00 2001 From: Shrirang Date: Fri, 11 Dec 2020 09:26:08 +0530 Subject: [PATCH 1/2] Contest Column Added --- controls/admin.js | 14 +++++++++++++- views/admin.ejs | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/controls/admin.js b/controls/admin.js index 51a7e2fc..697bbc81 100644 --- a/controls/admin.js +++ b/controls/admin.js @@ -12,7 +12,19 @@ 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((data) => { + .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 { diff --git a/views/admin.ejs b/views/admin.ejs index 747dc26b..cdb7af83 100644 --- a/views/admin.ejs +++ b/views/admin.ejs @@ -38,6 +38,7 @@ Id Name + Contests Visible publicly @@ -51,6 +52,17 @@ <%= data[i].name %> + + <% if (data[i].contests.length===0) { %> + No Contests Found + <% } %> + + <% for(var j = 0; j < data[i].contests.length; j++) { %> + + <%= data[i].contests[j] %>, + + <% } %> + <%= data[i].isVisible %> From a31c6deabbf77d7ed8673343caa1c7187c2f1be5 Mon Sep 17 00:00:00 2001 From: Shrirang Date: Wed, 23 Dec 2020 10:28:31 +0530 Subject: [PATCH 2/2] Added Many-Many Relation --- controls/admin.js | 39 +++++++++++++++++++++++---------------- models/contests.js | 15 ++++++++------- models/problems.js | 1 + views/admin.ejs | 4 ++-- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/controls/admin.js b/controls/admin.js index 697bbc81..ed44ec8b 100644 --- a/controls/admin.js +++ b/controls/admin.js @@ -11,20 +11,8 @@ 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 +136,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 +148,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 %>, <% } %>