Skip to content

Commit

Permalink
edit contest's info (except startTime and endTime)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkhoi committed Jun 22, 2017
1 parent c6365d2 commit 67d6dc5
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 63 deletions.
64 changes: 56 additions & 8 deletions api/contest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const express = require('express');
const router = express.Router();
const path = require('path');
const multer = require('multer');
const ensureAdmin = require('../helpers/ensure-admin');
const Contests = require('../helpers/contests');
const dateTimeCvt = require('../helpers/datetime-converter');
const testDir = 'data/contests/tests';
const express = require('express');
const router = express.Router();
const path = require('path');
const multer = require('multer');
const ensureAdmin = require('../helpers/ensure-admin');
const ensureAuthorized = require('../helpers/ensure-authorized');
const Contests = require('../helpers/contests');
const dateTimeCvt = require('../helpers/datetime-converter');
const testDir = 'data/contests/tests';
const moment = require('moment');

// Specify directory to store problem statements of contests.
var storage = multer.diskStorage({
Expand Down Expand Up @@ -34,6 +36,7 @@ router.post('/create', [ensureAdmin, upload], function(req, res) {
filePath: path.join('data/contests', req.file.filename),
problemNames: req.body.problemNames
};
// console.log(newContest);

let id = null;
Contests.addContest(newContest).then(function successCallback(contestId) {
Expand All @@ -49,6 +52,8 @@ router.post('/create', [ensureAdmin, upload], function(req, res) {
}
res.send({ status: 'SUCCESS', id: id });
});
}, function errorCallback(err) {
res.send({ status: 'FAILED' });
});
});

Expand Down Expand Up @@ -104,4 +109,47 @@ router.get('/all', [], function(req, res) {
});
});

// Name: Get pending contest.
// Type: GET.
router.get('/pendingContest', [ensureAdmin], function(req, res) {
Contests.getPendingContest().then(function successCallback(contest) {
res.send({ contest: contest });
}, function errorCallback(err) {
res.status(500).send(err.toString());
});
});

// Name: Edit contest's info.
// Type: POST.
router.post('/edit', [ensureAdmin], function(req, res) {
let contest = {
id: req.body.id,
name: req.body.name,
topic: req.body.topic,
startTime: req.body.startTime,
endTime: req.body.endTime,
duration: dateTimeCvt.toDuration(req.body.startTime, req.body.endTime),
problemNames: req.body.problemNames
};
Contests.editContest(contest).then(function successCallback(result) {
res.send({ status: "SUCCESS "});
}, function errorCallback(err) {
res.status(500).send(err.toString());
});
});

// Name: Edit problem file.
// Type: POST.
router.post('/editProblemFile', [ensureAdmin, upload], function(req, res) {
let contest = {
id: req.body.id,
filePath: path.join('data/contests', req.file.filename)
};
Contests.editContestProblemFile(contest).then(function successCallback(result) {
res.send({ status: "SUCCESS", filePath: contest.filePath });
}, function errorCallback(err) {
res.status(500).send(err.toString());
});
});

module.exports = router;
2 changes: 1 addition & 1 deletion api/get-problems.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Contests = require('../helpers/contests');

