From 6f2c504884a8e6fb5701fe77d3f3c3352377a23d Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Wed, 22 May 2019 00:32:08 +0530 Subject: [PATCH 1/4] tag models done --- framework/serializers/tags.js | 17 +++++++++ migrations/20190521182844-createTags.js | 36 +++++++++++++++++++ .../20190521183150-createQuestionTags.js | 36 +++++++++++++++++++ models/questionTags.js | 14 ++++++++ models/questions.js | 3 +- models/tags.js | 16 +++++++++ routes/api/index.js | 2 ++ routes/api/tags/index.js | 16 +++++++++ 8 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 framework/serializers/tags.js create mode 100644 migrations/20190521182844-createTags.js create mode 100644 migrations/20190521183150-createQuestionTags.js create mode 100644 models/questionTags.js create mode 100644 models/tags.js create mode 100644 routes/api/tags/index.js diff --git a/framework/serializers/tags.js b/framework/serializers/tags.js new file mode 100644 index 0000000..21a5f58 --- /dev/null +++ b/framework/serializers/tags.js @@ -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 +} \ No newline at end of file diff --git a/migrations/20190521182844-createTags.js b/migrations/20190521182844-createTags.js new file mode 100644 index 0000000..61ce1c2 --- /dev/null +++ b/migrations/20190521182844-createTags.js @@ -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 + }, + createdBy: { + 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') + } +}; diff --git a/migrations/20190521183150-createQuestionTags.js b/migrations/20190521183150-createQuestionTags.js new file mode 100644 index 0000000..2685508 --- /dev/null +++ b/migrations/20190521183150-createQuestionTags.js @@ -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') + } +}; diff --git a/models/questionTags.js b/models/questionTags.js new file mode 100644 index 0000000..3196279 --- /dev/null +++ b/models/questionTags.js @@ -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 +} \ No newline at end of file diff --git a/models/questions.js b/models/questions.js index 224dfae..3148d87 100644 --- a/models/questions.js +++ b/models/questions.js @@ -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; }; \ No newline at end of file diff --git a/models/tags.js b/models/tags.js new file mode 100644 index 0000000..e712040 --- /dev/null +++ b/models/tags.js @@ -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: 'createdBy'}) + tags.belongsToMany(models.questions, {through: models.question_tags}) + } + + return tags +} \ No newline at end of file diff --git a/routes/api/index.js b/routes/api/index.js index 38ceb10..ec8d5ed 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -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 \ No newline at end of file diff --git a/routes/api/tags/index.js b/routes/api/tags/index.js new file mode 100644 index 0000000..06fe1c1 --- /dev/null +++ b/routes/api/tags/index.js @@ -0,0 +1,16 @@ +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) + +module.exports = routes; From ef45b6fc8d3358a74fc90f06df7d2bbada578c7b Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Wed, 22 May 2019 01:27:32 +0530 Subject: [PATCH 2/4] question update added --- framework/serializers/questions.js | 14 +++++++++++++- migrations/20190521182844-createTags.js | 2 +- models/tags.js | 2 +- routes/api/tags/index.js | 1 + 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/framework/serializers/questions.js b/framework/serializers/questions.js index 5f1c5e9..1d6b372 100644 --- a/framework/serializers/questions.js +++ b/framework/serializers/questions.js @@ -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 @@ -33,6 +40,11 @@ module.exports = function (included = [], type, config) { attributes: ['title', 'description', 'positiveWeight' ,'negativeWeight', 'question'], included: included.includes('choices') }, + tags: { + ref: 'id', + ...require('./tags')([], 'serialize'), + included: included.includes('tags') + }, ...config } diff --git a/migrations/20190521182844-createTags.js b/migrations/20190521182844-createTags.js index 61ce1c2..be3f1a9 100644 --- a/migrations/20190521182844-createTags.js +++ b/migrations/20190521182844-createTags.js @@ -12,7 +12,7 @@ module.exports = { type: Sequelize.STRING, allowNull: false }, - createdBy: { + createdById: { type: Sequelize.INTEGER, allowNull: false }, diff --git a/models/tags.js b/models/tags.js index e712040..aee5951 100644 --- a/models/tags.js +++ b/models/tags.js @@ -8,7 +8,7 @@ module.exports = (Sequelize, DataTypes) => { }, { paranoid: true }) tags.associate = function(models) { - tags.belongsTo(models.users,{foreignKey: 'createdBy'}) + tags.belongsTo(models.users,{foreignKey: 'createdById'}) tags.belongsToMany(models.questions, {through: models.question_tags}) } diff --git a/routes/api/tags/index.js b/routes/api/tags/index.js index 06fe1c1..3984180 100644 --- a/routes/api/tags/index.js +++ b/routes/api/tags/index.js @@ -12,5 +12,6 @@ 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; From f168dec9eaa8451127ba0623c45490d2eb4064f0 Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Wed, 22 May 2019 01:34:32 +0530 Subject: [PATCH 3/4] add delete --- routes/api/tags/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/routes/api/tags/index.js b/routes/api/tags/index.js index 3984180..f4b8367 100644 --- a/routes/api/tags/index.js +++ b/routes/api/tags/index.js @@ -13,5 +13,6 @@ routes.get('/', controller.handleQuery) routes.get('/:id', controller.handleQueryById) routes.post('/', controller.handleCreate) routes.patch('/:id', controller.handleUpdateById) +routes.delete('/:id', controller.handleDeleteById) module.exports = routes; From 57c55ca9d5c68111ae38d16136c9b8ed4b649ead Mon Sep 17 00:00:00 2001 From: Manit Malhotra Date: Fri, 24 May 2019 01:33:45 +0530 Subject: [PATCH 4/4] question fix --- framework/serializers/questions.js | 2 +- routes/api/questions/controller.js | 26 ++++++++++++++++++++++++++ routes/api/tags/index.js | 1 - 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/framework/serializers/questions.js b/framework/serializers/questions.js index 1d6b372..184a55e 100644 --- a/framework/serializers/questions.js +++ b/framework/serializers/questions.js @@ -42,7 +42,7 @@ module.exports = function (included = [], type, config) { }, tags: { ref: 'id', - ...require('./tags')([], 'serialize'), + attributes: ['name'], included: included.includes('tags') }, ...config diff --git a/routes/api/questions/controller.js b/routes/api/questions/controller.js index 3806714..5c66ec7 100644 --- a/routes/api/questions/controller.js +++ b/routes/api/questions/controller.js @@ -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 diff --git a/routes/api/tags/index.js b/routes/api/tags/index.js index f4b8367..3984180 100644 --- a/routes/api/tags/index.js +++ b/routes/api/tags/index.js @@ -13,6 +13,5 @@ routes.get('/', controller.handleQuery) routes.get('/:id', controller.handleQueryById) routes.post('/', controller.handleCreate) routes.patch('/:id', controller.handleUpdateById) -routes.delete('/:id', controller.handleDeleteById) module.exports = routes;