Skip to content

Commit 7f51264

Browse files
committed
add multicorrect flag to questions
1 parent 7db3435 commit 7f51264

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

framework/serializers/questions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function (included = [], type, config) {
3131
'difficulty',
3232
'positiveScore',
3333
'negativeScore',
34+
'multicorrect',
3435
'user',
3536
'choices',
3637
'tags',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
module.exports = {
4+
async up (queryInterface, Sequelize) {
5+
await queryInterface.addColumn('questions', 'multicorrect', {
6+
type: Sequelize.BOOLEAN,
7+
allowNull: false,
8+
defaultValue: false
9+
})
10+
// Migrate existing data
11+
return queryInterface.query(`
12+
update questions
13+
set multicorrect = array_length("correctAnswers", 1) > 1
14+
where array_length("correctAnswers", 1) is not null;
15+
`)
16+
17+
},
18+
down: (queryInterface, Sequelize) => {
19+
return queryInterface.removeColumn('questions', 'multicorrect')
20+
}
21+
};

models/questions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ module.exports = (sequelize, DataTypes) => {
3232
type: DataTypes.INTEGER,
3333
allowNull: false,
3434
defaultValue: 0
35+
},
36+
multicorrect: {
37+
type: DataTypes.BOOLEAN,
38+
allowNull: false,
39+
defaultValue: false
3540
}
3641
}, {
3742
paranoid: true

routes/api/questions/controller.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ class QuestionsController extends BaseController {
4343
})
4444

4545
}
46+
47+
async onAfterCreate(req, model) {
48+
model.multicorrect = model.correctAnswers.length > 1
49+
return model.save()
50+
}
51+
52+
async onAfterUpdate(req, model) {
53+
model.multicorrect = model.correctAnswers.length > 1 || model.multicorrect
54+
return model.save()
55+
}
4656

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

0 commit comments

Comments
 (0)