Skip to content

Commit

Permalink
Merge a110ddc into e69a1f9
Browse files Browse the repository at this point in the history
  • Loading branch information
Lundii committed Aug 7, 2019
2 parents e69a1f9 + a110ddc commit 09566cf
Show file tree
Hide file tree
Showing 28 changed files with 726 additions and 17 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"dotenv": "^6.0.0",
"errorhandler": "^1.5.0",
"express": "^4.16.3",
"faker": "^4.1.0",
"jsonwebtoken": "^8.3.0",
"lodash": "^4.17.15",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"node-cron": "^2.0.3",
Expand Down
18 changes: 15 additions & 3 deletions server/controllers/articleController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import uuid from 'uuid';
import sequelize from 'sequelize';
import models from '../db/models';
import helpers from '../helpers';
import utils from '../helpers/Utilities';

import Paginate from '../helpers/paginate';

const { Op } = sequelize;
const { paginateArticles } = Paginate;
const { querySearch, filterSearch } = helpers;
/**
* @Module ArticleController
* @description Controlls all activities related to Articles
Expand Down Expand Up @@ -47,11 +50,20 @@ class ArticleController {
* @memberof ArticleController
*/
static async getAllArticles(req, res) {
const { searchQuery } = req.query;
const queryFilters = req.body;
let articles;
const { page, limit } = req.query;
if (!page && !limit) {
const articles = await models.Article.findAll({
include: [{ model: models.User, as: 'author', attributes: ['firstname', 'lastname', 'username', 'image', 'email'] }]
});
if (!searchQuery) {
articles = await models.Article.findAll({
include: [{ model: models.User, as: 'author', attributes: ['firstname', 'lastname', 'username', 'image', 'email'] }]
});
} else if (searchQuery && Object.keys(queryFilters)[0] !== 'undefined') {
articles = await filterSearch(searchQuery, queryFilters);
} else {
articles = await querySearch(searchQuery);
}
return utils.successStat(res, 200, 'articles', articles);
}
paginateArticles(req, res);
Expand Down
29 changes: 29 additions & 0 deletions server/db/migrations/20190803192315-create-categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Categories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
description: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Categories')
};
29 changes: 29 additions & 0 deletions server/db/migrations/20190803192334-create-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Tags', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
description: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Tags')
};
27 changes: 27 additions & 0 deletions server/db/migrations/20190803192656-create-article-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('ArticleTags', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
articleId: {
type: Sequelize.INTEGER
},
tagId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('ArticleTags')
};
27 changes: 27 additions & 0 deletions server/db/migrations/20190803193836-create-article-categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('ArticleCategories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
articleId: {
type: Sequelize.INTEGER
},
categoryId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('ArticleCategories')
};
8 changes: 8 additions & 0 deletions server/db/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ module.exports = (sequelize, DataTypes) => {
Article.hasMany(models.Comment, {
foreignKey: 'articleId', onDelete: 'CASCADE', as: 'comment', hooks: true
});
Article.belongsToMany(models.Categories, {
through: 'ArticleCategories',
foreignKey: 'articleId'
});
Article.belongsToMany(models.Tags, {
through: 'ArticleTags',
foreignKey: 'articleId'
});
};
return Article;
};
14 changes: 14 additions & 0 deletions server/db/models/articlecategories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable func-names */

'use strict';

module.exports = (sequelize, DataTypes) => {
const ArticleCategories = sequelize.define('ArticleCategories', {
articleId: DataTypes.INTEGER,
categoryId: DataTypes.INTEGER
}, {});
ArticleCategories.associate = function () {
// associations can be defined here
};
return ArticleCategories;
};
14 changes: 14 additions & 0 deletions server/db/models/articletags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable func-names */

'use strict';

module.exports = (sequelize, DataTypes) => {
const ArticleTags = sequelize.define('ArticleTags', {
articleId: DataTypes.INTEGER,
tagId: DataTypes.INTEGER
}, {});
ArticleTags.associate = function () {
// associations can be defined here
};
return ArticleTags;
};
17 changes: 17 additions & 0 deletions server/db/models/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable func-names */

'use strict';

