Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: refactor articles endpoint response #103

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions server/controllers/get-articles.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import models from '../models';
import validations from '../helpers/validations';
import serverError from '../helpers/server-error';

const { Article } = models;
const { Article, User } = models;

const getArticles = async (req, res) => {
if (validations.validateArticlePage(req.params.page)) {
Expand All @@ -22,7 +22,7 @@ const getArticles = async (req, res) => {
const offset = pageSize * page - pageSize;

try {
const articles = await Article.findAll({
const articles = await Article.findAndCountAll({
offset,
limit: pageSize,
order: ['title'],
Expand All @@ -34,10 +34,17 @@ const getArticles = async (req, res) => {
'updatedAt',
'likes_count',
],
include: [
{
model: User,
as: 'author',
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
},
],
});

return res.status(200).json({
articles,
articles: articles.rows,
articlesCount: articles.count,
});
} catch (e) {
return res.status(500).json({
Expand All @@ -47,7 +54,7 @@ const getArticles = async (req, res) => {
}
return res.status(400).json({
errors: {
body: ['cannot be anything but numbers'],
body: ['Page number cannot be anything but numbers'],
},
});
};
Expand Down
13 changes: 10 additions & 3 deletions server/controllers/search-article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const searchArticles = async (req, res) => {
},
});
}
const searchFilter = req.query.search;
const searchFilter = req.query.filter;
const articles = Article.findAll({
where: {
title: {
Expand All @@ -32,7 +32,7 @@ const searchArticles = async (req, res) => {
{
model: User,
as: 'author',
attributes: ['first_name', 'last_name'],
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
},
],
});
Expand All @@ -56,6 +56,13 @@ const searchArticles = async (req, res) => {
'updatedAt',
'likes_count',
],
include: [
{
model: User,
as: 'author',
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
},
],
},
],
});
Expand All @@ -66,7 +73,7 @@ const searchArticles = async (req, res) => {
[sequelize.Op.iLike]: `%${searchFilter}%`,
},
},
attributes: ['first_name', 'last_name'],
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
include: [
{
model: Article,
Expand Down
10 changes: 7 additions & 3 deletions tests/article-pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ describe('Display articles in pages', () => {
.request(app)
.get('/api/v1/articles/1')
.end((err, res) => {
const { articles } = JSON.parse(res.text);
expect(articles).to.be.a('array');
expect(res).to.have.status(200);
expect(res.body.articles).to.be.a('array');
expect(res.body.articlesCount).to.be.a('number');

done();
});
Expand All @@ -20,7 +21,10 @@ describe('Display articles in pages', () => {
.get('/api/v1/articles/a')
.end((err, res) => {
const errorMessage = res.body.errors.body[0];
expect(errorMessage).to.be.equal('cannot be anything but numbers');
expect(res).to.have.status(400);
expect(errorMessage).to.be.equal(
'Page number cannot be anything but numbers'
);

done();
});
Expand Down
6 changes: 3 additions & 3 deletions tests/article-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Article Search', () => {
it('Display articles by search filter', done => {
chai
.request(app)
.get(`/api/v1/search?search=${articleSearchFilter}`)
.get(`/api/v1/search?filter=${articleSearchFilter}`)
.end((err, res) => {
const { articleFilter } = res.body;
expect(res.status).to.equal(200);
Expand All @@ -22,7 +22,7 @@ describe('Article Search', () => {
it('Display author by search filter', done => {
chai
.request(app)
.get(`/api/v1/search?search=${authorSearchFilter}`)
.get(`/api/v1/search?filter=${authorSearchFilter}`)
.end((err, res) => {
const { authorFilter } = res.body;
expect(res.status).to.equal(200);
Expand All @@ -35,7 +35,7 @@ describe('Article Search', () => {
it('Display keyword by search filter', done => {
chai
.request(app)
.get(`/api/v1/search?search=${keywordSearchFilter}`)
.get(`/api/v1/search?filter=${keywordSearchFilter}`)
.end((err, res) => {
const { keywordFilter } = res.body;
expect(res.status).to.equal(200);
Expand Down