-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Users should see their reading stats)- Create User read stats mo…
…del- Create functionality for user to see amount of articles read- Write test for featurefinishes(162414288)
- Loading branch information
Makwe-O
committed
Jan 29, 2019
1 parent
5c32adf
commit 819c2fa
Showing
16 changed files
with
423 additions
and
101 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
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,99 @@ | ||
import db from '../models'; | ||
import response from '../helpers/response'; | ||
|
||
const { Article, ReadingStats } = db; | ||
|
||
/** | ||
* | ||
* @class ReadingStatsController | ||
*/ | ||
class ReadingStatsController { | ||
/** | ||
* | ||
* @description Method to get user reading stats | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} Json response | ||
* @memberof ReadingStatsController | ||
*/ | ||
static async getReadingStats(req, res) { | ||
try { | ||
const { userId } = req.user; | ||
const userStats = await ReadingStats.findAndCountAll({ | ||
where: { | ||
userId | ||
}, | ||
include: [ | ||
{ | ||
model: Article, | ||
attributes: ['slug', 'title', 'content', 'banner'] | ||
} | ||
] | ||
}); | ||
|
||
if (userStats.count === 0) { | ||
return response( | ||
res, | ||
404, | ||
'failure', | ||
"You currently don't have any reading stats" | ||
); | ||
} | ||
const payload = { | ||
readStats: userStats.rows, | ||
readStatsCount: userStats.count | ||
}; | ||
return response(res, 200, 'success', 'ReadingStats', null, payload); | ||
} catch (error) { | ||
return response( | ||
res, | ||
500, | ||
'failure', | ||
'Something went wrong on the server', | ||
null, | ||
null | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @description Method to create user reading stats | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {object} Json response | ||
* @memberof ReadingStatsController | ||
*/ | ||
static async createReadingStats(req, res) { | ||
try { | ||
const { userId } = req.user; | ||
const articleSlug = req.params; | ||
const theArticleSlug = JSON.stringify(articleSlug).split('"')[3]; | ||
const theArticle = await Article.findOne({ | ||
where: { slug: theArticleSlug } | ||
}); | ||
const articleId = theArticle.dataValues.id; | ||
|
||
ReadingStats.findOrCreate({ | ||
where: { userId, articleId }, | ||
attributes: ['id', 'articleId', 'userId'] | ||
}).spread((stats, created) => { | ||
if (created) { | ||
return stats; | ||
} | ||
}); | ||
} catch (error) { | ||
return response( | ||
res, | ||
500, | ||
'failure', | ||
'Something went wrong on the server', | ||
null, | ||
null | ||
); | ||
} | ||
} | ||
} | ||
export default ReadingStatsController; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.sequelize | ||
.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";') | ||
.then(() => { | ||
return queryInterface.createTable('ReadingStats', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()') | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false | ||
}, | ||
articleId: { | ||
type: Sequelize.UUID, | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
} | ||
}); | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('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,26 @@ | ||
export default (sequelize, DataTypes) => { | ||
const ReadingStats = sequelize.define( | ||
'ReadingStats', | ||
{ | ||
articleId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
}, | ||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
} | ||
}, | ||
{} | ||
); | ||
ReadingStats.associate = (models) => { | ||
const { User, Article } = models; | ||
ReadingStats.belongsTo(User, { | ||
foreignKey: 'userId' | ||
}); | ||
ReadingStats.belongsTo(Article, { | ||
foreignKey: 'articleId' | ||
}); | ||
}; | ||
return ReadingStats; | ||
}; |
Oops, something went wrong.