module.exports = (sequelize, DataTypes) => {
const Categories = sequelize.define('Categories', {
name: DataTypes.STRING,
description: DataTypes.STRING
}, {});
Categories.associate = function (models) {
Categories.belongsToMany(models.Article, {
through: 'ArticleCategories',
foreignKey: 'categoryId'
});
};
return Categories;
};
17 changes: 17 additions & 0 deletions server/db/models/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable func-names */

'use strict';

module.exports = (sequelize, DataTypes) => {
const Tags = sequelize.define('Tags', {
name: DataTypes.STRING,
description: DataTypes.STRING
}, {});
Tags.associate = function (models) {
Tags.belongsToMany(models.Article, {
through: 'ArticleTags',
foreignKey: 'tagId'
});
};
return Tags;
};
2 changes: 1 addition & 1 deletion server/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (sequelize, DataTypes) => {
hooks: true,
timestamps: false,
});
User.hasMany(models.Article, { as: 'authorId', foreignKey: 'authorId', onDelete: 'CASCADE' });
User.hasMany(models.Article, { foreignKey: 'id', onDelete: 'CASCADE' });
User.hasMany(models.Comment, { foreignKey: 'authorId', onDelete: 'CASCADE' });
};
return User;
Expand Down
28 changes: 28 additions & 0 deletions server/db/seeders/20190801215421-articles-seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const _ = require('lodash');
const faker = require('faker');

module.exports = {
up: (queryInterface) => {
const randomArticles = _.times(30, () => ({
title: faker.name.title(),
description: faker.lorem.sentence(),
articleBody: faker.lorem.sentence(),
uuid: faker.random.number({ max: '300' }),
slug: faker.lorem.words(),
image: faker.lorem.sentence(),
authorId: faker.random.number({
min: 1,
max: 12
}),
favorited: true,
favoriteCounts: 25,
createdAt: new Date(),
updatedAt: new Date()
}));
return queryInterface.bulkInsert('Articles', randomArticles, {});
},

down: queryInterface => queryInterface.bulkDelete('Articles', null, {})
};
20 changes: 20 additions & 0 deletions server/db/seeders/20190803194225-demo-categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const faker = require('faker');

const categories = ['technology', 'health', 'science', 'fashion', 'education', 'culture', 'lifestyle'];

module.exports = {
up: (queryInterface) => {
const randomCategories = categories.map(item => ({
name: item,
description: faker.lorem.sentence(),
createdAt: new Date(),
updatedAt: new Date()
}));

return queryInterface.bulkInsert('Categories', randomCategories, {});
},

down: queryInterface => queryInterface.bulkDelete('Categories', null, {})
};
25 changes: 25 additions & 0 deletions server/db/seeders/20190803194616-demo-articleCategories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const _ = require('lodash');
const faker = require('faker');

module.exports = {
up: (queryInterface) => {
const articleCategories = _.times(30, () => ({
articleId: faker.random.number({
min: 1,
max: 30
}),
categoryId: faker.random.number({
min: 1,
max: 7
}),
createdAt: new Date(),
updatedAt: new Date()
}));

return queryInterface.bulkInsert('ArticleCategories', articleCategories, {});
},

down: queryInterface => queryInterface.bulkDelete('ArticleCategories', null, {})
};
19 changes: 19 additions & 0 deletions server/db/seeders/20190805085916-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const faker = require('faker');

const tags = ['javascript', 'AI', 'travel', 'peace', 'believe', 'race', 'react', 'tutorial', 'knowledge'];
module.exports = {
up: (queryInterface) => {
const randomCategories = tags.map(item => ({
name: item,
description: faker.lorem.sentence(),
createdAt: new Date(),
updatedAt: new Date()
}));

return queryInterface.bulkInsert('Tags', randomCategories, {});
},

down: queryInterface => queryInterface.bulkDelete('Tags', null, {})
};
25 changes: 25 additions & 0 deletions server/db/seeders/20190805085934-articleTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const _ = require('lodash');
const faker = require('faker');

module.exports = {
up: (queryInterface) => {
const articleCategories = _.times(30, () => ({
articleId: faker.random.number({
min: 1,
max: 30
}),
tagId: faker.random.number({
min: 1,
max: 9
}),
createdAt: new Date(),
updatedAt: new Date()
}));

return queryInterface.bulkInsert('ArticleTags', articleCategories, {});
},

down: queryInterface => queryInterface.bulkDelete('ArticleTags', null, {})
};
Loading

0 comments on commit 09566cf

Please sign in to comment.