-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* contest-questions APIs * Update server/models/contests/getAllQuestions.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/getAllQuestions.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/removeQuestion.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/getQuestion.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/getAllQuestions.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/getAllQuestions.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/getQuestion.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/getQuestion.js Co-authored-by: Chirag Jain <[email protected]> * Update server/models/contests/removeQuestion.js Co-authored-by: NIKHIL BN <[email protected]> * Update server/models/contests/removeQuestion.js Co-authored-by: NIKHIL BN <[email protected]> * Update server/models/contests/removeQuestion.js Co-authored-by: NIKHIL BN <[email protected]> Co-authored-by: Chirag Jain <[email protected]> Co-authored-by: NIKHIL BN <[email protected]>
- Loading branch information
1 parent
a9ff039
commit 9038986
Showing
14 changed files
with
388 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const { pool } = require('../database') | ||
|
||
/** | ||
* @param {*} param0 | ||
* @param {Object} param0.body | ||
* @param {Object} param0.params | ||
* @param {String} param0.username | ||
* @return {Promise} | ||
*/ | ||
|
||
function addQuestion({ params, body, username }) { | ||
return new Promise((resolve, reject) => { | ||
const { contest_id: contestId } = params | ||
const { question_id: questionId, max_score: maxScore } = body | ||
pool.query( | ||
`INSERT INTO contests_questions (contest_id, question_id, max_score) SELECT contest_id, ?, ? FROM contests_moderators WHERE contest_id=? AND moderator=?`, | ||
[questionId, maxScore, contestId, username], | ||
(error, res) => { | ||
if (error || res === undefined) { | ||
const { code } = error | ||
if (code === 'ER_DUP_ENTRY') { | ||
return reject('The question is already added to the contest') | ||
} | ||
return reject(error) | ||
} else { | ||
const { affectedRows } = res | ||
if (!affectedRows) { | ||
return reject( | ||
'Invalid question Id or the user do not have required permissions' | ||
) | ||
} | ||
return resolve('Question added successfully!!') | ||
} | ||
} | ||
) | ||
}) | ||
} | ||
|
||
module.exports = addQuestion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const { pool } = require('../../models/database') | ||
|
||
/** | ||
* @param {*} param0 | ||
* @param {String} param0.username | ||
* @param {Object} param0.params | ||
* @return {Promise} | ||
*/ | ||
|
||
function getAllQuestions({ username, params }) { | ||
return new Promise((resolve, reject) => { | ||
const { contest_id: contestId } = params | ||
pool.query( | ||
`SELECT question_id, q.name, q.difficulty, q.type, max_score FROM contests_questions cq | ||
INNER JOIN contests c ON c.id = cq.contest_id | ||
INNER JOIN questions q ON cq.question_id=q.id | ||
WHERE c.id=? AND (EXISTS(SELECT 1 from contests_moderators WHERE contest_id=? AND moderator=?) OR (NOW()>c.end_time AND c.confidential_questions=0 AND (c.public = 1 | ||
OR EXISTS(SELECT 1 FROM contests_groups cg INNER JOIN user_groups ug ON cg.group_id = ug.group_id | ||
WHERE ug.username = ? AND cg.contest_id = ?))) OR (NOW()>=c.start_time AND NOW()<=c.end_time AND EXISTS(SELECT 1 FROM contests_participants WHERE contest_id=? AND participant=?)))`, | ||
[ | ||
contestId, | ||
contestId, | ||
username, | ||
username, | ||
contestId, | ||
contestId, | ||
username, | ||
], | ||
(error, results) => { | ||
if (error || results === undefined) { | ||
return reject(error) | ||
} | ||
if (!results || !results.length) { | ||
return reject( | ||
'You do not have moderator access of the contest. If the contest is active, make sure that you are registered as a participant.' | ||
) | ||
} | ||
return resolve(results) | ||
} | ||
) | ||
}) | ||
} | ||
|
||
module.exports = getAllQuestions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { pool } = require('../../models/database') | ||
|
||
/** | ||
* @param {*} param0 | ||
* @param {String} param0.username | ||
* @param {Object} param0.params | ||
* @return {Promise} | ||
*/ | ||
|
||
function getQuestion({ username, params }) { | ||
return new Promise((resolve, reject) => { | ||
const { contest_id: contestId, question_id: questionId } = params | ||
pool.query( | ||
`SELECT q.name, q.difficulty, q.type, max_score, q.creator, q.problem_statement, q.input_format, q.output_format, q.constraints, q.options FROM contests_questions cq | ||
INNER JOIN contests c ON c.id = cq.contest_id | ||
INNER JOIN questions q ON cq.question_id=q.id | ||
WHERE (c.id=? AND q.id=?) AND (EXISTS(SELECT 1 from contests_moderators WHERE contest_id=? AND moderator=?) OR (NOW()>c.end_time AND c.confidential_questions=0 AND (c.public = 1 | ||
OR EXISTS(SELECT 1 FROM contests_groups cg INNER JOIN user_groups ug ON cg.group_id = ug.group_id | ||
WHERE ug.username = ? AND cg.contest_id = ?))) OR (NOW()>=c.start_time AND NOW()<=c.end_time AND EXISTS(SELECT 1 FROM contests_participants WHERE contest_id=? AND participant=?)))`, | ||
[ | ||
contestId, | ||
questionId, | ||
contestId, | ||
username, | ||
username, | ||
contestId, | ||
contestId, | ||
username, | ||
], | ||
(error, results) => { | ||
if (error || results === undefined) { | ||
return reject(error) | ||
} | ||
if (!results || !results.length) { | ||
return reject( | ||
'You do not have moderator access of the contest. If the contest is active, make sure that you are registered as a participant.' | ||
) | ||
} | ||
return resolve(results) | ||
} | ||
) | ||
}) | ||
} | ||
|
||
module.exports = getQuestion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { pool } = require('../database') | ||
|
||
/** | ||
* @param {*} param0 | ||
* @param {Object} param0.body | ||
* @param {Object} param0.params | ||
* @param {String} param0.username | ||
* @return {Promise} | ||
*/ | ||
|
||
function removeQuestion({ params, body, username }) { | ||
return new Promise((resolve, reject) => { | ||
const { contest_id: contestId } = params | ||
const { question_id: questionId } = body | ||
pool.query( | ||
`DELETE FROM contests_questions WHERE EXISTS(SELECT 1 FROM contests_moderators WHERE contest_id=? AND moderator=?) AND question_id = ? AND contest_id = ?`, | ||
[contestId, username, questionId, contestId], | ||
(error, res) => { | ||
if (error || res === undefined) { | ||
return reject(error) | ||
} else { | ||
const { affectedRows } = res | ||
if (!affectedRows) { | ||
return reject( | ||
'Invalid question Id or you do not have moderator access to the contest' | ||
) | ||
} | ||
return resolve('Question removed successfully!!') | ||
} | ||
} | ||
) | ||
}) | ||
} | ||
|
||
module.exports = removeQuestion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const ajv = require('../../schema') | ||
const contests = require('../../models/contests') | ||
const { addQuestionSchema } = require('../../schema/contests') | ||
const middleware = require('../middlewares') | ||
|
||
/** | ||
* | ||
* @param {Array} errArray | ||
* @return {String} | ||
*/ | ||
function sumErrors(errArray) { | ||
const cb = (a, b) => a + b.message + ', ' | ||
return errArray.reduce(cb, '') | ||
} | ||
|
||
router.post( | ||
'/:contest_id/questions', | ||
middleware.verifyUserAccessToken, | ||
async (request, response) => { | ||
const validate = ajv.compile(addQuestionSchema) | ||
const isValid = validate(request.body) | ||
if (!isValid) { | ||
return response.status(400).json({ | ||
success: false, | ||
error: sumErrors(validate.errors), | ||
results: null, | ||
}) | ||
} | ||
contests | ||
.addQuestion(request) | ||
.then((results) => { | ||
return response.status(200).json({ | ||
success: true, | ||
error: null, | ||
results, | ||
}) | ||
}) | ||
.catch((error) => { | ||
return response.status(400).json({ | ||
success: false, | ||
error, | ||
results: null, | ||
}) | ||
}) | ||
} | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const contests = require('../../models/contests') | ||
const middleware = require('../middlewares') | ||
|
||
router.get( | ||
'/:contest_id/questions', | ||
middleware.verifyUserAccessToken, | ||
async (request, response) => { | ||
contests | ||
.getAllQuestions(request) | ||
.then((results) => { | ||
return response.status(200).json({ | ||
success: true, | ||
error: null, | ||
results, | ||
}) | ||
}) | ||
.catch((error) => { | ||
return response.status(400).json({ | ||
success: false, | ||
error, | ||
results: null, | ||
}) | ||
}) | ||
} | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const contests = require('../../models/contests') | ||
const middleware = require('../middlewares') | ||
|
||
router.get( | ||
'/:contest_id/questions/:question_id', | ||
middleware.verifyUserAccessToken, | ||
async (request, response) => { | ||
contests | ||
.getQuestion(request) | ||
.then((results) => { | ||
return response.status(200).json({ | ||
success: true, | ||
error: null, | ||
results, | ||
}) | ||
}) | ||
.catch((error) => { | ||
return response.status(400).json({ | ||
success: false, | ||
error, | ||
results: null, | ||
}) | ||
}) | ||
} | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const ajv = require('../../schema') | ||
const contests = require('../../models/contests') | ||
const { removeQuestionSchema } = require('../../schema/contests') | ||
const middleware = require('../middlewares') | ||
|
||
/** | ||
* | ||
* @param {Array} errArray | ||
* @return {String} | ||
*/ | ||
function sumErrors(errArray) { | ||
const cb = (a, b) => a + b.message + ', ' | ||
return errArray.reduce(cb, '') | ||
} | ||
|
||
router.delete( | ||
'/:contest_id/questions', | ||
middleware.verifyUserAccessToken, | ||
async (request, response) => { | ||
const validate = ajv.compile(removeQuestionSchema) | ||
const isValid = validate(request.body) | ||
if (!isValid) { | ||
return response.status(400).json({ | ||
success: false, | ||
error: sumErrors(validate.errors), | ||
results: null, | ||
}) | ||
} | ||
contests | ||
.removeQuestion(request) | ||
.then((results) => { | ||
return response.status(200).json({ | ||
success: true, | ||
error: null, | ||
results, | ||
}) | ||
}) | ||
.catch((error) => { | ||
return response.status(400).json({ | ||
success: false, | ||
error, | ||
results: null, | ||
}) | ||
}) | ||
} | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.