From 0f3cbc928c22b6e31309e782a24c0334e68afba3 Mon Sep 17 00:00:00 2001 From: Shrirang Date: Fri, 25 Dec 2020 16:01:21 +0530 Subject: [PATCH 1/3] Added Validation to Add Contest Page --- controls/admin.js | 23 +++++------- public/js/new_contest.js | 79 ++++++++++++++++++++++++++++++++++++++++ views/new_contest.ejs | 20 +++++----- 3 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 public/js/new_contest.js diff --git a/controls/admin.js b/controls/admin.js index 697bbc81..29813dc1 100644 --- a/controls/admin.js +++ b/controls/admin.js @@ -138,17 +138,10 @@ helper.getQuestion = async (req, res, next) => { helper.createContest = async (req, res, next) => { /**Creating an object for new contest */ - var newContest = { - code: req.body.contestCode, - name: req.body.contestName, - date: req.body.date + " " + req.body.startTime, - endDate: 0, - duration: req.body.duration, - visible: req.body.visibility, - problemsID: req.body.problemsID.split(",").map(qID => qID.trim()) - }; + var newContest = req.body.newContest; + 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) => { @@ -162,15 +155,19 @@ helper.createContest = async (req, res, next) => { await contests.create(newContest) .then((val) => { console.log(val); + res.send(`Contest created successfully with code ${newContest.code}.`); }) .catch((err) => { console.log(err); + res.send("Error occurred while creating the contest."); }) - res.redirect("/admin/my-contests"); + + // res.redirect("/admin/my-contests"); } else { - console.log("Inside flag_contest = 1") - res.redirect("/admin/new-contest"); + console.log("Inside flag_contest = 1"); + res.send("The contest with same code already exists !, try different code"); + } } diff --git a/public/js/new_contest.js b/public/js/new_contest.js new file mode 100644 index 00000000..97998f50 --- /dev/null +++ b/public/js/new_contest.js @@ -0,0 +1,79 @@ +const settings = { + async: true, + crossDomain: true, + url: "/admin/new-contest", + method: "POST", + headers: { + "Content-Type": "application/json", + "cache-control": "no-cache", + }, + processData: false, + data: "_fill", +}; + + +$(".submit").on('click', function(){ + + //disable button + $('.submit').toggleClass('is-loading'); + + // creating contest object + const newContest = { + + code: $("#contestCode").val(), + name: $("#contestName").val(), + date: $("#date").val()+" "+$("#startTime").val(), + endDate:0, + duration: $("#duration").val(), + visible: $("#visibility").val(), + problemsID: $("#problemsID").val().split(",").map(qID => qID.trim()) + + }; + + // console.log(newContest); + + // Check whether all star marked fields have value or not + function check(data) { + if(data !== null && data !== '' && data !== undefined){ + return true; + } + return false; + } + + if(check(newContest.code) && check(newContest.name) && check(newContest.date) && check(newContest.duration) && check(newContest.visible)) { + // Works fine; + ; + } + else { + window.alert("All star marked fields must be non-empty!"); + //enable button + $('.submit').toggleClass('is-loading'); + return 0; + } + + if(newContest.visible !== 1 && newContest.visible!==0) { + window.alert("The value of visibility can be 1 or 0 only."); + //enable button + $('.submit').toggleClass('is-loading'); + return 0; + } + + + + //send data to server; + const data = { + newContest + }; + settings.data = JSON.stringify(data); + $.ajax(settings).done(function (response) { + //enable button + $('.submit').toggleClass('is-loading'); + + //show response + window.alert(response); + + //empty the input fields + $('input').val(''); + }); + +}); \ No newline at end of file diff --git a/views/new_contest.ejs b/views/new_contest.ejs index 00c0f605..0d3b5492 100644 --- a/views/new_contest.ejs +++ b/views/new_contest.ejs @@ -27,15 +27,15 @@ MANAGE ADMINS

New Contests


-
- Contest Code: *
- Contest Name: *
- Date: *
- Starting Time: *
- Duration (minutes): *
- Make the contest visible to the public? (1 for yes, 0 for no) *
- Problems to be included (qID "," comma separated):
- + + Contest Code: *
+ Contest Name: *
+ Date: *
+ Starting Time: *
+ Duration (minutes): *
+ Make the contest visible to the public? (1 for yes, 0 for no) *
+ Problems to be included (qID "," comma separated):
+
@@ -44,5 +44,5 @@ <% include partials/footer %> - + \ No newline at end of file From a251972af179eeb4e1083b4bc4576da16e39b33b Mon Sep 17 00:00:00 2001 From: Shrirang Date: Fri, 25 Dec 2020 16:25:55 +0530 Subject: [PATCH 2/3] Added Input Validation in Edit Contest --- controls/admin.js | 11 ++---- public/js/edit_contest.js | 76 +++++++++++++++++++++++++++++++++++++++ views/edit_contest.ejs | 20 +++++------ 3 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 public/js/edit_contest.js diff --git a/controls/admin.js b/controls/admin.js index 29813dc1..7f8aaef8 100644 --- a/controls/admin.js +++ b/controls/admin.js @@ -228,15 +228,8 @@ helper.displayEditContest = async (req, res, next) => { */ helper.editContest = async (req, res, next) => { /**Getting data from each fields in the edit contest form */ - var editContest = { - code: req.params.contCode, - name: req.body.contestName, - date: req.body.date + " " + req.body.startTime, - duration: req.body.duration, - visible: req.body.visibility, - /**comma separated qID of the problems to be included in the contest */ - problemsID: req.body.problemsID.split(",").map(qID => qID.trim()) - }; + var editContest = req.body.editContest; + editContest.code = req.params.contCode; editContest.endDate = moment(editContest.date).add(editContest.duration,'m').toDate(); await contests.update({ code: req.params.contCode }, editContest) diff --git a/public/js/edit_contest.js b/public/js/edit_contest.js new file mode 100644 index 00000000..2fbe37a8 --- /dev/null +++ b/public/js/edit_contest.js @@ -0,0 +1,76 @@ +const settings = { + async: true, + crossDomain: true, + url: window.location.href, + method: "POST", + headers: { + "Content-Type": "application/json", + "cache-control": "no-cache", + }, + processData: false, + data: "_fill", + }; + + + $(".submit").on('click', function(){ + + //disable button + $('.submit').toggleClass('is-loading'); + + // creating contest object + const editContest = { + + name: $("#contestName").val(), + date: $("#date").val()+" "+$("#startTime").val(), + endDate:0, + duration: $("#duration").val(), + visible: $("#visibility").val(), + problemsID: $("#problemsID").val().split(",").map(qID => qID.trim()) + + }; + + // console.log(newContest); + + // Check whether all star marked fields have value or not + function check(data) { + if(data !== null && data !== '' && data !== undefined){ + return true; + } + return false; + } + + if(check(editContest.name) && check(editContest.date) && check(editContest.duration) && check(editContest.visible)) { + // Works fine; + ; + } + else { + window.alert("All star marked fields must be non-empty!"); + //enable button + $('.submit').toggleClass('is-loading'); + return 0; + } + + if(editContest.visible !== '1' && editContest.visible!=='0') { + window.alert("The value of visibility can be 1 or 0 only."); + //enable button + $('.submit').toggleClass('is-loading'); + return 0; + } + + + + //send data to server; + const data = { + editContest + }; + settings.data = JSON.stringify(data); + $.ajax(settings).done(function (response) { + //enable button + $('.submit').toggleClass('is-loading'); + + //show response + window.alert(response); + + }); + + }); \ No newline at end of file diff --git a/views/edit_contest.ejs b/views/edit_contest.ejs index c80b7da4..b03c296e 100644 --- a/views/edit_contest.ejs +++ b/views/edit_contest.ejs @@ -20,22 +20,22 @@ <% include partials/header %>
-
+

EDIT CONTEST


- Contest Code: <%= data.code %>
- Contest Name:
- Date:
- Starting Time:
- Duration (minutes):
- Make the contest visible to the public? (1 for yes, 0 for no)
- Problems to be included (qID "," comma separated):
- - + Contest Code: <%= data.code %>
+ Contest Name:
+ Date:
+ Starting Time:
+ Duration (minutes):
+ Make the contest visible to the public? (1 for yes, 0 for no)
+ Problems to be included (qID "," comma separated):
+
<% include partials/footer %>
+ \ No newline at end of file From c78d134ed69164e7d0d38c4904326ad1fa97163d Mon Sep 17 00:00:00 2001 From: Shrirang Date: Fri, 25 Dec 2020 16:29:29 +0530 Subject: [PATCH 3/3] Added Astericks to Required fields in Edit Contest --- views/edit_contest.ejs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/views/edit_contest.ejs b/views/edit_contest.ejs index b03c296e..965985b7 100644 --- a/views/edit_contest.ejs +++ b/views/edit_contest.ejs @@ -23,11 +23,11 @@

EDIT CONTEST


Contest Code: <%= data.code %>
- Contest Name:
- Date:
- Starting Time:
- Duration (minutes):
- Make the contest visible to the public? (1 for yes, 0 for no)
+ Contest Name: *
+ Date: *
+ Starting Time: *
+ Duration (minutes): *
+ Make the contest visible to the public? (1 for yes, 0 for no) *
Problems to be included (qID "," comma separated):