Skip to content

Commit

Permalink
Merge 04cc7c6 into 60e7b73
Browse files Browse the repository at this point in the history
  • Loading branch information
halimahO committed Jul 23, 2019
2 parents 60e7b73 + 04cc7c6 commit 393b7b2
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/controllers/article.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ 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);
Expand Down Expand Up @@ -413,5 +413,15 @@ export default {
});
}
if (result) return Helper.successResponse(response, 200, 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: 1 addition & 1 deletion src/helpers/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const pageMetadata = (page = 1, limit = 10, totalItems, entity = '') => {
1}&limit=${pageLimit}`
: null;
if (page > totalPages) {
return 'No articles on this page';
return 'No content on this page';
}
return {
totalItems,
Expand Down
6 changes: 5 additions & 1 deletion src/routes/v1/article.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ router
verifyToken,
validator('commentLike'),
checkValidationResult,
likeComment
likeComment,
authorization.verifyToken,
validator('fetchRating'),
checkValidationResult,
articleController.fetchRatings
);

router.get(
Expand Down
21 changes: 17 additions & 4 deletions src/services/article.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,29 @@ export const likeCommentService = async (slug, commentId, 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 ratings = await Rating.findAll({
const { limit, offset } = paginationQueryMetadata(
queryParams.query.page,
queryParams.query.limit
);

const ratings = await Rating.findAndCountAll({
limit,
offset,
where: { articleId },
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 };
};
79 changes: 73 additions & 6 deletions src/tests/controller/article.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,56 @@ describe('Article API endpoints', () => {
});
});

it('Should successfully create an article', done => {
chai
.request(app)
.post(`${process.env.API_VERSION}/articles`)
.set({ Authorization: `Bearer ${userToken}` })
.field('title', 'first article')
.field('description', 'this is a description')
.field('body', 'this is a description this is a description')
.end((error, response) => {
expect(response.status).to.equal(201);
expect(response).to.be.an('Object');
expect(response.body).to.have.property('status');
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('success');
expect(response.body.data.title).to.equal('first article');
expect(response.body.data.description).to.equal(
'this is a description'
);
expect(response.body.data.body).to.equal(
'this is a description this is a description'
);
done();
});
});

it('Should successfully create an article', done => {
chai
.request(app)
.post(`${process.env.API_VERSION}/articles`)
.set({ Authorization: `Bearer ${userToken}` })
.field('title', 'first article')
.field('description', 'this is a description')
.field('body', 'this is a description this is a description')
.end((error, response) => {
expect(response.status).to.equal(201);
expect(response).to.be.an('Object');
expect(response.body).to.have.property('status');
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('success');
expect(response.body.data.title).to.equal('first article');
expect(response.body.data.description).to.equal(
'this is a description'
);
expect(response.body.data.body).to.equal(
'this is a description this is a description'
);
done();
});
});

it('Should successfully create an article', done => {
chai
.request(app)
Expand Down Expand Up @@ -617,17 +667,37 @@ describe('Article API endpoints', () => {
});
});
describe('GET single article /articles/:slug', () => {
it('Should successfully publish articles by user', done => {
chai
.request(app)
.put(
`${process.env.API_VERSION}/articles/publish/${secondArticle.slug}`
)
.set({ Authorization: `Bearer ${userToken}` })
.end((error, response) => {
expect(response.status).to.equal(200);
expect(response).to.be.an('Object');
expect(response.body).to.have.property('status');
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('success');
expect(response.body.data.message).to.equal(
'Article published successfully'
);
done();
});
});

it('Should successfully fetch a single article', done => {
chai
.request(app)
.get(`${process.env.API_VERSION}/articles/${createdArticle.slug}`)
.get(`${process.env.API_VERSION}/articles/${secondArticle.slug}`)
.end((error, response) => {
expect(response.status).to.equal(200);
expect(response).to.be.an('Object');
expect(response.body).to.have.property('status');
expect(response.body).to.have.property('data');
expect(response.body.status).to.equal('success');
expect(response.body.data.title).to.equal('first article');
expect(response.body.data.title).to.equal('new article');
expect(response.body.data.description).to.equal(
'this is a description'
);
Expand Down Expand Up @@ -1183,18 +1253,16 @@ 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.allRatings[0].articleId).to.equal(article.id);
});

it('should return error for article id that is less than 1', async () => {
const response = await chai
.request(app)
.get(`${API_VERSION}/articles/-1/ratings`)
.set({ Authorization: `Bearer ${userToken}` });

expect(response).to.have.status(400);
expect(response.body.status).to.equal('fail');
expect(response.body.data[0].msg).to.equal(
Expand All @@ -1207,7 +1275,6 @@ describe('Article API endpoints', () => {
.request(app)
.get(`${API_VERSION}/articles/notNumber/ratings`)
.set({ Authorization: `Bearer ${userToken}` });

expect(response).to.have.status(400);
expect(response.body.status).to.equal('fail');
expect(response.body.data[0].msg).to.equal('Invalid value');
Expand Down
1 change: 0 additions & 1 deletion src/tests/controller/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ describe('Auth API endpoints', () => {
expect(response.status).to.have.been.calledWith(500);
});
});

describe('POST /users/login', () => {
it('Should log user in successfully', async () => {
const user = getUser();
Expand Down
7 changes: 7 additions & 0 deletions src/tests/controller/stats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ describe('Page View Count Tests', () => {
});
});

beforeEach(async () => {
await chai
.request(app)
.put(`${process.env.API_VERSION}/articles/publish/${firstArticle.slug}`)
.set({ Authorization: `Bearer ${userToken}` });
});

it('Should successfully increase the view count this article by 1', done => {
chai
.request(app)
Expand Down
1 change: 1 addition & 0 deletions src/validators/article.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const ArticleValidator = {
param('articleId')
.isNumeric({ gt: 0 })
.not()
.not()
.matches('[-, +, %]')
.withMessage(
'Article ID must be a number and can not be less than 1'
Expand Down
23 changes: 23 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,29 @@
}
}
}
},
"get": {
"tags": ["Articles"],
"summary": "Post endpoint to rate an article",
"produces": ["application/json"],
"consumes": ["application/x-www-form-urlencoded"],
"parameters": [
{
"name": "articleId",
"in": "path",
"description": "id of the article's ratings to be fetched",
"required": true,
"type": "integer"
}
],
"responses": {
"400": {
"description": "Error in input"
},
"401": {
"description": "Error with authentication"
}
}
}
},
"securityDefinitions": {
Expand Down

0 comments on commit 393b7b2

Please sign in to comment.