Skip to content

Commit

Permalink
Merge c77f6ed into ef1e519
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beloved committed Sep 8, 2018
2 parents ef1e519 + c77f6ed commit c66470f
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PROD_DB_DIALECT=postgres
USER_PASSWORD=
ADMIN_PASSWORD=

JWT_VAL=
JWT_KEY=

FACEBOOK_APP_ID=
FACEBOOK_APP_SECRET=
Expand Down
2 changes: 2 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fail_on_violations: true

scss:
enabled: false
config_file: .scss-lint.yml
Expand Down
17 changes: 13 additions & 4 deletions server/controllers/article.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import db from '../models';
import articleValidation from '../utils/articles';
import paginateArticle from '../utils/articlesPaginate';

const { Article, User, Tag } = db;

Expand Down Expand Up @@ -110,8 +111,12 @@ class ArticleController {
* @return {json} res
* @description returns all article.
*/
static getAll(req, res) {
return Article.findAll({
static getAll({ query }, res) {
const limit = Number(query.limit) || 4;
const currentPage = Number(query.page) || 1;
const offset = (currentPage - 1) * limit;

return Article.findAndCountAll({
include: [{
model: User,
as: 'author',
Expand All @@ -124,11 +129,15 @@ class ArticleController {
attributes: [],
},
}],
attributes: ['slug', 'title', 'description', 'body', 'createdAt', 'updatedAt']
attributes: ['slug', 'title', 'description', 'body', 'createdAt', 'updatedAt', 'authorId'],
limit,
offset
})
.then((article) => {
const pagination = paginateArticle(article, currentPage, limit);
res.status(200).json({
articles: article
pagination,
articles: article.rows
});
})
.catch(error => res.status(500).json(error));
Expand Down
7 changes: 2 additions & 5 deletions server/controllers/socialAuth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import jwt from 'jsonwebtoken';
import { User } from '../models';

const secretKey = process.env.JWT_KEY;
import TokenHelper from '../utils/TokenHelper';


/**
Expand Down Expand Up @@ -47,8 +45,7 @@ class SocialAuthController {
firstName: req.user.firstName,
image: req.user.image
};
const { email } = user;
user.token = jwt.sign({ email }, secretKey, { expiresIn: '24h' });
user.token = TokenHelper.generateToken(user);
if (req.user.created) {
return res.status(201).send({ message: 'you have successfully signed up', user });
}
Expand Down
51 changes: 23 additions & 28 deletions server/migrations/20180906045430-create-user-follow.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserFollows', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER
},
followerId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserFollows');
}
up: (queryInterface, Sequelize) => queryInterface.createTable('UserFollows', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER
},
followerId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
}),
down: queryInterface => queryInterface.dropTable('UserFollows'),
};
37 changes: 37 additions & 0 deletions server/seeders/articleSeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

module.exports = {
up: queryInterface => queryInterface.bulkInsert(
'Articles',
[
{
authorId: 1,
title: 'jbjkka2 kbviu buibi updated',
body: 'ibin update1 cc',
description: 'updfated kvilbulibvi',
slug: 'gfvegybjcnwudj',
createdAt: new Date(),
updatedAt: new Date(),
},
{
authorId: 1,
title: 'jbjkka2 kbviu buibi updated',
body: 'ibin update1 cc',
description: 'updfated kvilbulibvi',
slug: 'ygehjnec',
createdAt: new Date(),
updatedAt: new Date(),
},
{
authorId: 1,
title: 'jbjkka2 kbviu buibi updated',
body: 'ibin update1 cc',
description: 'updfated kvilbulibvi',
slug: 'evbenjkve',
createdAt: new Date(),
updatedAt: new Date(),
}
], {}
),

down: queryInterface => queryInterface.bulkDelete('Articles', null, {})
};
27 changes: 27 additions & 0 deletions server/seeders/articleTagSeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
up: queryInterface => queryInterface.bulkInsert(
'ArticleTags',
[
{
tagId: 1,
articleId: 1,
createdAt: new Date(),
updatedAt: new Date()
},
{
tagId: 1,
articleId: 2,
createdAt: new Date(),
updatedAt: new Date()
},
{
tagId: 1,
articleId: 3,
createdAt: new Date(),
updatedAt: new Date()
}
], {}
),

down: queryInterface => queryInterface.bulkDelete('ArticleTags', null, {})
};
14 changes: 14 additions & 0 deletions server/seeders/tagSeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
up: queryInterface => queryInterface.bulkInsert(
'Tags',
[
{
tag: 'walker',
createdAt: new Date(),
updatedAt: new Date()
}
], {}
),

down: queryInterface => queryInterface.bulkDelete('Tags', null, {})
};
18 changes: 14 additions & 4 deletions server/tests/controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const author2Login = {
const Article = {
title: 'jbjkka2 kbviu buibi',
body: 'ibin',
description: 'kvilbulibvi',
description: 'kvilbulibvi'
};

const Article2 = {
Expand Down Expand Up @@ -220,14 +220,24 @@ describe('Articles controller', () => {
});
});

describe('getAllArticle()', () => {
it('should return a list of articles', (done) => {
describe('getAllArticle(s)', () => {
it('should return a list of articles, 4 per page when no limit is specified', (done) => {
chai.request(server)
.get('/api/articles')
.set('Authorization', `Bearer ${token1}`)
.end((err, res) => {
res.should.have.status(200);
res.body.articles.should.be.an('array');
res.body.articles.should.be.an('Array');
done();
});
});
it('should return a list of articles on the next page given a page limit', (done) => {
chai.request(server)
.get('/api/articles/?page=2&limit=4')
.set('Authorization', `Bearer ${token1}`)
.end((err, res) => {
res.should.have.status(200);
res.body.articles.should.be.an('Array');
done();
});
});
Expand Down
31 changes: 31 additions & 0 deletions server/utils/articlesPaginate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @description - Paginates the articles collections
*
* @param {object} articles - articles details
*
* @param {Number} currentPage - Current page
*
* @param {Number} limit - Page limit
*
* @returns {object} pagination - Pagination object
*/

const paginateArticle = ({
count = 0, rows = []
}, currentPage, limit) => {
const totalRecords = count;
const totalPages = Math.ceil(totalRecords / limit);
const newArticles = Object.assign(
{
},
{
currentPage,
currentPageSize: rows.length,
totalPages,
totalRecords
}
);
return newArticles;
};

export default paginateArticle;

0 comments on commit c66470f

Please sign in to comment.