Skip to content

Commit

Permalink
[#164198504] Fix husky violation and push
Browse files Browse the repository at this point in the history
  • Loading branch information
shaolinmkz committed Mar 5, 2019
2 parents 3d357e8 + 07b21af commit 99d23cf
Show file tree
Hide file tree
Showing 27 changed files with 743 additions and 119 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
}
},
"parser": "babel-eslint"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"sequelize": "./node_modules/.bin/babel-node ./node_modules/.bin/sequelize $*",
"migrate": "./node_modules/.bin/babel-node ./node_modules/.bin/sequelize db:migrate",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"reset:db": "npm run sequelize db:migrate:undo:all && npm run sequelize db:migrate",
"reset:seeds": "npm run sequelize db:seed:undo:all && npm run sequelize db:seed:all",
"reset:db": "npm run sequelize db:migrate:undo:all && npm run sequelize db:migrate && npm run reset:seeds",
"start:dev": "npm run reset:db && npm run dev",
"build": "rm -rf dist && mkdir dist && babel -d ./dist ./server -s",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm run build",
Expand Down
2 changes: 1 addition & 1 deletion server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ passport.use(new TwitterStrategy({
consumerSecret: twitterConsumerSecret,
callbackURL: twitterReturnUrl,
userProfileURL:
'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true',
'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true',
includeEmail: true,
proxy: trustProxy
},
Expand Down
75 changes: 49 additions & 26 deletions server/controllers/article.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import slugify from 'slug';
import { validationResult } from 'express-validator/check';
import response, { validationErrors } from '../utils/response';
import { HelperUtils, response } from '../utils';
import db from '../database/models';
import HelperUtils from '../utils';

const { Article, User } = db;
const { Article, Tag, User } = db;

/**
* @class ArticleController
Expand All @@ -16,6 +15,7 @@ class ArticleController {
constructor() {
this.defaultLimit = 20;
this.article = {};
this.tags = [];
}

/**
Expand All @@ -29,10 +29,12 @@ class ArticleController {
const errors = validationResult(req);
if (!errors.isEmpty()) {
response(res).badRequest({
errors: validationErrors(errors)
errors: response.validationErrors(errors)
});
} else {
const { title, description, body } = req.body;
const {
title, description, body, tagId
} = req.body;
let slug = slugify(title, {
lower: true
});
Expand All @@ -42,22 +44,22 @@ class ArticleController {
userId: req.user.id,
title,
description,
body
}))
.then((article) => {
slug = slug.concat(`-${article.id}`);
article.slug = slug;
body,
tagId
})).then((article) => {
slug = slug.concat(`-${article.id}`);
article.slug = slug;

// Append id to slug and update.
return Article.update({
slug
},
{
where: {
id: article.id
}
}).then(() => article);
})
// Append id to slug and update.
return Article.update({
slug
},
{
where: {
id: article.id
}
}).then(() => article);
})
.then((article) => {
article.userId = undefined;
const readTime = HelperUtils.estimateReadingTime(article.body);
Expand All @@ -70,6 +72,26 @@ class ArticleController {
}
}

/**
* Returns all tags
* @method getTags
* @param {object} req The request object
* @param {object} res The response object
* @returns {null} - Returns nothing
*/
async getTags(req, res) {
try {
this.tags = await Tag.findAll({ where: {} });
response(res).success({
tags: this.tags
});
} catch (err) {
response(res).serverError({
message: 'Could not get all tags'
});
}
}

/**
* @method getAll
* @param {object} req The request object from the route
Expand All @@ -92,12 +114,13 @@ class ArticleController {
where: {},
offset, // Default is page 1
limit,
include: [
{
model: User,
attributes: ['username', 'bio', 'image']
}
],
include: [{
model: User,
attributes: ['username', 'bio', 'image'],
}, {
model: Tag,
attributes: ['name']
}],
attributes: [
'id',
'slug',
Expand Down
3 changes: 1 addition & 2 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import '@babel/polyfill';
import dotenv from 'dotenv';
import db from '../database/models';
import HelperUtils from '../utils';
import response from '../utils/response';
import { HelperUtils, response } from '../utils';
import verifyEmailMarkup from '../utils/markups/emailVerificationMarkup';
import passwordResetMarkup from '../utils/markups/passwordResetMarkup';

Expand Down
26 changes: 26 additions & 0 deletions server/database/migrations/20190103185917-create-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
up(queryInterface, Sequelize) {
return queryInterface.createTable('Tags', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date().getTime(),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: new Date().getTime(),
}
});
},
down: queryInterface => queryInterface.dropTable('Tags')
};
8 changes: 8 additions & 0 deletions server/database/migrations/20190227133849-create-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export default {
slug: {
type: Sequelize.STRING,
},
tagId: {
type: Sequelize.INTEGER,
references: {
model: 'Tags',
key: 'id',
as: 'tagId'
},
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export default {
up(queryInterface, Sequelize) {
return queryInterface.createTable('ArticleComments', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
articleId: {
type: Sequelize.INTEGER,
allowNull: false,
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
},
comment: {
type: Sequelize.STRING,
allowNull: false,
},
totalLikes: {
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
}
});
},
down: queryInterface => queryInterface.dropTable('ArticleComments')
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default {
up(queryInterface, Sequelize) {
return queryInterface.createTable('ArticleCommentLikes', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
articleId: {
type: Sequelize.INTEGER,
allowNull: false,
},
commentId: {
type: Sequelize.INTEGER,
allowNull: false,
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
}
});
},
down: queryInterface => queryInterface.dropTable('ArticleCommentLikes')
};
8 changes: 8 additions & 0 deletions server/database/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ export default (sequelize, DataTypes) => {
primaryImageUrl: DataTypes.STRING,
totalClaps: DataTypes.INTEGER,
slug: DataTypes.STRING,
tagId: DataTypes.INTEGER
}, {});
Article.associate = (models) => {
// associations can be defined here
Article.belongsTo(models.Tag, {
foreignKey: 'tagId',
});
Article.belongsTo(models.User, {
foreignKey: 'userId',
});
Article.hasMany(models.ArticleComment, {
foreignKey: 'id',
});
};
return Article;
};
17 changes: 17 additions & 0 deletions server/database/models/articlecomment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default (sequelize, DataTypes) => {
const ArticleComment = sequelize.define('ArticleComment', {
articleId: DataTypes.INTEGER,
userId: DataTypes.INTEGER,
comment: DataTypes.STRING,
totalLikes: DataTypes.INTEGER
}, {});
ArticleComment.associate = (models) => {
ArticleComment.belongsTo(models.User, {
foreignKey: 'userId',
});
ArticleComment.belongsTo(models.Article, {
foreignKey: 'articleId',
});
};
return ArticleComment;
};
16 changes: 16 additions & 0 deletions server/database/models/articlecommentlike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default (sequelize, DataTypes) => {
const ArticleCommentLike = sequelize.define('ArticleCommentLike', {
articleId: DataTypes.INTEGER,
commentId: DataTypes.INTEGER,
userId: DataTypes.INTEGER
}, {});
ArticleCommentLike.associate = (models) => {
ArticleCommentLike.belongsTo(models.User, {
foreignKey: 'userId',
});
ArticleCommentLike.belongsTo(models.ArticleComment, {
foreignKey: 'commentId',
});
};
return ArticleCommentLike;
};
13 changes: 13 additions & 0 deletions server/database/models/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default (sequelize, DataTypes) => {
const Tag = sequelize.define('Tag', {
name: DataTypes.STRING
}, {});
Tag.associate = (models) => {
// associations can be defined here
Tag.hasMany(models.Article, {
foreingKey: 'tagId',
as: 'articles'
});
};
return Tag;
};
7 changes: 7 additions & 0 deletions server/database/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ export default (sequelize, DataTypes) => {
as: 'followers'
});

// Relations for articles.
User.hasMany(models.Article, {
foreignKey: 'id',
});
User.hasMany(models.ArticleComment, {
foreignKey: 'id',
});
User.hasMany(models.ArticleCommentLike, {
foreignKey: 'id',
});
};
return User;
};
19 changes: 19 additions & 0 deletions server/database/seeders/20190303192902-add-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
up(queryInterface) {
return queryInterface.bulkInsert('Tags', [{
name: 'Food'
}, {
name: 'Technology'
}, {
name: 'Art'
}, {
name: 'Finance'
}, {
name: 'Health'
}], {});
},

down(queryInterface) {
return queryInterface.bulkDelete('Tags', null, {});
}
};
3 changes: 1 addition & 2 deletions server/middlewares/AuthenticateUser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import HelperUtils from '../utils/HelperUtils';
import response from '../utils/response';
import { response, HelperUtils } from '../utils';

/**
* @class AuthenticateUser
Expand Down
Loading

0 comments on commit 99d23cf

Please sign in to comment.