Skip to content

Commit

Permalink
feat(Rating):Pagination for article ratings
Browse files Browse the repository at this point in the history
Paginate article ratings

[Delivers #166841018]
  • Loading branch information
chokonaira committed Jul 15, 2019
1 parent f5bd4ff commit 9cd13ba
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/controllers/ratingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,50 @@ class RatingController {
});
}
}

/**
*
* @returns { object } res
* @description - paginate article ratings
* @static
* @param {*} req
* @param {*} res
* @memberof RatingController
*/
static async getAllArticlesRating(req, res){
const {article} = res.locals;
const articleId = article.id
try{
let offset = 0;
const { currentPage, limit } = req.query; // page number
const defaultLimit = limit || 3; // number of records per page
const defaultPage = currentPage || 1;

const { count, rows: rating } = await Rating.findAndCountAll({
where: { articleId },
raw: true,
attributes: { exclude: ['id', 'updatedAt', 'articleId']},
limit: defaultLimit,
offset
});

const pages = Math.ceil(count / limit) || 1;
offset = limit * (defaultPage - 1);

return res.status(200).json({
status: 200,
message: 'All Ratings fetched successfully',
article,
rating,
pages
});

} catch(err){
return res.status(500).json({
status: 500,
message: 'Internal server error'
})
}
}
}
export default RatingController;
5 changes: 5 additions & 0 deletions src/db/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ module.exports = (sequelize, DataTypes) => {
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Article.hasMany(models.Rating, {
foreignKey: 'articleId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Article;
};
1 change: 0 additions & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ router.use('/api/v1/user', userRoute);
router.use('/api/v1/', articleRoute);
router.use('/api/v1', userRoute);
router.use('/api/v1/articles', commentRoute);
router.use('/api/v1/article', commentRoute);
router.use('/api/v1', ratingRoute);

export default router;
1 change: 1 addition & 0 deletions src/routes/ratingRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import ratingChecker from '../middleware/validators/ratingValidation';
const router = express.Router();

router.post('/articles/rate/:slug', verify, ratingChecker, findItem.findArticle, RatingController.rateArticle);
router.get('/articles/rating/:slug', verify, findItem.findArticle, RatingController.getAllArticlesRating);

export default router;
18 changes: 18 additions & 0 deletions test/rating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,22 @@ describe('RatingController', () => {
});
});
});
it('should get all ratings for a particular article', (done) => {
chai.request(app).post('/api/v1/auth/login')
.send(user)
.end((err, res) => {
testToken = res.body.token;
chai.request(app).get('/api/v1/articles/rating/article?page=3&limit=2')
.set('token', testToken)
.end((err, res) => {
res.should.have.status(200);
expect(res.body).to.have.property('article');
expect(res.body.rating[0]).to.have.property('value');
expect(res.body.rating[0]).to.have.property('createdAt');
expect(res.body.rating[0]).to.have.property('userId');
expect(res.body.message).equal('All Ratings fetched successfully');
done();
});
});
});
});

0 comments on commit 9cd13ba

Please sign in to comment.