Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#164489943 getting article reading time #34

Merged
merged 1 commit into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 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,13 @@ 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.dataValues.readingTime = readingTime(article.title + article.body);
return article;
});
res.status(200).json({ status: 200, articles: articlesWithReadingTime });
}).catch(error => res.status(500).json({ error }));
}

/**
Expand Down Expand Up @@ -82,6 +88,7 @@ class ArticleController {
if (!article) {
return res.status(404).json({ error: 'Sorry the requested resource could not be found.' });
}
article.dataValues.readingTime = readingTime(article.title + article.body);
// @return article
return res.status(200).json({ status: 200, article });
})
Expand Down
19 changes: 19 additions & 0 deletions helpers/readingTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

const readingTime = (body) => {
const numberOfWords = body.split(' ').length;
const x = (numberOfWords / 265);
const initialMinutes = Math.floor(x);
const seconds = Math.floor((x - Math.floor(x)) * 60);
let finalMinutes;
if (initialMinutes > 0 && seconds >= 30) {
finalMinutes = initialMinutes + 1;
}
if (initialMinutes > 0 && seconds < 30) {
finalMinutes = initialMinutes;
}
const time = initialMinutes > 0 ? `${finalMinutes} 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.GlvtBh1zpj-_QCVaZhyOkXLtLDG-s5vawM1FWGnnhtkoV3lMMile89zb-lLNmadfvzWWNbVdPyaRTMlzQin8NliZF2cOo-k6oAVkPCxmEANhgkYTsW14acaFnw0r"
},
"googleInvalidToken": {
"access_token": "ya29.GlvhBpzY2hl2ShgOMrpkni8obGgwyX0mr85Oendf2kmblu3BrRNTmYK2DVQiPciVOBFkLvR57YE90qDyffgJOqgzV68zutO3-Y9QDKooAPuxPvwsbsWM36wwVPHT"
Expand Down