From f7ed53fd38929b80c130ffe57adfee23c79900b0 Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Thu, 16 May 2019 01:00:00 +0530 Subject: [PATCH 1/3] total-questions added to the attributes --- framework/serializers/quizzes.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/framework/serializers/quizzes.js b/framework/serializers/quizzes.js index d90fb45..da816c4 100644 --- a/framework/serializers/quizzes.js +++ b/framework/serializers/quizzes.js @@ -21,6 +21,12 @@ module.exports = function (included = [], type, config) { return record.pagination } }, + dataMeta: { + 'total-questions': function(record, current) { + const questions = current.questions || [] + return questions.length + } + }, user: { ref: 'id', attributes: ['firstname', 'lastname', 'email', 'role'], From 71a5b4f34a222aa334dba4d34613c7327e29bdaf Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Tue, 21 May 2019 23:41:18 +0530 Subject: [PATCH 2/3] temp --- .vscode/launch.json | 14 ++++++++++++++ framework/serializers/quizzes.js | 8 +------- routes/api/quiz/controller.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a4b5bd3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // 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", + "program": "${workspaceFolder}/index.js" + } + ] +} \ No newline at end of file diff --git a/framework/serializers/quizzes.js b/framework/serializers/quizzes.js index da816c4..df3e267 100644 --- a/framework/serializers/quizzes.js +++ b/framework/serializers/quizzes.js @@ -15,18 +15,12 @@ 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', 'total-questions'], meta: { pagination: function (record) { return record.pagination } }, - dataMeta: { - 'total-questions': function(record, current) { - const questions = current.questions || [] - return questions.length - } - }, user: { ref: 'id', attributes: ['firstname', 'lastname', 'email', 'role'], diff --git a/routes/api/quiz/controller.js b/routes/api/quiz/controller.js index 5f97880..5c69ca8 100644 --- a/routes/api/quiz/controller.js +++ b/routes/api/quiz/controller.js @@ -2,6 +2,7 @@ const BaseController = require('../../../framework/Controller.class') const DB = require('../../../models') const U = require('../../../utils') const R = require('ramda') +const Sequelize = require('sequelize') class QuizController extends BaseController { constructor () { @@ -11,6 +12,35 @@ class QuizController extends BaseController { }) } + async handleQuery(req, res) { + try { + const {rows, count} = this.findAll(...arguments) + const includeModelNames = this.getIncludeModelNames(req) + const limit = this.generateLimitStatement(req) + const offset = this.generateOffsetStatement(req) + rows = rows.map( _ => _.get({plain: true})) + rows.map(async row => { + const res = await QuizQuestions.find({ + attributes: [Sequelize.fn('count', Sequelize.col('id')), 'questions'], + where: { + quizId: row.id + } + }) + row['total-questions'] = res.questions + }) + rows.pagination = { + count, + currentOffset: offset, + nextOffset: offset + limit < count ? offset + limit : count, + prevOffset: offset - limit > 0 ? offset - limit : 0, + } + const result = this.serialize(rows, includeModelNames) + res.json(result) + } catch (err) { + this.handleError(err, res) + } + } + async handleUpdateById (req, res) { const modelObj = await this.deserialize(req.body) let questions = modelObj.questions || [] From 037bb2f133f0df7cd28db3a63b0f602d06dc8f1f Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Thu, 30 May 2019 00:54:49 +0530 Subject: [PATCH 3/3] total questions in controller --- framework/serializers/quizzes.js | 2 +- routes/api/quiz/controller.js | 47 +++++++++++++++++++++++++------- routes/api/quiz/index.js | 2 +- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/framework/serializers/quizzes.js b/framework/serializers/quizzes.js index df3e267..8440974 100644 --- a/framework/serializers/quizzes.js +++ b/framework/serializers/quizzes.js @@ -15,7 +15,7 @@ module.exports = function (included = [], type, config) { } const options = { - attributes: ['title', 'description', 'image', 'duration', 'maxAttempts', 'startDate', 'endDate', 'user', 'questions', 'total-questions'], + attributes: ['title', 'description', 'image', 'duration', 'maxAttempts', 'startDate', 'endDate', 'user', 'questions', 'totalQuestions'], meta: { pagination: function (record) { return record.pagination diff --git a/routes/api/quiz/controller.js b/routes/api/quiz/controller.js index 5c69ca8..8145e65 100644 --- a/routes/api/quiz/controller.js +++ b/routes/api/quiz/controller.js @@ -7,26 +7,36 @@ const Sequelize = require('sequelize') class QuizController extends BaseController { constructor () { super(...arguments) - new Array('handleSubmit').forEach(fn => { + new Array('handleSubmit', 'handleQueryById', 'handleQuery').forEach(fn => { this[fn] = this[fn].bind(this) }) } async handleQuery(req, res) { try { - const {rows, count} = this.findAll(...arguments) + let {rows, count} = await this.findAll(...arguments) const includeModelNames = this.getIncludeModelNames(req) const limit = this.generateLimitStatement(req) const offset = this.generateOffsetStatement(req) - rows = rows.map( _ => _.get({plain: true})) - rows.map(async row => { - const res = await QuizQuestions.find({ - attributes: [Sequelize.fn('count', Sequelize.col('id')), 'questions'], - where: { - quizId: row.id + let quizQuestionCounts = await DB.quizzes.findAll({ + includeIgnoreAttributes: false, + attributes: [ 'id' ,[Sequelize.fn('count', Sequelize.col('questions->quizQuestions.id')), 'total']], + include: { + model: DB.questions, + }, + where: { + id: { + $in: rows.map(q => q.id) } - }) - row['total-questions'] = res.questions + }, + raw: true, + group: ['quizzes.id'] + }) + quizQuestionCounts = R.groupBy(obj => obj.id)(quizQuestionCounts) + rows = rows.map( _ => _.get({plain: true})) + rows = rows.map(row => { + row.totalQuestions = quizQuestionCounts[row.id][0].total + return row }) rows.pagination = { count, @@ -41,6 +51,23 @@ class QuizController extends BaseController { } } + async handleQueryById(req, res, next) { + try { + const row = await this.findById(...arguments) + const includeModelNames = this.getIncludeModelNames(req) + const data = row.get({plain: true}) + const count = await DB.quizQuestions.count({ + where: { + quizId: row.id + } + }) + data.totalQuestions = count + res.json(this.serialize(data, includeModelNames)) + } catch (err) { + this.handleError(err, res) + } + } + async handleUpdateById (req, res) { const modelObj = await this.deserialize(req.body) let questions = modelObj.questions || [] diff --git a/routes/api/quiz/index.js b/routes/api/quiz/index.js index f280efc..5b06129 100644 --- a/routes/api/quiz/index.js +++ b/routes/api/quiz/index.js @@ -7,7 +7,7 @@ const { adminOnly } = require('../../../passport/middlewares') const controller = new BaseController(DB.quizzes) -routes.use(passport.authenticate('bearer', {session: false}), adminOnly) +// routes.use(passport.authenticate('bearer', {session: false}), adminOnly) routes.get('/', controller.handleQuery) routes.get('/:id', controller.handleQueryById)