Skip to content

Commit 6b51af5

Browse files
authored
Merge pull request #21 from imdhruvgupta/TagsMechanism
[FEATURE] introduced tags
2 parents 7c24405 + bae7a38 commit 6b51af5

14 files changed

+297
-52
lines changed

.DS_Store

-6 KB
Binary file not shown.

framework/serializers/questions.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ module.exports = function (included = [], type, config) {
1212
id: relationship.id
1313
}
1414
}
15+
},
16+
tags: {
17+
valueForRelationship (relationship) {
18+
return {
19+
id: relationship.id
20+
}
21+
}
1522
}
1623
}
1724
}
18-
25+
1926
const options = {
20-
attributes: ['title', 'description', 'difficulty' ,'user', 'choices', 'createdBy'],
27+
attributes: ['title', 'description', 'difficulty' ,'user', 'choices', 'tags','createdBy'],
2128
meta: {
2229
pagination: function (record) {
2330
return record.pagination
@@ -33,6 +40,11 @@ module.exports = function (included = [], type, config) {
3340
attributes: ['title', 'description', 'positiveWeight' ,'negativeWeight', 'question'],
3441
included: included.includes('choices')
3542
},
43+
tags: {
44+
ref: 'id',
45+
attributes: ['title'],
46+
included: included.includes('tags')
47+
},
3648
...config
3749
}
3850

framework/serializers/tags.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Created by imdhruvgupta on 11/7/19.
3+
*/
4+
5+
module.exports = function(included = [], type,config) {
6+
if(typeof type === Object) {
7+
config = type
8+
}
9+
10+
if (type === 'deserialize') {
11+
return {
12+
keyForAttribute: 'camelCase',
13+
users: {
14+
valueForRelationship (relationship) {
15+
return relationship.id
16+
}
17+
},
18+
questions: {
19+
valueForRelationship (relationship) {
20+
return relationship.id
21+
}
22+
}
23+
}
24+
}
25+
26+
const options = {
27+
attributes: ['title', 'user', 'questions', 'userId'],
28+
user: {
29+
ref: 'id',
30+
attributes: ['firstname', 'lastname', 'email', 'role'],
31+
included: included.includes('user')
32+
},
33+
questions: {
34+
ref: 'id',
35+
...require('./questions')([], 'serialize'),
36+
included: included.includes('questions')
37+
},
38+
39+
...config
40+
}
41+
42+
return options
43+
}

migrations/20180725125530-createQuiz.js

-25
This file was deleted.
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
module.exports = {
4+
async up (queryInterface, Sequelize) {
5+
await queryInterface.createTable('tags', {
6+
id: {
7+
type: Sequelize.INTEGER,
8+
primaryKey: true,
9+
autoIncrement: true
10+
},
11+
title: {
12+
type: Sequelize.STRING,
13+
allowNull: false
14+
},
15+
userId: {
16+
type: Sequelize.INTEGER,
17+
reference: {
18+
model: 'users',
19+
key: 'id'
20+
}
21+
},
22+
createdAt: {
23+
type: Sequelize.DATE
24+
},
25+
updatedAt: {
26+
type: Sequelize.DATE
27+
},
28+
deletedAt: {
29+
type: Sequelize.DATE
30+
}
31+
});
32+
33+
await queryInterface.createTable('questionsTags', {
34+
id: {
35+
type: Sequelize.INTEGER,
36+
primaryKey: true,
37+
autoIncrement: true
38+
},
39+
tagId: {
40+
type: Sequelize.INTEGER,
41+
reference: {
42+
model: 'tags',
43+
key: 'id'
44+
},
45+
allowNull: false
46+
},
47+
questionId: {
48+
type: Sequelize.INTEGER,
49+
reference: {
50+
model: 'questions',
51+
key: 'id'
52+
},
53+
allowNull: false
54+
},
55+
createdAt: {
56+
type: Sequelize.DATE
57+
},
58+
updatedAt: {
59+
type: Sequelize.DATE
60+
},
61+
deletedAt: {
62+
type: Sequelize.DATE
63+
}
64+
});
65+
},
66+
67+
down: (queryInterface, Sequelize) => {
68+
/*
69+
Add reverting commands here.
70+
Return a promise to correctly handle asynchronicity.
71+
72+
Example:
73+
return queryInterface.dropTable('users');
74+
*/
75+
}
76+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
module.exports = {
4+
async up(queryInterface, Sequelize) {
5+
await queryInterface.addIndex('questionsTags', {
6+
fields: ['questionId', 'tagId'],
7+
name: 'questionsTags_questionId_tagId_partial',
8+
unique: 'id',
9+
where: {
10+
deletedAt: null
11+
}
12+
13+
});
14+
15+
await queryInterface.removeConstraint('questionsTags', 'questionsTags_questionId_tagId_key').catch(() => {})
16+
17+
},
18+
19+
down: (queryInterface, Sequelize) => {
20+
}
21+
};

models/questions.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ module.exports = (sequelize, DataTypes) => {
2727
questions.hasMany(models.choices)
2828
questions.belongsTo(models.users,{foreignKey: 'updatedById'})
2929
questions.belongsToMany(models.quizzes, {through: models.quizQuestions})
30-
31-
30+
questions.belongsToMany(models.tags, {through: models.questionsTags})
3231
};
3332
return questions;
3433
};

models/questionsTags.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
module.exports = (sequelize, DataTypes) => {
3+
var questionsTags = sequelize.define('questionsTags', {
4+
id: {
5+
type: DataTypes.INTEGER,
6+
primaryKey: true,
7+
autoIncrement: true
8+
}
9+
}, {
10+
paranoid: true
11+
})
12+
13+
questionsTags.associate = function(model) {
14+
// Associations can be defined
15+
}
16+
17+
return questionsTags;
18+
}

models/tags.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
module.exports = (sequelize, DataTypes) => {
3+
var tags = sequelize.define('tags', {
4+
title: {
5+
type: DataTypes.STRING,
6+
allowNull: false
7+
}
8+
}, {
9+
paranoid: true
10+
});
11+
12+
tags.associate = function(models) {
13+
tags.belongsTo(models.users),
14+
tags.belongsToMany(models.questions, {through: models.questionsTags})
15+
}
16+
17+
return tags;
18+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"sq": "./node_modules/.bin/sequelize"
88
},
99
"dependencies": {
10-
"@coding-blocks/express-jsonapi-controller": "^1.0.0",
10+
"@coding-blocks/express-jsonapi-controller": "^1.0.4",
1111
"body-parser": "^1.18.3",
1212
"connect-ensure-login": "^0.1.1",
1313
"cookie-parser": "^1.4.3",

routes/api/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ const questions = require('./questions')
22
const choices = require('./choices')
33
const users = require('./users')
44
const quiz = require('./quiz')
5+
const tags = require('./tags')
56
const Router = require('express').Router()
67

78
Router.use('/questions', questions)
89
Router.use('/choices', choices)
910
Router.use('/users', users)
1011
Router.use('/quizzes', quiz)
11-
12+
Router.use('/tags', tags)
1213

1314
module.exports = Router

0 commit comments

Comments
 (0)