// Name: Get problems' names.
// Type: POST.
router.post('/', [], function(req, res) {
router.post('/', [ensureAuthorized], function(req, res) {
let id = req.body.id;

if (id) {
Expand Down
156 changes: 139 additions & 17 deletions helpers/contests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const DataStore = require('nedb');
const Contests = new DataStore({ filename: path.join(process.cwd(), 'data/contests/', 'contests.db'),autoload: true });
const Contests = new DataStore({ filename: path.join(process.cwd(), 'data/contests/', 'contests.db'), autoload: true });
const config = require('../config');
const exec = require('child_process').exec;
const schedule = require('node-schedule');
Expand All @@ -26,27 +26,45 @@ const redisClient = redis.createClient();

const testDir = 'data/contests/tests';

// Function to insert a new problem into the database.
// Function to create a new contest into the database.
function addContest(newContest) {
return new Promise(function(resolve, reject) {
Contests.insert({
setter: newContest.setter,
name: newContest.name,
topic: newContest.topic,
startTime: newContest.startTime,
endTime: newContest.endTime,
duration: newContest.duration,
filePath: newContest.filePath,
problemNames: newContest.problemNames
}, function(err, contest) {
if (err) {
reject(Error("Could not add new contest"));
getAllContests().then(function successCallback(allContests) {
let ok = true;
for (let i = 0; i < allContests.length; i += 1) {
if (moment(allContests[i].endTime, "HH:mm, DD/MM/YYYY") >= moment()) {
ok = false;
break;
}
}
if (ok) {
Contests.insert({
setter: newContest.setter,
name: newContest.name,
topic: newContest.topic,
startTime: newContest.startTime,
endTime: newContest.endTime,
duration: newContest.duration,
filePath: newContest.filePath,
problemNames: newContest.problemNames
}, function(err, contest) {
if (err) {
reject(Error("Could not add new contest"));
}
else {
console.log('added', contest);
redisClient.set("pendingContest", contest._id);
resolve(contest._id);
}
});
}
else {
console.log('added', contest);
resolve(contest._id);
reject(Error("There's already a contest going to occur"));
}
}, function errorCallback(err) {
reject(Error(err.toString()));
});

});
}

Expand Down Expand Up @@ -76,6 +94,9 @@ function getAllContests() {
}
res.push(elem);
});
res.sort(function(a, b) {
return moment(a.startTime, "HH:mm, DD/MM/YYYY") < moment(b.startTime, "HH:mm, DD/MM/YYYY");
});
resolve(res);
}
});
Expand Down Expand Up @@ -190,6 +211,8 @@ function scheduleContestEnd(t) {
redisClient.del("contest", function(err, reply) {
console.log('delete contest', reply);
});
redisClient.del("pendingContest", function(err, reply) {
});
// Only stop contest 5 minutes after actual endTime. This is because
// there might be some submissions that were submitted near the end
// of the contest and have not been graded.
Expand Down Expand Up @@ -291,6 +314,101 @@ function getArchivedScoreboard(contestId) {
});
}

// Function to get the pending contest's id.
function getPendingContestId() {
return new Promise(function(resolve, reject) {
redisClient.get("pendingContest", function(err, reply) {
if (err) {
reject(Error("Unable to get pending contest's id"));
}
else if (reply) {
resolve(reply);
}
else {
resolve(-1);
}
});
});
}

// Function to get the pending contest.
function getPendingContest() {
return new Promise(function(resolve, reject) {
getPendingContestId().then(function successCallback(id) {
Contests.findOne({ _id: id }, function(err, contest) {
if (err) {
reject(Error("Could not retrieve contest with id"));
}
else if (!contest) {
resolve(-1);
}
else {
resolve(contest);
}
});
}, function errorCallback(err) {
reject(Error(err.toString()));
});
});
}

// Function to edit the pending contest.
function editContest(contest) {
return new Promise(function(resolve, reject) {
getContest(contest.id).then(function successCallback(_contest) {
if (!_contest) {
reject(Error("Could not find this contest in database"));
}
else {
Contests.update({ _id: contest.id }, {
$set: {
name: contest.name,
topic: contest.topic,
problemNames: contest.problemNames
}
}, {}, function(err, numAffected) {
if (err) {
reject(Error("Could not update contest's info"));
}
else {
resolve();
}
});
}
}, function errorCallback(err) {
reject(Error(err.toString()));
});
});
}

// Function to edit contest's problem file.
function editContestProblemFile(contest) {
return new Promise(function(resolve, reject) {
getContest(contest.id).then(function successCallback(_contest) {
if (!_contest) {
reject(Error("Could not find this contest in database"));
}
else {
Contests.update({ _id: contest.id }, {
$set: {
filePath: contest.filePath
}
}, {}, function(err, numAffected) {
if (err) {
reject(Error("Could not update contest's problem file"));
}
else {
resolve();
}
});
}
}, function errorCallback(err) {
reject(Error(err.toString()));
});
});
}


module.exports = {
addContest: addContest,
getAllContests: getAllContests,
Expand All @@ -303,5 +421,9 @@ module.exports = {
getCurrentContestId: getCurrentContestId,
getContestProblemNames: getContestProblemNames,
getCurrentContestScoreboard: getCurrentContestScoreboard,
getArchivedScoreboard: getArchivedScoreboard
getArchivedScoreboard: getArchivedScoreboard,
getPendingContest: getPendingContest,
getPendingContestId: getPendingContestId,
editContest: editContest,
editContestProblemFile: editContestProblemFile
};
Loading

0 comments on commit 67d6dc5

Please sign in to comment.