-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(article): implement article readtime
- Add wordsPerMinute column to the user model - Add number of words column to the article model - create function for calculating read time - add beforeValidate hook to calculate noOfWords in the article model - create get single article endpoint [Finishes #159987415]
- Loading branch information
1 parent
6c9af2f
commit 5b585bf
Showing
10 changed files
with
231 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import models from '../models'; | ||
|
||
const { Users } = models; | ||
/** | ||
* @description This method calculates the read of an article | ||
* @param {integer} userId The id of the user getting the article | ||
* @param {object} article The article object | ||
* @returns {string} read time of an article | ||
*/ | ||
const readTime = async (userId, article) => { | ||
// Number of words in the article | ||
const { noOfWords } = article.dataValues; | ||
const user = await Users.findOne({ where: { id: userId } }); | ||
// number of words user can read per minute | ||
const { wordsPerMinute } = user.dataValues; | ||
const time = Math.ceil(noOfWords / wordsPerMinute); | ||
return `${time} min read`; | ||
}; | ||
|
||
export default readTime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn('Users', 'wordsPerMinute', { | ||
type: Sequelize.INTEGER, | ||
defaultValue: 200 | ||
}), | ||
|
||
down: queryInterface => queryInterface.removeColumn('Users', 'wordsPerMinute') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn('Articles', 'noOfWords', { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}), | ||
|
||
down: queryInterface => queryInterface.removeColumn('Articles', 'noOfWords') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.