Skip to content

Commit

Permalink
Merge 47ecd13 into f80bcb2
Browse files Browse the repository at this point in the history
  • Loading branch information
Lumexralph committed Aug 24, 2018
2 parents f80bcb2 + 47ecd13 commit 2894f3d
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ dist/

#txt
txt.txt

#VS Code
.vscode
30 changes: 23 additions & 7 deletions controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class ArticleController {
model: User,
attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] }
}],
attributes: { exclude: ['userId'] }
})
.then((article) => {
// if the article does not exist
Expand All @@ -79,31 +78,48 @@ class ArticleController {
}

/**
* get all articles created
* get all articles created and use the query
* if provided to implement pagination
* @param {object} req - request object
* @param {object} res - response object
* @param {function} next - next function
* @returns {object} - the found article from database or empty if not found
*/
static listAllArticles(req, res, next) {
const { page, limit } = req;
let offset = null;

if (page || limit) {
// calculate offset
offset = limit * (page - 1);
}

return Article
.findAll({
include: [{
model: User,
attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] }
}],
attributes: { exclude: ['userId'] }
offset,
limit,
})
.then((articles) => {
// check if there was no article created
if (articles.length === 0) {
/** check if there was no article created
* for the page query
*/
const message = page ? 'articles limit exceeded'
: 'Sorry, no articles created';
return res.status(200).json({
message: 'No articles created',
message,
articles,
articlesCount: articles.length
});
}

return res.status(200).json({ articles, articlesCount: articles.length });
return res.status(200).json({
articles,
articlesCount: articles.length
});
})
.catch(next);
}
Expand Down
1 change: 0 additions & 1 deletion helpers/createArticleHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const createArticleHelper = (res, articleObject, imageUrl = null) => {
model: User,
attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] }
}],
attributes: { exclude: ['userId'] }
}))
.then(article => res.status(201).json({ article }));
};
Expand Down
Empty file removed helpers/isStringValidator.js
Empty file.
2 changes: 1 addition & 1 deletion helpers/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default class Utilities {
}

/**
* @function increaseCount
* @function increaseCount
* @summary: A funtion to increase count
* each time an article is updated
* @param {Integer} count: input param
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ passport.deserializeUser(((user, done) => {
// Create global app object
const app = express();
passportConfig(app);

app.use(cors());

// Normal express config defaults;
Expand Down
21 changes: 21 additions & 0 deletions middlewares/ParamsValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,25 @@ export default class ParamsValidator {
}
next();
}

/**
* Checks the page params provided for integer 1 and above
* and assign a default value when less than 1
* @param {string} req - The request to be validated
* @param {string} res - The response from validation
* @param {string} next - The next function
* @returns {undefined}
*/
static validatePageQuery(req, res, next) {
let { page, limit } = req.query;

if (page || limit) {
page = Number(page);
limit = Number(limit);

req.page = (page < 1 || !Number.isInteger(page)) ? 1 : page;
req.limit = (limit < 1 || !Number.isInteger(limit)) ? 10 : limit;
}
next();
}
}
3 changes: 2 additions & 1 deletion routes/api/articleRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Router } from 'express';
import ArticleControllers from '../../controllers/ArticleController';
import validateArticle from '../../middlewares/validateArticle';
import verifyToken from '../../middlewares/verifyToken';
import ParamsValidator from '../../middlewares/ParamsValidator';
import { checkCount, articleExists } from '../../middlewares/checkUser';


Expand All @@ -17,6 +18,6 @@ router.delete('/articles/:slug', verifyToken, articleExists, ArticleControllers.

router.get('/articles/:slug', ArticleControllers.getArticle);

router.get('/articles', ArticleControllers.listAllArticles);
router.get('/articles', ParamsValidator.validatePageQuery, ArticleControllers.listAllArticles);

export default router;
73 changes: 71 additions & 2 deletions tests/controllers/articleController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ describe('Articles API endpoints', () => {
.get('/api/articles')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an('object').to.have.property('message').to.equal('No articles created');
expect(res.body).to.be.an('object').to.have.property('message').to.equal('Sorry, no articles created');
expect(res.body).to.have.property('articles').to.be.an('array').with.lengthOf(0);
done();
});
});

it('Should register user', (done) => {
chai.request(app)
.post('/api/users')
.send(validUser)
.end((err, res) => {
expect(res).to.have.status(200);
validToken = res.body.user.token;
expect(res).to.have.status(200);
expect(res).to.have.status(200);
done();
});
});
Expand Down Expand Up @@ -194,6 +195,73 @@ describe('Articles API endpoints', () => {
});
});

it('Should paginate articles when limit and page is provided', (done) => {
chai.request(app)
.get('/api/articles?page=1&limit=10')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.articles).to.be.an('array').with.lengthOf(1);
expect(res.body.articlesCount).to.equal(res.body.articles.length);
done();
});
});

it('Should paginate articles when only page query is provided', (done) => {
chai.request(app)
.get('/api/articles?page=1')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.articles).to.be.an('array').with.lengthOf(1);
expect(res.body.articlesCount).to.equal(res.body.articles.length);
done();
});
});

it('Should paginate articles when page query is less than 1', (done) => {
chai.request(app)
.get('/api/articles?page=0')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.articles).to.be.an('array').with.lengthOf(1);
expect(res.body.articlesCount).to.equal(res.body.articles.length);
done();
});
});

it('Should paginate articles when limit query is less than 1', (done) => {
chai.request(app)
.get('/api/articles?limit=0')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.articles).to.be.an('array').with.lengthOf(1);
expect(res.body.articlesCount).to.equal(res.body.articles.length);
done();
});
});


it('Should paginate articles when only limit query is provided', (done) => {
chai.request(app)
.get('/api/articles?limit=5')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.articles).to.be.an('array').with.lengthOf(1);
expect(res.body.articlesCount).to.equal(res.body.articles.length);
done();
});
});

it('Should return empty array for query that exceeds number of articles during pagination', (done) => {
chai.request(app)
.get('/api/articles?page=10')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an('object').to.have.property('message').to.equal('articles limit exceeded');
expect(res.body.articlesCount).to.equal(res.body.articles.length);
done();
});
});

it('Should be able to edit an article created by a user', (done) => {
chai.request(app)
.put(`/api/articles/${createdArticle.slug}`)
Expand Down Expand Up @@ -258,6 +326,7 @@ describe('Articles API endpoints', () => {
done();
});
});

it('Should delete an article created by a user', (done) => {
chai.request(app)
.delete(`/api/articles/${createdArticle.slug}`)
Expand Down

0 comments on commit 2894f3d

Please sign in to comment.