Skip to content

Commit

Permalink
send maxMarks along with quiz/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek97 committed Sep 14, 2020
1 parent 68238b6 commit ea1d900
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js"
}
]
}
2 changes: 1 addition & 1 deletion framework/serializers/quizzes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function (included = [], type, config) {
}

const options = {
attributes: ['title', 'description', 'image', 'duration', 'maxAttempts', 'startDate', 'endDate', 'user', 'questions'],
attributes: ['title', 'description', 'image', 'duration', 'maxAttempts', 'startDate', 'endDate', 'user', 'questions', 'maxMarks'],
meta: {
pagination: function (record) {
return record.pagination
Expand Down
17 changes: 17 additions & 0 deletions routes/api/quiz/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const DB = require('../../../models')
const U = require('../../../utils')
const R = require('ramda')
const WhispererService = require('../../../services/whisperer')
const Sequelize = require('sequelize')

class QuizController extends BaseController {
constructor () {
Expand Down Expand Up @@ -148,6 +149,22 @@ class QuizController extends BaseController {
})
res.json(questions)
}

async onAfterGetById(req, quiz) {
const [result] = await DB.quizzes.findAll({
logging: true,
includeIgnoreAttributes: false,
attributes: [ 'id' ,[Sequelize.fn('sum', Sequelize.col('questions.positiveScore')), 'total']],
include: {
model: DB.questions,
},
where: {
id: quiz.id
},
group: ['quizzes.id']
})
quiz.maxMarks = result ? +result.get('total') : 0
}
}

module.exports = QuizController

0 comments on commit ea1d900

Please sign in to comment.