Skip to content

Commit

Permalink
Merge branch 'ft-pagination-for-articles-ratings-166816108' of https:…
Browse files Browse the repository at this point in the history
…//github.com/andela/persephone-ah-backend into ft-pagination-for-articles-ratings-166816108
  • Loading branch information
Halimah Oladosu authored and Halimah Oladosu committed Jul 21, 2019
2 parents 6f9e51e + 63c47e6 commit 840f17a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/controllers/article.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,19 @@ export default {
let { articleId } = request.params;
articleId = parseInt(articleId, 10);

const result = await fetchRatingsService(articleId);

const result = await fetchRatingsService(articleId, request, response);
if (result === `Article with id: ${articleId} does not exist`) {
return Helper.failResponse(response, 404, result);
}

if (result.length < 1) {
return Helper.successResponse(
response,
200,
'specified article does not have any ratings'
);
}

return Helper.successResponse(response, 200, result);
}
};
2 changes: 2 additions & 0 deletions src/routes/v1/article.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ router
authorization.verifyToken,
validator('fetchRating'),
checkValidationResult,
paginationValidator()('pagination'),
ValidationResult,
articleController.fetchRatings
)
.post(
Expand Down
20 changes: 17 additions & 3 deletions src/services/article.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,30 @@ export const articleRatingsService = async (articleId, rating, userId) => {
* @param {integer} articleId
*/

export const fetchRatingsService = async articleId => {
export const fetchRatingsService = async (articleId, queryParams) => {
const articleExist = await Article.findByPk(articleId);

if (!articleExist) return `Article with id: ${articleId} does not exist`;
const { limit, offset } = paginationQueryMetadata(
queryParams.query.page,
queryParams.query.limit
);

const ratings = await Rating.findAll({
const ratings = await Rating.findAndCountAll({
where: { articleId },
limit,
offset,
attributes: {
exclude: ['createdAt', 'updatedAt']
}
});
return ratings;

const pageResponse = pageMetadata(
queryParams.query.page,
queryParams.query.limit,
ratings.count,
`/articles/${articleId}/ratings`
);
const allRatings = ratings.rows;
return { pageResponse, allRatings };
};
32 changes: 30 additions & 2 deletions src/tests/controller/article.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,10 +1169,9 @@ describe('Article API endpoints', () => {
.request(app)
.get(`${API_VERSION}/articles/${article.id}/ratings`)
.set({ Authorization: `Bearer ${userToken}` });

expect(response).to.have.status(200);
expect(response.body.status).to.equal('success');
expect(response.body.data[0].articleId).to.equal(article.id);
expect(response.body.data.pageResponse.totalItems).to.equal(1);
});

it('should return error for article id that is less than 1', async () => {
Expand Down Expand Up @@ -1210,5 +1209,34 @@ describe('Article API endpoints', () => {
'No token provided, you do not have access to this page'
);
});
it('Should use deafult vaules for page and query if they are set to zero', async () => {
const response = await chai
.request(app)
.get(`${API_VERSION}/articles/${article.id}/ratings?page=0&limit=0`)
.set({ Authorization: `Bearer ${userToken}` });
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('success');
expect(response.body.data).to.have.property('allRatings');
});
it('Should return error if invalid query', async () => {
const response = await chai
.request(app)
.get(`${API_VERSION}/articles/${article.id}/ratings?page=a&limit=a`)
.set({ Authorization: `Bearer ${userToken}` });
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('fail');
expect(response.body.data[0].msg).to.equal('Page must be a number');
});
it('Should use return thispage is empty if page given in query does not exist', async () => {
const response = await chai
.request(app)
.get(`${API_VERSION}/articles/${article.id}/ratings?page=20&limit=10`)
.set({ Authorization: `Bearer ${userToken}` });
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('success');
expect(response.body.data.pageResponse).to.equal(
'No articles on this page'
);
});
});
});

0 comments on commit 840f17a

Please sign in to comment.