Skip to content

Commit

Permalink
Merge pull request #27 from Oik17/master
Browse files Browse the repository at this point in the history
feat: added admin auth with q's
  • Loading branch information
Mr-Emerald-Wolf committed Sep 4, 2023
2 parents c69f5c7 + ac77d38 commit 8eeb49b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
48 changes: 36 additions & 12 deletions api/controllers/questions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const ques = require("../models/ques");
const jwt = require("jsonwebtoken");

async function getQuestionByID(req, res) {
try {
const questions = await ques.findById(req.body.id).populate("testCases");
console.log(questions);
var questions;
const decoded=req.user

if(decoded.userRole=='admin'){
questions = await ques.findById(req.body.id).populate("testCases");
}
else{
questions = await ques.findById(req.body.id).populate({path: 'testCases', match: {hidden: false}});
}
if(questions.length==0){
return res.status(404).json({
message: "No questions found",
Expand All @@ -12,6 +20,7 @@ async function getQuestionByID(req, res) {
else{
return res.status(201).json(questions);
}

} catch (error) {
return res.status(500).json({
message: error.message,
Expand All @@ -21,17 +30,24 @@ async function getQuestionByID(req, res) {

async function getAll(req, res) {
try {
const questionsAll = await ques.find().populate("testCases");

var questionsAll;
const decoded=req.user;
if(decoded.userRole=='admin'){
questionsAll = await ques.find().populate("testCases");
}
else{
questionsAll = await ques.find().populate({path: 'testCases', match: {hidden: false}});
}
if(questionsAll.length==0){
return res.status(404).json({
return res.status(404).json({
message: "No questions found",
})
}
else{
return res.status(201).json(questionsAll);
}
} catch (error) {
return res.status(201).json(questionsAll);
}
}
catch (error) {
return res.status(500).json({
message: error.message,
});
Expand All @@ -40,8 +56,16 @@ async function getAll(req, res) {

async function getByRound(req, res) {
try {
const questionByRound = await ques.where("round").equals(req.body.round).populate("testCases");
console.log(questionByRound);
var questionByRound;
const decoded=req.user;
if(decoded.userRole=='admin'){
questionByRound = await ques.where("round").equals(req.body.round).populate("testCases");
}
else{
questionByRound = await ques.where("round").equals(req.body.round).populate({path: 'testCases', match: {hidden: false}});

}

if(questionByRound.length==0){
return res.status(404).json({
message: "No questions found",
Expand Down Expand Up @@ -102,7 +126,7 @@ async function createQuestion(req, res) {
name: req.body.name,
id: req.body.id,
inputFormat: req.body.inputFormat,
outputformat: req.body.outputFormat,
outputFormat: req.body.outputFormat,
constraints: req.body.constraints,
sampleTestInput: req.body.sampleTestInput,
sampleTestOutput: req.body.sampleTestOutput,
Expand Down Expand Up @@ -130,4 +154,4 @@ module.exports = {
getQuestionByID,
updateQuestion,
deleteQuestion
};
};
6 changes: 2 additions & 4 deletions api/models/ques.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const quesSchema = new mongoose.Schema({
inputFormat: [{ type: String }],
outputFormat: [{ type: String }],
constraints: [{ type: String, required: true }],
sampleTestInput: { type: String, required: true },
sampleTestOutput: { type: String, required: true },
timeLimit: { type: String, required: true },
sourceLimit: { type: String, required: true },
sampleTestInput: [{ type: String, required: true }],
sampleTestOutput: [{ type: String, required: true }],
round: { type: Number, required: true },
testCases: [{ type: mongoose.Schema.Types.ObjectId, ref: "Testcase" }],
});
Expand Down
13 changes: 8 additions & 5 deletions api/routes/questionsRouter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const express = require("express");
const router = express.Router();
const {verifyAccessToken,verifyAdminToken} = require("../middleware/jwtMiddleware.js");

const {
createQuestion,
getAll,
getByRound,
getQuestionByID,
updateQuestion,
deleteQuestion,
deleteQuestion
} = require("../controllers/questions");

router.post("/createQues", createQuestion);
router.use(verifyAccessToken)
router.post("/createQues", verifyAdminToken, createQuestion);
router.get("/getOne", getAll);
router.post("/getId", getQuestionByID);
router.post("/getRound", getByRound);
router.put("/updateQuestion/:id", updateQuestion);
router.delete("/deleteQuestion/:id",deleteQuestion);
module.exports = router;
router.put("/updateQuestion/:id", verifyAdminToken, updateQuestion);
router.delete("/deleteQuestion/:id",verifyAdminToken, deleteQuestion);

module.exports = router;

0 comments on commit 8eeb49b

Please sign in to comment.