Skip to content

Commit

Permalink
[Feature #164489789] pagination support for article ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
mystere10 committed May 14, 2019
1 parent 963164b commit 5d3c34e
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 29 deletions.
9 changes: 6 additions & 3 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ module.exports = {
password: process.env.DBPASSWORD,
database: process.env.DBNAME,
host: process.env.DBHOST,
dialect: 'postgres'
dialect: 'postgres',
logging: false
},
test: {
username: process.env.DBUSERNAME,
password: process.env.DBPASSWORD,
database: process.env.TESTDBNAME,
host: process.env.DBHOST,
dialect: 'postgres'
dialect: 'postgres',
logging: false
},
production: {
username: process.env.DBUSERNAME,
password: process.env.DBPASSWORD,
database: process.env.DBNAME,
host: process.env.DBHOST,
dialect: 'postgres'
dialect: 'postgres',
logging: false
}
};
33 changes: 32 additions & 1 deletion controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,6 @@ class Article {
error: 'Article can not be found.'
});
}

const allArticles = await ratingModel.allRatings(UserModel, ArticleModel, slug);
const { rows } = allArticles;
if (rows.length === 0) {
Expand All @@ -720,5 +719,37 @@ class Article {
averageRating: objKey(Math.ceil(AvgRate))
});
}

/**
* @author Innocent Nkunzi
* @param {*} req
* @param {*} res
* @returns {object} it returns an object of articles
*/
static async articleRatingPagination(req, res) {
const pageNumber = parseInt(req.query.page, 10);
const limitRatings = parseInt(req.query.limit, 10);

if (pageNumber <= 0) {
return res.status(403).json({
error: 'Invalid page number'
});
}
if (limitRatings <= 0) {
return res.status(403).json({
error: 'Invalid limit'
});
}
const offset = limitRatings * (pageNumber - 1);
const listOfRatings = await ratingModel.paginateArticleRatings(limitRatings, offset);
if (listOfRatings.length) {
res.status(200).json({
articles: listOfRatings,
ratingCounts: listOfRatings.length
});
} else {
res.status(404).json({ error: 'No article found' });
}
}
}
export default Article;
12 changes: 0 additions & 12 deletions migrations/20190410060956-create-resetpassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ module.exports = {
token: {
type: Sequelize.STRING
},
userId: {
type: Sequelize.INTEGER,
allowNull: false
},
articleSlug: {
type: Sequelize.STRING,
allowNull: false
},
rating: {
type: Sequelize.INTEGER,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
Expand Down
4 changes: 0 additions & 4 deletions models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const ArticleModel = (sequelize, DataTypes) => {
Article.createArticle = article => Article.create(article);
Article.getAll = () => Article.findAll();
Article.getOneArticle = slug => Article.findOne({ where: { slug } });
<<<<<<< HEAD
Article.findArticleSlug = (authorid, slug) => Article.findOne({ where: { authorid, slug } });
Article.deleteArticle = slug => Article.destroy({ where: { id: slug } });
Article.getAllPages = (limit, offset) => Article.findAll({ limit, offset });
Expand All @@ -55,9 +54,6 @@ const ArticleModel = (sequelize, DataTypes) => {
return data;
};
Article.addViewer = id => Article.increment('views', { by: 1, where: { id } });
=======
Article.verifyArticle = slug => Article.findOne({ where: { slug } });
>>>>>>> get user from token

Article.associate = (models) => {
Article.belongsTo(models.user, {
Expand Down
6 changes: 1 addition & 5 deletions models/rating.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ module.exports = (sequelize, DataTypes) => {
}
],
});

rating.rateCheck = (rate, article, id) => rating.findOrCreate({ where: { userId: id, articleSlug: article }, defaults: { rating: rate } });
rating.addRate = (rate, article, id) => rating.findOrCreate({ where: { userId: id, articleSlug: article }, defaults: { rating: rate } });
rating.rateUpdate = (rateId, rate) => rating.update({ rating: rate }, { returning: true, where: { id: rateId } });
rating.avgFind = id => rating.findAll({ where: { articleId: id }, attributes: ['articleId', [sequelize.fn('AVG', sequelize.col('rating')), 'avgRating']], group: 'rating.articleId' });
rating.paginateArticleRatings = (limit, offset) => rating.findAll({ limit, offset });
rating.associate = (models) => {
rating.belongsTo(models.user, { foreignKey: 'userId', onDelete: 'CASCADE' });
rating.belongsTo(models.article, { foreignKey: 'articleSlug', onDelete: 'CASCADE' });
Expand Down
1 change: 1 addition & 0 deletions routes/api/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ router.post('/:slug/rate/:rate', AuthToken, articleController.rateArticle);
router.get('/:slug/rates', AuthToken, articleController.fetchArticleRating);
router.delete('/:slug', AuthToken, errorHandler(articleController.deleteArticle));
router.put('/:slug', AuthToken, errorHandler(articleController.updateArticle));
router.get('/rating/articles', errorHandler(articleController.articleRatingPagination));
router.patch(
'/:slug/:likeState',
Strategy.verifyToken,
Expand Down
Loading

0 comments on commit 5d3c34e

Please sign in to comment.