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 a74c804
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 4 deletions.
4 changes: 3 additions & 1 deletion 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 @@ -82,8 +83,9 @@ class ArticleController {
if (!article) {
return res.status(404).json({ error: 'Sorry the requested resource could not be found.' });
}
const time = readingTime(article.title + article.body);
// @return article
return res.status(200).json({ status: 200, article });
return res.status(200).json({ status: 200, article, readingTime: `${time} min` });
})
.catch(error => res.status(500).json({ error: `Something wrong please try again later. ${error}` }));
}
Expand Down
16 changes: 16 additions & 0 deletions helpers/readingTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

const readingTime = (body) => {
let time;
const numberOfWords = body.split(' ').length;
const minutes = (numberOfWords / 265).toFixed(2);
const seconds = minutes - Math.floor(minutes);
if (seconds >= 0.30) {
time = Math.floor(minutes) + 1;
} else {
time = Math.floor(minutes);
}

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.GlzsBkXVGjsECcatNR04ApJ72Jf96hzq7N5dJ0vOI3Nr1ZWFIRZFyBOETI3HyVs7uuS5Xn16VCrH39PTw8_E0DTLbfpvN58XWiGOL8DSoamHLMohkdY_WgwzGJ_Hpg"
},
"googleInvalidToken": {
"access_token": "ya29.GlvhBpzY2hl2ShgOMrpkni8obGgwyX0mr85Oendf2kmblu3BrRNTmYK2DVQiPciVOBFkLvR57YE90qDyffgJOqgzV68zutO3-Y9QDKooAPuxPvwsbsWM36wwVPHT"
Expand Down

0 comments on commit a74c804

Please sign in to comment.