Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

something #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
secret: 'thisisthebiggestsecretyouhaveeverknowninyourwholelife',
scoreIncrementor: 10,
scoreDecrementor: 5,
scoreDecrementor: 0,
saltRounds: 10,
}
12 changes: 6 additions & 6 deletions src/models/createSampleModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module.exports = function () {
models.sequelize.sync().then(function () {
return models.User.bulkCreate([
{ email: '[email protected]', password: bcrypt.hashSync('password', config.saltRounds), name: 'Megh'},
{ email: '[email protected]', password: bcrypt.hashSync('password', config.saltRounds), name: 'Megh2'}
{ email: '[email protected]', password: bcrypt.hashSync('password', config.saltRounds), name: 'Megh2'},
], { ignoreDuplicates: true })
}).then(function () {
return models.Question.bulkCreate([
{ qno: 1, title: 'Q1 title', body: 'Question body 1', answer: 1 },
{ qno: 3, title: 'Q3 title', body: 'Question body 3', answer: 3 },
{ qno: 2, title: 'Q2 title', body: 'Question body 2', answer: 2 },
{ qno: 4, title: 'Q4 title', body: 'Question body 4', answer: 4 },
{ qno: 5, title: 'Q5 title', body: 'Question body 5', answer: 5 },
{ qno: 1, title: 'Q1 title', body: 'Question body 1', answer: 1,unlock_points:0 },
{ qno: 2, title: 'Q2 title', body: 'Question body 2', answer: 2,unlock_points:10 },
{ qno: 3, title: 'Q3 title', body: 'Question body 3', answer: 3,unlock_points:20 },
{ qno: 4, title: 'Q4 title', body: 'Question body 4', answer: 4,unlock_points:20 },
{ qno: 5, title: 'Q5 title', body: 'Question body 5', answer: 5,unlock_points:30 },
], { ignoreDuplicates: true })
})
}
1 change: 1 addition & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
sequelize: require('./dbconfig'),
User: require('./user'),
Question: require('./question'),
Mapping: require('./mapping'),
}
23 changes: 23 additions & 0 deletions src/models/mapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var Sequelize = require('sequelize');
var sequelize = require('./dbconfig');

var Mapping = sequelize.define('mapping', {
qno: {
type: Sequelize.INTEGER,
allowNull: false,
validate: {
notEmpty: true,
isInt: true,
}
},
uid: {
type: Sequelize.INTEGER,
allowNull: false,
validate: {
isInt: true,
notEmpty: true,
}
},
});

module.exports = Mapping;
11 changes: 9 additions & 2 deletions src/models/question.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var Sequelize = require('sequelize');
var sequelize = require('./dbconfig');

var Question = sequelize.define('question', {
qno: {
type: Sequelize.INTEGER,
Expand Down Expand Up @@ -32,7 +31,15 @@ var Question = sequelize.define('question', {
validate: {
notEmpty: true,
}
},
unlock_points: {
type: Sequelize.INTEGER,
allowNull: false,
validate: {
notEmpty: true,
isInt: true,
}
}
});

module.exports = Question
module.exports = Question;
50 changes: 31 additions & 19 deletions src/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,48 @@ var middleware = require('./middleware');
var config = require('./config');

router.get('/:qno(\\d+)?', middleware.isAuthenticated, (req, res) => {
var { qno } = req.params;
const { lastQuestionAllowed } = req.user;
var qno = req.params.qno;
var score = req.user.score;
var lastQuestionAllowed =req.user.lastQuestionAllowed;
if (!qno) qno = lastQuestionAllowed;
if (qno > lastQuestionAllowed) {res.sendStatus(403); return false;}
models.Question.findOne({
where : { qno },
attributes: {exclude: ['answer']}
}).then(question => {
if (question) res.send(question);
else res.sendStatus(400);
if (question){
if ( question.unlock_points > score)
{res.status(403).send({points:question.unlock_points});return false;}
else res.send(question);
}
else {
res.sendStatus(400);
}
})
});

router.post('/check/:qno(\\d+)?', middleware.isAuthenticated, (req, res) => {
var { qno } = req.params;
const { answer } = req.body;
const { lastQuestionAllowed } = req.user;
const { score } = req.user;
var qno = req.params.qno;
const uid = req.user.id;
const answer = req.body.answer;
const lastQuestionAllowed = req.user.lastQuestionAllowed;
const score = req.user.score;
if (!qno) qno = lastQuestionAllowed;
if (qno > lastQuestionAllowed) {res.sendStatus(403); return false;}
models.Question.findOne({where : { qno }})
.then(question => {
if (question) {
if (question.answer == answer && qno ==lastQuestionAllowed)
req.user.update({ score: score + config.scoreIncrementor, lastQuestionAllowed: lastQuestionAllowed + 1 });
else if (question.answer != answer && qno == lastQuestionAllowed)
req.user.update({ score: score - config.scoreDecrementor });
res.send({result: question.answer == answer});
} else res.sendStatus(400);
})
.then(question => {
if (question) {
models.Mapping.findOne({where: {qno:qno,uid:uid}}).then(mapping => {
if(!mapping){
if (question.answer == answer && question.unlock_points <= score){
req.user.update({ score: score + config.scoreIncrementor, lastQuestionAllowed: lastQuestionAllowed + 1 });
models.Mapping.create({qno,uid});
}
else if (question.answer != answer && question.unlock_points <= score)
req.user.update({ score: score - config.scoreDecrementor });
}
})
res.send({result: question.answer == answer});
} else res.sendStatus(400);
})
})

module.exports = router;