-
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.
- Loading branch information
1 parent
31d58c2
commit 79a0247
Showing
7 changed files
with
183 additions
and
3 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,122 @@ | ||
const { pool } = require('../database') | ||
|
||
/** | ||
* | ||
* @param {*} param0 | ||
* @param {String} param0.username | ||
* @param {String} param0.name | ||
* @param {String} param0.problem_statement | ||
* @param {String} param0.type | ||
* @param {String} param0.input_format | ||
* @param {String} param0.output_format | ||
* @param {String} param0.constraints | ||
* @param {Array} param0.options | ||
* @param {Number} param0.correct | ||
* @param {String} param0.difficulty | ||
* @param {Array} param0.tags | ||
* @return {Promise} | ||
*/ | ||
|
||
function createQuestion({ | ||
username, | ||
name, | ||
problem_statement: problemStatement, | ||
type, | ||
input_format: inputFormat, | ||
output_format: outputFormat, | ||
constraints, | ||
options, | ||
correct, | ||
difficulty, | ||
tags, | ||
}) { | ||
return new Promise((resolve, reject) => { | ||
pool.getConnection((err, connection) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
connection.beginTransaction((err) => { | ||
if (err) { | ||
connection.release() | ||
return reject(err) | ||
} | ||
connection.query( | ||
`INSERT INTO questions(creator,type,name,problem_statement,input_format, | ||
output_format,constraints,options,correct,difficulty) | ||
VALUES(?,?,?,?,?,?,?,?,?,?)`, | ||
[ | ||
username, | ||
type, | ||
name, | ||
problemStatement, | ||
inputFormat, | ||
outputFormat, | ||
constraints, | ||
JSON.stringify(options), | ||
correct, | ||
difficulty, | ||
], | ||
(error, results) => { | ||
if (error || !results) { | ||
connection.release() | ||
return reject(error) | ||
} | ||
|
||
const questionId = results.insertId | ||
|
||
if (!tags || !tags.length) { | ||
return connection.commit((error) => { | ||
if (error) { | ||
return connection.rollback(() => { | ||
connection.release() | ||
return reject(error) | ||
}) | ||
} | ||
connection.release() | ||
return resolve({ | ||
message: 'Question created successfully', | ||
questionId, | ||
}) | ||
}) | ||
} | ||
|
||
let query = `INSERT INTO questions_tags(question_id, tag_id) VALUES` | ||
const queryArr = [] | ||
|
||
tags.forEach((tag, idx) => { | ||
query += `(?,?)` | ||
if (idx !== tags.length - 1) { | ||
query += `,` | ||
} | ||
queryArr.push(questionId, tag) | ||
}) | ||
|
||
connection.query(query, queryArr, (error) => { | ||
if (error) { | ||
return connection.rollback(() => { | ||
connection.release() | ||
return reject(error) | ||
}) | ||
} | ||
return connection.commit((error) => { | ||
if (error) { | ||
return connection.rollback(() => { | ||
connection.release() | ||
return reject(error) | ||
}) | ||
} | ||
connection.release() | ||
return resolve({ | ||
message: 'Question created successfully', | ||
questionId, | ||
}) | ||
}) | ||
}) | ||
} | ||
) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = createQuestion |
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,3 @@ | ||
const createQuestion = require('./createQuestion') | ||
|
||
module.exports = { createQuestion } |
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,45 @@ | ||
const express = require('express') | ||
const { verifyUserAccessToken, verifyAdmin } = require('../middlewares') | ||
const router = express.Router() | ||
const ajv = require('../../schema') | ||
const { createQuestionSchema } = require('../../schema/questions') | ||
const { createQuestion } = require('../../models/questions') | ||
|
||
/** | ||
* | ||
* @param {Array} errArray | ||
* @return {String} | ||
*/ | ||
function sumErrors(errArray) { | ||
const cb = (a, b) => a + b.message + ', ' | ||
return errArray.reduce(cb, '') | ||
} | ||
|
||
router.post('/', verifyUserAccessToken, verifyAdmin, (req, res) => { | ||
const validate = ajv.compile(createQuestionSchema) | ||
const isValid = validate(req.body) | ||
if (!isValid) { | ||
return res.status(400).json({ | ||
success: false, | ||
error: sumErrors(validate.errors), | ||
results: null, | ||
}) | ||
} | ||
createQuestion({ ...req.body, username: req.username }) | ||
.then((results) => { | ||
return res.status(200).json({ | ||
success: true, | ||
results, | ||
error: null, | ||
}) | ||
}) | ||
.catch((error) => { | ||
return res.status(400).json({ | ||
success: false, | ||
results: null, | ||
error, | ||
}) | ||
}) | ||
}) | ||
|
||
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,3 @@ | ||
const createQuestion = require('./createQuestion') | ||
|
||
module.exports = { createQuestion } |
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