Skip to content

Commit

Permalink
add multicorrect flag to questions
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek97 committed Jun 11, 2020
1 parent 7db3435 commit 7f51264
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework/serializers/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = function (included = [], type, config) {
'difficulty',
'positiveScore',
'negativeScore',
'multicorrect',
'user',
'choices',
'tags',
Expand Down
21 changes: 21 additions & 0 deletions migrations/20200611143525-addMultipleCorrectFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

module.exports = {
async up (queryInterface, Sequelize) {
await queryInterface.addColumn('questions', 'multicorrect', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
})
// Migrate existing data
return queryInterface.query(`
update questions
set multicorrect = array_length("correctAnswers", 1) > 1
where array_length("correctAnswers", 1) is not null;
`)

},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('questions', 'multicorrect')
}
};
5 changes: 5 additions & 0 deletions models/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
multicorrect: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
paranoid: true
Expand Down
10 changes: 10 additions & 0 deletions routes/api/questions/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class QuestionsController extends BaseController {
})

}

async onAfterCreate(req, model) {
model.multicorrect = model.correctAnswers.length > 1
return model.save()
}

async onAfterUpdate(req, model) {
model.multicorrect = model.correctAnswers.length > 1 || model.multicorrect
return model.save()
}

async handleUpdateById(req, res) {
const modelObj = await this.deserialize(req.body)
Expand Down

0 comments on commit 7f51264

Please sign in to comment.