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

total-questions added to the attributes #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Manit1
Copy link
Contributor

@Manit1 Manit1 commented May 15, 2019

Fixes #13

@Manit1
Copy link
Contributor Author

Manit1 commented May 15, 2019

@abhishek97
I used this as the reference

@abhishek97
Copy link
Contributor

This would not work if i don't include all questions along with quiz. Ideally this should at controller level

@Manit1
Copy link
Contributor Author

Manit1 commented May 16, 2019

@abhishek97 sure !
Doing this for /:id is pretty straightforward but when querying all the quizzes should again iterate through them to include the questions count. Something like

const {rows, count} = this.findAll(...arguments)
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
  })

@abhishek97
Copy link
Contributor

Can be done for /quizzes by group by

DB.quizzes.findAll({
  includeIgnoreAttributes: false,
  attributes: [ 'id' ,[Sequelize.fn('count', Sequelize.col('questions->quizQuestions.id')), 'total']],
  include: {
    model: DB.questions,
  },
 where: {
    $in: quizIds
  },
  group: ['quizzes.id']
})

and then map total to the quizzes object

@Manit1
Copy link
Contributor Author

Manit1 commented May 29, 2019

@abhishek97 Thanks and done! 😄 🚀

@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't mean to be like this

group: ['quizzes.id']
})
quizQuestionCounts = R.groupBy(obj => obj.id)(quizQuestionCounts)
rows = rows.map( _ => _.get({plain: true}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these those map calls can be combined into one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Send in totalQuestions along with quiz
2 participants