-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reading-stats): users should be able to see their reading stats
- create an endpoint to enable user to create and retrieve stats - unit test and swagger documentation of the feature [Finishes #166789894]
- Loading branch information
Showing
9 changed files
with
297 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import model from '../db/models/index'; | ||
|
||
const { Articles, Statistics } = model; | ||
/** | ||
* statistic controller | ||
*/ | ||
class StatisticManager { | ||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} stats | ||
*/ | ||
static async createStats(req, res) { | ||
const { id } = req.user; | ||
const { slug } = req.params; | ||
const findArticle = await Articles.findOne({ where: { slug } }); | ||
if (!findArticle) return res.status(200).json({ error: 'No article found' }); | ||
const [result, created] = await Statistics.findOrCreate({ | ||
where: { userId: id, articleId: findArticle.id }, defaults: { numberOfReading: 1 } | ||
}); | ||
if (!created) { | ||
await Statistics.update( | ||
{ numberOfReading: result.numberOfReading + 1 }, | ||
{ where: { id: result.id }, returning: true } | ||
); | ||
} | ||
return res.status(201).json({ | ||
message: 'I am reading an article...', | ||
On: result.updatedAt, | ||
article: findArticle.title | ||
}); | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} retrieve all user stats | ||
*/ | ||
static async retrieveUserReadingStats(req, res) { | ||
const { id } = req.user; | ||
const countTotalArticlesRead = await Statistics.count({ where: { userId: id } }); | ||
const articlesRead = await Statistics.findAll({ | ||
where: { userId: id }, | ||
attributes: [ | ||
['numberOfReading', 'TimesArticleRead'], | ||
['updatedAt', 'lastSeen']], | ||
include: [ | ||
{ | ||
model: Articles, | ||
attributes: ['title', 'slug', 'body'], | ||
} | ||
] | ||
}); | ||
if (countTotalArticlesRead) { | ||
return res.status(200).json({ allArticlesRead: countTotalArticlesRead, articlesRead }); | ||
} | ||
return res.status(200).json({ allArticlesRead: 0, articlesRead }); | ||
} | ||
} | ||
export default StatisticManager; |
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,47 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Statistics', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}, | ||
numberOfReading: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Statistics'); | ||
} | ||
}; |
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,33 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Statistics = sequelize.define('Statistics', { | ||
id: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
defaultValue: DataTypes.UUIDV4 | ||
}, | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: true, | ||
}, | ||
articleId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: true | ||
}, | ||
numberOfReading: { | ||
type: DataTypes.INTEGER, | ||
allowNull: true, | ||
defaultValue: 0 | ||
} | ||
}, {}); | ||
Statistics.associate = function(models) { | ||
Statistics.belongsTo(models.Users, { | ||
foreignKey: 'userId' | ||
}); | ||
Statistics.belongsTo(models.Articles, { | ||
foreignKey: 'articleId' | ||
}); | ||
}; | ||
return Statistics; | ||
}; |
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.