Commit e4c6248a by Shaganaz

Implemented admin and user functionality using express

parent ed43969e
......@@ -6,6 +6,18 @@ meta {
post {
url: http://localhost:3000/api/auth/login
body: none
body: json
auth: inherit
}
headers {
~content-type: application/json
}
body:json {
{
"email":"admin@gmail.com",
"password":"admin@123",
"role":"admin"
}
}
......@@ -5,7 +5,23 @@ meta {
}
post {
url: http://localhost:3000/api/admin/createQuiz
body: none
auth: inherit
url: http://localhost:3000/api/admin/assign-quiz
body: json
auth: bearer
}
headers {
content-type: application/json
}
auth:bearer {
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImVtYWlsIjoiYWRtaW5AZ21haWwuY29tIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNzQ4NTIyMTg2LCJleHAiOjE3NDg1MjU3ODZ9.WTUXrCWQuLqncxQzt9XB2HBMvJ5OXb2MeYP6SWmHUAc
}
body:json {
{
"user_id": 2,
"quiz_id": 5
}
}
......@@ -5,7 +5,11 @@ meta {
}
get {
url: http://localhost:3000/api/dashboard/admin
url: http://localhost:3000/api/dashboard/user
body: none
auth: inherit
}
headers {
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjIsImVtYWlsIjoic2hhZ2FuYXpAZ21haWwuY29tIiwicm9sZSI6InVzZXIiLCJpYXQiOjE3NDg1MTQzNjEsImV4cCI6MTc0ODUxNzk2MX0.BOFvb33tIZYkXTJ_1yG8xNfhdKojjVvNVgNRqp9lvw0
}
......@@ -6,6 +6,20 @@ meta {
post {
url: http://localhost:3000/api/auth/register
body: none
body: json
auth: inherit
}
headers {
~content-type: application/json
}
body:json {
{
"email": "shaganaz@gmail.com",
"password": "shaganaz",
"role": "user"
}
}
meta {
name: user
type: http
seq: 5
}
get {
url: http://localhost:3000/api/user/take-quiz/2/1
body: none
auth: inherit
}
......@@ -8,8 +8,9 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/auth', require('./server/routes/authroutes'));
app.use('/api/dashboard', require('./server/routes/dashboard'))
app.use('/api/dashboard', require('./server/routes/dashboard'));
app.use('/api/admin', require('./server/routes/adminroutes'));
app.use('/api/user', require('./server/routes/userroutes'));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
......
......@@ -34,4 +34,34 @@ exports.assignQuiz = async (req, res) => {
} catch (err) {
return res.json({ message: 'Unable to assign quiz', error: err.message });
}
};
\ No newline at end of file
};
exports.getUserResult = async (req, res) => {
const { user_id, quiz_id } = req.params;
try {
const [results] = await db.query(`select q.question_text,
ua.answer as selected_option_id,
o1.option_text as selected_answer,
o2.option_text as correct_answer,
ua.is_correct
from user_answers ua
join questions q ON ua.question_id = q.id
join options o1 ON ua.answer = o1.id
join options o2 ON o2.question_id = q.id and o2.is_correct = 1
where ua.user_id = ? and ua.quiz_id = ?`,
[user_id, quiz_id]
);
const [summary] = await db.query(`SELECT score, total from results where user_id = ? and quiz_id = ?`,[user_id, quiz_id]);
return res.json({
score: summary[0]?.score || 0,
total: summary[0]?.total || 0,
answers: results
});
} catch (err) {
return res.status(500).json({ message: 'Error fetching user result', error: err.message });
}
};
const db=require('../config/database');
exports.takeQuiz=async(req,res)=>{
const {quiz_id,user_id}=req.params;
try{
const[assigned]=await db.query(`SELECT* from user_quizzes where user_id=? and quiz_id=? and status='pending'`,[user_id,quiz_id]);
if(assigned.length===0){
return res.json({message:'Quiz completed or not assigned properly'});
}
const[quiz]=await db.query(`select id,title,description from quizzes where id=?`,[quiz_id]);
const[questions]=await db.query(`select q.id as question_id, q.question_text,
o.id as option_id, o.option_text
from questions q
join options o on q.id=o.question_id
where q.quiz_id=?
order by q.id,o.id`,[quiz_id]);
const questionsMap = {};
for (const row of questions) {
if (!questionsMap[row.question_id]) {
questionsMap[row.question_id] = {
question_id: row.question_id,
question_text: row.question_text,
options: []
};
}
questionsMap[row.question_id].options.push({
option_id: row.option_id,
option_text: row.option_text
});
}
return res.json({
quiz: quiz[0],
questions: Object.values(questionsMap)
});
}
catch(err){
return res.json({message:"Error fetching quiz",error:err.message});
}
};
exports.submitQuiz = async (req, res) => {
const { user_id, quiz_id, answers } = req.body;
try {
let score = 0;
const total = answers.length;
for (const ans of answers) {
const [correct] = await db.query(
'select is_correct from options where id = ?',
[ans.selected_option_id]
);
const isCorrect = correct[0]?.is_correct === 1 ? 1 : 0;
if (isCorrect) score++;
await db.query(
`insert into user_answers (user_id, quiz_id, question_id, answer, is_correct)
values (?, ?, ?, ?, ?)`,
[user_id, quiz_id, ans.question_id, ans.selected_option_id, isCorrect]
);
}
await db.query(
`insert into results (user_id, quiz_id, score, total)
values (?, ?, ?, ?)`,
[user_id, quiz_id, score, total]
);
await db.query(
`update user_quizzes set status = 'completed' where user_id = ? AND quiz_id = ?`,
[user_id, quiz_id]
);
return res.json({ message: 'Quiz submitted', score, total });
} catch (err) {
return res.status(500).json({ message: 'Error submitting quiz', error: err.message });
}
};
......@@ -6,4 +6,5 @@ const adminctrl=require('../controller/adminController');
router.post('/create-quiz',authenticateAdmin,adminctrl.createQuiz);
router.post('/create-question',authenticateAdmin,adminctrl.createQuestion);
router.post('/assign-quiz', authenticateAdmin, adminctrl.assignQuiz);
router.get('/user-result/:user_id/:quiz_id', authenticateAdmin, adminctrl.getUserResult);
module.exports=router;
\ No newline at end of file
const express=require('express');
const router = express.Router();
const authenticateUser=require('../middleware/usermiddleware');
const userctrl=require('../controller/userController');
router.get('/take-quiz/:user_id/:quiz_id',authenticateUser,userctrl.takeQuiz);
router.post('/submit-quiz',authenticateUser,userctrl.submitQuiz);
module.exports=router;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment