Skip to content

Commit

Permalink
feat(articles): Completes get a single article by slug
Browse files Browse the repository at this point in the history
- Write tests
  • Loading branch information
chukwuemekachm committed Aug 10, 2018
1 parent 5205b35 commit 4979b91
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 16 deletions.
27 changes: 24 additions & 3 deletions server/controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ArticleController {
const limit = req.query.limit || 100;
const offset = req.query.offset || 0;
Article.findAll(
Object.assign({}, queryHelper.default, { offset, limit })
Object.assign({}, queryHelper.allArticles, { offset, limit })
).then((articles) => {
ArticleController.sendPaginationResponse(res, articles, false);
})
Expand All @@ -50,13 +50,34 @@ export default class ArticleController {
const offset = req.query.offset || 0;
const { username: userUsername } = req.params;
Article.findAll(
Object.assign({}, queryHelper.default, { where: { userUsername }, offset, limit }),
Object.assign({}, queryHelper.allArticles, { where: { userUsername }, offset, limit }),
).then((articles) => {
ArticleController.sendPaginationResponse(res, articles, userUsername);
})
.catch(err => next(err));
}

static getSingleArticle(req, res, next) {
const { slug } = req.params;
Article.findOne(
Object.assign({}, queryHelper.singleArticle, { where: { slug } }),
).then((article) => {
if (article) {
return res.status(200).json({
status: 'success',
article,
});
}
return res.status(404).json({
status: 'fail',
errors: {
article: [`Article with slig: ${slug} not found.`],
},
});
})
.catch(err => next(err));
}

/**
* Sends the response to the user when a list of paginated articles are requested
* For a given user or random articles
Expand All @@ -68,7 +89,7 @@ export default class ArticleController {
*/
static sendPaginationResponse(res, articles, username) {
// Returns a 20 when a list of articles are requested and found
if (articles.length > 0) {
if (articles[0]) {
return res.status(200).json({
status: 'success',
articles,
Expand Down
22 changes: 20 additions & 2 deletions server/helpers/queryHelper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import models from '../models';

const { User, Rating, Tag } = models;
const {
User, Rating, Tag,
} = models;

/*
* Holds the query helper object which contains the various query templates
*/
export default {
default: {
allArticles: {
order: [
['createdAt', 'DESC']
],
Expand All @@ -17,4 +19,20 @@ export default {
{ model: Tag, as: 'tags', attributes: ['id', 'articleSlug', 'title', 'createdAt', 'updatedAt'] },
],
},
singleArticle: {
order: [
['createdAt', 'DESC']
],
attributes: ['id', 'slug', 'userUsername', 'categoryId', 'title', 'body', 'imageUrl', 'createdAt', 'updatedAt'],
include: [
{ model: User, as: 'author', attributes: ['username', 'firstName', 'lastName', 'bio', 'image'] },
{
model: Rating,
as: 'ratings',
attributes: ['id', 'userUsername', 'articleSlug', 'value', 'createdAt', 'updatedAt'],
include: [{ model: User, as: 'rater', attributes: ['username', 'firstName', 'lastName', 'bio', 'image'] }],
},
{ model: Tag, as: 'tags', attributes: ['id', 'articleSlug', 'title', 'createdAt', 'updatedAt'] },
],
},
};
2 changes: 1 addition & 1 deletion server/routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import ArticleController from '../controllers/ArticleController';
const articleRouter = Router();

articleRouter.get('/', ArticleValidator.paginationValidation, ArticleController.getAllArticles);
articleRouter.get('/:username', ArticleValidator.paginationValidation, ArticleController.getUserArticles);
articleRouter.get('/:slug', ArticleController.getSingleArticle);

export default articleRouter;
4 changes: 3 additions & 1 deletion server/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Router } from 'express';
import UserController from '../controllers/UserController';
import isLoggedIn from '../middlewares/isLoggedIn';
import ArticleController from '../controllers/ArticleController';
import ArticleValidator from '../middlewares/validations/ArticleValidator';

const userRouter = Router();

userRouter.get('/', isLoggedIn, UserController.getLoggedInUser);

userRouter.get('/:username/articles', ArticleValidator.paginationValidation, ArticleController.getUserArticles);
userRouter.get('/', isLoggedIn, UserController.getLoggedInUser);

export default userRouter;
18 changes: 9 additions & 9 deletions server/tests/paginationArticle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ chai.should();
chai.use(chaiHttp);

describe('GET /api/articles Tests a list of paginated articles', () => {
it('should return 200 when the user is authenticated and articles are paginated', (done) => {
it('should return 200 when the articles are paginated', (done) => {
chai.request(app).get('/api/articles?limit=2&offset=0')
.end((req, res) => {
res.status.should.eql(200);
Expand Down Expand Up @@ -96,9 +96,9 @@ describe('GET /api/articles Tests a list of paginated articles', () => {
});
});

describe('GET /api/articles/:username Tests a list of paginated articles which belongs to a given', () => {
it('should return 200 when the user is authenticated and articles are paginated', (done) => {
chai.request(app).get('/api/articles/oyomi?limit=2&offset=0')
describe('GET /api/users/:username/articles Tests a list of paginated articles which belongs to a given', () => {
it('should return 200 when there are articles that belongs to the author and the articles are paginated', (done) => {
chai.request(app).get('/api/users/oyomi/articles?limit=2&offset=0')
.end((req, res) => {
res.status.should.eql(200);
res.body.should.be.a('object');
Expand All @@ -124,7 +124,7 @@ describe('GET /api/articles/:username Tests a list of paginated articles which b
});

it('should return 400 when the limit is not an integer', (done) => {
chai.request(app).get('/api/articles/oyomi?limit=uj')
chai.request(app).get('/api/users/oyomi/articles?limit=uj')
.end((req, res) => {
res.status.should.eql(400);
res.body.should.be.a('object');
Expand All @@ -136,7 +136,7 @@ describe('GET /api/articles/:username Tests a list of paginated articles which b
});

it('should return 400 when the limit is a negative integer', (done) => {
chai.request(app).get('/api/articles/oyomi?limit=-2')
chai.request(app).get('/api/users/oyomi/articles?limit=-2')
.end((req, res) => {
res.status.should.eql(400);
res.body.should.be.a('object');
Expand All @@ -148,7 +148,7 @@ describe('GET /api/articles/:username Tests a list of paginated articles which b
});

it('should return 400 when the offset is not an integer', (done) => {
chai.request(app).get('/api/articles/oyomi?limit=2&offset=er')
chai.request(app).get('/api/users/oyomi/articles?limit=2&offset=er')
.end((req, res) => {
res.status.should.eql(400);
res.body.should.be.a('object');
Expand All @@ -160,7 +160,7 @@ describe('GET /api/articles/:username Tests a list of paginated articles which b
});

it('should return 400 when the offset is a negative integer', (done) => {
chai.request(app).get('/api/articles/oyomi?limit=2&offset=-2')
chai.request(app).get('/api/users/oyomi/articles?limit=2&offset=-2')
.end((req, res) => {
res.status.should.eql(400);
res.body.should.be.a('object');
Expand All @@ -172,7 +172,7 @@ describe('GET /api/articles/:username Tests a list of paginated articles which b
});

it('should return 404 when no articles where found for the user', (done) => {
chai.request(app).get('/api/articles/chima?limit=0&offset=0')
chai.request(app).get('/api/users/chima/articles?limit=0&offset=0')
.end((req, res) => {
res.status.should.eql(404);
res.body.should.be.a('object');
Expand Down

0 comments on commit 4979b91

Please sign in to comment.