Skip to content

Commit

Permalink
Feature(articles): getting article reading time
Browse files Browse the repository at this point in the history
-showing time it takes to read an article

[Delivers #164489943]
  • Loading branch information
P.C. Ndayisenga authored and P.C. Ndayisenga committed Apr 15, 2019
1 parent 5abbf4b commit eef5cf9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
30 changes: 27 additions & 3 deletions controllers/article.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import models from '../models/index';
import readingTime from '../helpers/readingTime';

const Article = models.article;
/**
Expand Down Expand Up @@ -36,8 +37,20 @@ class ArticleController {
*/
static getArticle(req, res) {
Article.findAll()
.then(articles => res.status(200).json({ status: 200, articles }))
.catch(error => res.status(500).json({ error }));
.then((articles) => {
const articlesWithReadingTime = articles.map(article => ({
article_id: article.article_id,
title: article.title,
body: article.body,
taglist: article.taglist,
author: article.author,
image: article.image,
createdAt: article.createdAt,
updatedAt: article.updatedAt,
readingTime: readingTime(article.title + article.body)
}));
res.status(200).json({ status: 200, articles: articlesWithReadingTime });
}).catch(error => res.status(500).json({ error }));
}

/**
Expand Down Expand Up @@ -82,8 +95,19 @@ class ArticleController {
if (!article) {
return res.status(404).json({ error: 'Sorry the requested resource could not be found.' });
}
const articleWithReadingTime = {
article_id: article.article_id,
title: article.title,
body: article.body,
taglist: article.taglist,
author: article.author,
image: article.image,
createdAt: article.createdAt,
updatedAt: article.updatedAt,
readingTime: readingTime(article.title + article.body)
};
// @return article
return res.status(200).json({ status: 200, article });
return res.status(200).json({ status: 200, article: articleWithReadingTime });
})
.catch(error => res.status(500).json({ error: `Something wrong please try again later. ${error}` }));
}
Expand Down
13 changes: 13 additions & 0 deletions helpers/readingTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

const readingTime = (body) => {
const numberOfWords = body.split(' ').length;
const x = (numberOfWords / 265);
const minutes = Math.floor(x);
const seconds = Math.floor((x - Math.floor(x)) * 60);

const time = `${minutes} min ${seconds} sec`;

return time;
};

export default readingTime;
1 change: 1 addition & 0 deletions migrations/20190402140158-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const articleMigration = {
author: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'id'
Expand Down
2 changes: 2 additions & 0 deletions migrations/20190408131611-create-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ module.exports = {
articleId: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: {
model: 'articles', key: 'article_id'
}
},
author: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: {
model: 'users', key: 'id'
}
Expand Down
2 changes: 2 additions & 0 deletions migrations/20190409161017-create-rate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rateMigration = {
userId: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'id',
Expand All @@ -18,6 +19,7 @@ const rateMigration = {
articleId: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: {
model: 'articles',
key: 'article_id',
Expand Down
4 changes: 2 additions & 2 deletions models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const articleModel = (Sequelize, DataTypes) => {
foreignKey: 'articleId',
allowNull: false
});
Article.belongsTo(models.user, { as: 'authorfkey', foreignKey: 'author' });
Article.hasMany(models.rate, { foreignKey: 'articleId' });
Article.belongsTo(models.user, { as: 'authorfkey', foreignKey: 'author', onDelete: 'CASCADE' });
Article.hasMany(models.rate, { foreignKey: 'articleId', onDelete: 'CASCADE' });
};
return Article;
};
Expand Down
2 changes: 1 addition & 1 deletion testingdata/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"email": "blaise@gmail.com"
},
"googleValidToken": {
"access_token": "ya29.GlvpBnXYdrCRtsIchnu6i01ejs_52UwfyI6wztxXe-Fyu15M4kPOBS3V-sXYGIX89tLRKluMgGIZG_3AhO8rfHbSzs0JcHDhcuukpVdJTbopyWn7dnJ0ao2ZS1Vr"
"access_token": "ya29.GlvsBlWAUaPkHuyNToTZAIh2ZvPUz97D7-yXP-LyJ0A1SfYuw1dVkD9C1IoO67lKf5ZF5iUsPzIWQHscB-MCS4Z2InhC0LX8PhiWQX2an2fYI285q4IlmfmDGAXM"
},
"googleInvalidToken": {
"access_token": "ya29.GlvhBpzY2hl2ShgOMrpkni8obGgwyX0mr85Oendf2kmblu3BrRNTmYK2DVQiPciVOBFkLvR57YE90qDyffgJOqgzV68zutO3-Y9QDKooAPuxPvwsbsWM36wwVPHT"
Expand Down

0 comments on commit eef5cf9

Please sign in to comment.