-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(users): users see their reading stats
- record user reading - get user reading stats [Finishes #166790015]
- Loading branch information
Herve Nkurikiyimfura
authored and
Herve Nkurikiyimfura
committed
Jul 31, 2019
1 parent
73ec4cf
commit 02dfcab
Showing
13 changed files
with
270 additions
and
25 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,71 @@ | ||
import models from '../models/index'; | ||
|
||
const { articleStats, users, articles } = models; | ||
|
||
/** | ||
* Reading stats controller | ||
* @exports | ||
* @class | ||
*/ | ||
class ReadingStats { | ||
/** | ||
* Record user reading stats | ||
* @param {object} req - user's request | ||
* @param {object} res - response | ||
* @return {object} response | ||
*/ | ||
static async recordUserReading(req, res) { | ||
try { | ||
const authUser = await users.findOne({ where: { email: req.decoded.email } }); | ||
const { articleSlug } = req.params; | ||
const [result, created] = await articleStats.findOrCreate({ | ||
where: { userId: authUser.id, articleSlug }, defaults: { numberOfReading: 1 } | ||
}); | ||
if (!created) { | ||
await articleStats.update( | ||
{ numberOfReading: result.numberOfReading + 1 }, | ||
{ where: { id: result.id }, returning: true } | ||
); | ||
} | ||
const getArticle = await articles.findOne({ where: { slug: articleSlug } }); | ||
res.status(201).send({ | ||
message: 'You are reading the article', | ||
article: getArticle.title | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Failed to record reading' }); | ||
} | ||
} | ||
|
||
/** | ||
* Get user reading stats | ||
* @param {object} req - user's request | ||
* @param {object} res - response | ||
* @return {object} response | ||
*/ | ||
static async getUserReadingStats(req, res) { | ||
try { | ||
const authUser = await users.findOne({ where: { email: req.decoded.email } }); | ||
const totalUserReadingStats = await articleStats.count({ where: { userId: authUser.id } }); | ||
const articlesRead = await articleStats.findAll({ | ||
where: { userId: authUser.id }, | ||
attributes: [ | ||
['numberOfReading', 'totalArticleRead'], | ||
['updatedAt', 'lastTime']], | ||
include: [ | ||
{ | ||
model: articles, | ||
attributes: ['title', 'slug', 'body'], | ||
} | ||
] | ||
}); | ||
if (totalUserReadingStats) { | ||
return res.status(200).send({ totalArticlesRead: totalUserReadingStats, articlesRead }); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Failed to get user reading stats' }); | ||
} | ||
} | ||
} | ||
|
||
export default ReadingStats; |
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,45 @@ | ||
const articleStatsMigration = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('articleStats', { | ||
id: { | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4, | ||
allowNull: false, | ||
primaryKey: true | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE' | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
references: { | ||
model: 'articles', | ||
key: 'slug' | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE' | ||
}, | ||
numberOfReading: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('articleStats') | ||
}; | ||
|
||
export default articleStatsMigration; |
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,38 @@ | ||
const articleStatsModel = (sequelize, DataTypes) => { | ||
const ArticleStats = sequelize.define('articleStats', { | ||
id: { | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
allowNull: false, | ||
primaryKey: true, | ||
}, | ||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
}, | ||
articleSlug: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'articles', | ||
key: 'slug' | ||
}, | ||
}, | ||
numberOfReading: { type: DataTypes.INTEGER } | ||
}, {}); | ||
ArticleStats.associate = (models) => { | ||
ArticleStats.belongsTo(models.users, { foreignKey: 'userId', onDelete: 'CASCADE' }); | ||
ArticleStats.belongsTo(models.articles, { foreignKey: 'articleSlug', onDelete: 'CASCADE', targetKey: 'slug' }); | ||
}; | ||
return ArticleStats; | ||
}; | ||
|
||
export default articleStatsModel; |
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
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
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
Oops, something went wrong.