Skip to content

Commit

Permalink
contest-questions APIs (#84)
Browse files Browse the repository at this point in the history
* 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
3 people authored Dec 27, 2020
1 parent a9ff039 commit 9038986
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 6 deletions.
39 changes: 39 additions & 0 deletions server/models/contests/addQuestion.js
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
44 changes: 44 additions & 0 deletions server/models/contests/getAllQuestions.js
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
45 changes: 45 additions & 0 deletions server/models/contests/getQuestion.js
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
8 changes: 8 additions & 0 deletions server/models/contests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const getContestModerators = require('./getContestModerators')
const participate = require('./participate')
const getAllParticipants = require('./getAllParticipants')
const getAllParticipantsDetails = require('./getAllParticipantsDetails')
const addQuestion = require('./addQuestion')
const removeQuestion = require('./removeQuestion')
const getAllQuestions = require('./getAllQuestions')
const getQuestion = require('./getQuestion')

module.exports = {
createContest,
Expand All @@ -28,4 +32,8 @@ module.exports = {
participate,
getAllParticipants,
getAllParticipantsDetails,
addQuestion,
removeQuestion,
getQuestion,
getAllQuestions,
}
35 changes: 35 additions & 0 deletions server/models/contests/removeQuestion.js
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
50 changes: 50 additions & 0 deletions server/routes/contests/addQuestion.js
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
29 changes: 29 additions & 0 deletions server/routes/contests/getAllQuestions.js
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
29 changes: 29 additions & 0 deletions server/routes/contests/getQuestion.js
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
8 changes: 8 additions & 0 deletions server/routes/contests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const getContestModeratorsRouter = require('./getContestModerators')
const participateRouter = require('./participate')
const getAllParticipantsRouter = require('./getAllParticipants')
const getAllParticipantsDetailsRouter = require('./getAllParticipantsDetails')
const addQuestionRouter = require('./addQuestion')
const removeQuestionRouter = require('./removeQuestion')
const getAllQuestionsRouter = require('./getAllQuestions')
const getQuestionRouter = require('./getQuestion')

module.exports = {
createContestRouter,
Expand All @@ -28,4 +32,8 @@ module.exports = {
participateRouter,
getAllParticipantsRouter,
getAllParticipantsDetailsRouter,
addQuestionRouter,
removeQuestionRouter,
getQuestionRouter,
getAllQuestionsRouter,
}
50 changes: 50 additions & 0 deletions server/routes/contests/removeQuestion.js
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
10 changes: 4 additions & 6 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ router.use('/contests', contestsRouter.getContestModeratorsRouter)
router.use('/contests', contestsRouter.participateRouter)
router.use('/contests', contestsRouter.getAllParticipantsRouter)
router.use('/contests', contestsRouter.getAllParticipantsDetailsRouter)
// router.use('/contests', contestsRouter.addQuestionRouter)
// router.use('/contests', contestsRouter.removeQuestionRouter)
// router.use('/contests', contestsRouter.getAllQuestionsRouter)
// router.use('/contests', contestsRouter.getQuestionRouter)
// router.use('/contests', contestsRouter.getLeaderboardRouter)
// router.use('/contests', contestsRouter.getQuestionLeaderboardRouter)
router.use('/contests', contestsRouter.addQuestionRouter)
router.use('/contests', contestsRouter.removeQuestionRouter)
router.use('/contests', contestsRouter.getAllQuestionsRouter)
router.use('/contests', contestsRouter.getQuestionRouter)

router.use('/questions', questionsRouter.getEditorQuestions)
router.use('/questions', questionsRouter.updateQuestion)
Expand Down
Loading

0 comments on commit 9038986

Please sign in to comment.