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

tag models done #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion framework/serializers/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ module.exports = function (included = [], type, config) {
id: relationship.id
}
}
},
tags: {
valueForRelationship (relationship) {
return {
id: relationship.id
}
}
}
}
}

const options = {
attributes: ['title', 'description', 'difficulty' ,'user', 'choices', 'createdBy'],
attributes: ['title', 'description', 'difficulty' ,'user', 'choices', 'tags', 'createdBy'],
meta: {
pagination: function (record) {
return record.pagination
Expand All @@ -33,6 +40,11 @@ module.exports = function (included = [], type, config) {
attributes: ['title', 'description', 'positiveWeight' ,'negativeWeight', 'question'],
included: included.includes('choices')
},
tags: {
ref: 'id',
attributes: ['name'],
included: included.includes('tags')
},
...config
}

Expand Down
17 changes: 17 additions & 0 deletions framework/serializers/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function (included = [], type, config) {
if (typeof type === 'object') {
config = type
}

const options = {
attributes: ['name'],
meta: {
pagination: function (record) {
return record.pagination
}
},
...config
}

return options
}
36 changes: 36 additions & 0 deletions migrations/20190521182844-createTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
queryInterface.createTable('tags', {
id: {
type:Sequelize.INTEGER,
primaryKey:true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
createdById: {
type: Sequelize.INTEGER,
allowNull: false
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
},
deletedAt: {
type: Sequelize.DATE
}
})
},

down: (queryInterface, Sequelize) => {
queryInterface.dropTable('tags')
}
};
36 changes: 36 additions & 0 deletions migrations/20190521183150-createQuestionTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
queryInterface.createTable('question_tags', {
id:{
type:Sequelize.INTEGER,
primaryKey:true,
autoIncrement: true
},
tagId: {
type: Sequelize.INTEGER,
allowNull: false
},
questionId: {
type: Sequelize.INTEGER,
allowNull: false
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
},
deletedAt: {
type: Sequelize.DATE
}
})
},

down: (queryInterface, Sequelize) => {
queryInterface.dropTable('question_tags')
}
};
14 changes: 14 additions & 0 deletions models/questionTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use-strict'
module.exports = (Sequelize, DataTypes) => {
const questionTags = Sequelize.define('question_tags', {
id:{
type:DataTypes.INTEGER,
primaryKey:true,
autoIncrement: true
}
}, { paranoid: true })

questionTags.associate = function (models) {}

return questionTags
}
3 changes: 1 addition & 2 deletions models/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ module.exports = (sequelize, DataTypes) => {
questions.hasMany(models.choices)
questions.belongsTo(models.users,{foreignKey: 'updatedById'})
questions.belongsToMany(models.quizzes, {through: models.quizQuestions})


questions.belongsToMany(models.tags, {through: models.question_tags})
};
return questions;
};
16 changes: 16 additions & 0 deletions models/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use-strict'
module.exports = (Sequelize, DataTypes) => {
const tags = Sequelize.define('tags', {
name: {
type: DataTypes.STRING,
allowNull: false
}
}, { paranoid: true })

tags.associate = function(models) {
tags.belongsTo(models.users,{foreignKey: 'createdById'})
tags.belongsToMany(models.questions, {through: models.question_tags})
}

return tags
}
2 changes: 2 additions & 0 deletions routes/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const questions = require('./questions')
const choices = require('./choices')
const users = require('./users')
const quiz = require('./quiz')
const tags = require('./tags')
const Router = require('express').Router()

Router.use('/questions', questions)
Router.use('/choices', choices)
Router.use('/users', users)
Router.use('/quizzes', quiz)
Router.use('/tags', tags)


module.exports = Router
26 changes: 26 additions & 0 deletions routes/api/questions/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ class QuestionsController extends BaseController {
this.submitQuestion = this.submitQuestion.bind(this)
}

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

// set updatedBy
modelObj.updatedById = req.user.id

await DB.question_tags.bulkCreate(modelObj.tags.map(tag => ({
tagId: tag.id,
questionId: req.params.id
})))

await this._model.update(modelObj, {
where: {
id: req.params.id
}
})
const result = await this._model.findById(req.params.id, {
include: this.generateIncludeStatement()
})
res.json(this.serialize(result))
} catch (err) {
rrthis.handleError(err, res)
}
}

async handleGetAnswers (req, res) {
const { id } = req.params

Expand Down
17 changes: 17 additions & 0 deletions routes/api/tags/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const BaseController = require('../../../framework/Controller.class')
const routes = require('express').Router()
const DB = require('../../../models')

const passport = require('../../../passport/index')
const { adminOnly } = require('../../../passport/middlewares')

const controller = new BaseController(DB.tags)

routes.use(passport.authenticate('bearer', {session: false}), adminOnly)

routes.get('/', controller.handleQuery)
routes.get('/:id', controller.handleQueryById)
routes.post('/', controller.handleCreate)
routes.patch('/:id', controller.handleUpdateById)

module.exports = routes;