diff --git a/_docs/question.js b/_docs/question.js index 43f453e..19b2ae4 100644 --- a/_docs/question.js +++ b/_docs/question.js @@ -104,6 +104,24 @@ * date_added:"23-02-2009", * difficulty:3 * }] + * getQuestionById: + * type: object + * properties: + * message: + * type: array + * description: To get questions from their respective questionsIds + * example: + * message: [{ + * tags:['graph'], + * time_limit:1, + * source_limit:50000, + * question_title:"Life, the Universe, and Everything", + * problem_code:"test", + * description:"Your program is to use the brute-force approach in order to find the A...", + * author:"admin", + * date_added:"23-02-2009", + * difficulty:3 + * }] * deleteQuestion: * type: object * properties: @@ -122,6 +140,31 @@ * message: * type: array * description: Update a question by ID + * getRandom: + * type: object + * properties: + * message: + * type: array + * description: To get n random question IDs + * example: + * message: [ + * "5f32a127c5e5b2da2163957c", + * "5f3374e499b995216f0aece4", + * "5f3374dc99b995216f0aece3" + * ] + * getTestCase: + * type: object + * properties: + * message: + * type: array + * description: Takes an input of array of questionID's and return an object with question ID's and their testcases. + * example: + * message: { + * "5f32aa1dd562473b18f8ebd9": ["abc"], + * "5f32aa25d562473b18f8ebda": ["def"], + * "5f32aa2dd562473b18f8ebdb": ["ghi"], + * "5f32aa34d562473b18f8ebdc": ["jkl"] + * } * Error: * type: object * properties: @@ -169,16 +212,23 @@ * application/json: * schema: * $ref: '#/components/schemas/Error' - * delete: - * summary: delete all questions + * /question/getQById/: + * post: + * summary: Get questions by Id * tags: [Question] + * requestBody: + * required: true + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/getQuestionById' * responses: * "200": - * description: delete all questions + * description: Return the questions of the questionIds * content: * application/json: * schema: - * $ref: '#/components/schemas/deleteQuestion' + * $ref: '#/components/schemas/getQuestionById' * "401": * description: Client mistake * content: @@ -224,4 +274,39 @@ * application/json: * schema: * $ref: '#/components/schemas/Error' + * /question/random/: + * get: + * summary: Get n random question Ids + * tags: [Question] + * responses: + * "200": + * description: get n random questionIds + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/getRandom' + * "401": + * description: Client mistake + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/Error' + * /testcase: + * get: + * summary: Takes an array of question ID's and returns an object with question_ID's and their testcases + * tags: [Question] + * responses: + * "200": + * description: get testcases for questions + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/getTestCase' + * "401": + * description: Client mistake + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/Error' + * */ diff --git a/server/controllers/questionController.js b/server/controllers/questionController.js index fd0035d..4713769 100644 --- a/server/controllers/questionController.js +++ b/server/controllers/questionController.js @@ -39,6 +39,27 @@ const getQuestion = async (req, res) => { } }; +const getTestCase = async (req, res) => { + try { + const testArray = {}; + const n = req.query.id.length; + let i; + // eslint-disable-next-line no-plusplus + for (i = 0; i < n; i++) { + // eslint-disable-next-line no-await-in-loop + const question = await Question.findOne({ _id: req.query.id[i] }); + testArray[req.query.id[i]] = question.testcase; + } + res.status(200).json({ + message: testArray, + }); + } catch (err) { + res.status(401).json({ + message: err.message, + }); + } +}; + const deleteQuestion = async (req, res) => { try { const resp = await Question.remove({}); @@ -89,8 +110,9 @@ const patchQuestionById = async (req, res) => { }; module.exports = { - getQuestion, putQuestion, + getQuestion, + getTestCase, deleteQuestion, deleteQuestionById, patchQuestionById, diff --git a/server/middlewares/auth.js b/server/middlewares/auth.js new file mode 100644 index 0000000..fcab3d1 --- /dev/null +++ b/server/middlewares/auth.js @@ -0,0 +1,8 @@ +const onlyAdmins = (req, next) => { + // check user permissions + next(); +}; + +module.exports = { + onlyAdmins, +}; diff --git a/server/models/questionModel.js b/server/models/questionModel.js index 0bf94a5..90df6ea 100644 --- a/server/models/questionModel.js +++ b/server/models/questionModel.js @@ -5,6 +5,10 @@ const questionSchema = new mongoose.Schema({ type: String, required: true, }, + picture: { + type: String, + required: false, + }, problemCode: { type: String, required: true, @@ -39,6 +43,10 @@ const questionSchema = new mongoose.Schema({ type: Number, required: true, }, + testcase: { + type: [String], + required: true, + }, }); // eslint-disable-next-line prettier/prettier diff --git a/server/routes/question.js b/server/routes/question.js index ce0dbe1..6bc4e86 100644 --- a/server/routes/question.js +++ b/server/routes/question.js @@ -1,23 +1,31 @@ const express = require('express'); - +const Question = require('../models/questionModel'); const route = express.Router(); const { getQuestion, putQuestion, + getTestCase, deleteQuestion, deleteQuestionById, patchQuestionById, } = require('../controllers/questionController'); +const { onlyAdmins } = require('../middlewares/auth'); + route.post('/', putQuestion); route.get('/', getQuestion); +//route.get('/test/:questionId', getTestCase); + route.delete('/', deleteQuestion); route.delete('/:questionId', deleteQuestionById); route.patch('/:questionId', patchQuestionById); +// testcase routes +route.get('/testcase', getTestCase); + module.exports = route; diff --git a/server/server.js b/server/server.js index 16a6a47..afb3735 100644 --- a/server/server.js +++ b/server/server.js @@ -12,7 +12,7 @@ connectDB(); app.use(bodyParser.json()); app.use('/questions', require('./routes/question')); -const server = app.listen(process.env.PORT || 3000, () => { +const server = app.listen(process.env.PORT || 5500, () => { const host = os.hostname(); console.log('Server Started at ', host, ':', server.address().port); });