Skip to content

Commit

Permalink
Merge pull request #54 from andela/bg-create-article-bug-167345040
Browse files Browse the repository at this point in the history
##167345040 Fix non-existent category error
  • Loading branch information
Cavdy committed Jul 23, 2019
2 parents 227fd30 + a7c6103 commit 9ebeac8
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ArticleController {
* @returns {object} new article
*
*/
static async create(req, res) {
static async createArticle(req, res) {
try {
const {
title, description, body, catId, tagList, isDraft
Expand Down
85 changes: 42 additions & 43 deletions src/db/migrations/20190716203009-create-comment-history.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('CommentHistories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('CommentHistories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
required: true,
references: {
model: 'CommentHistories',
key: 'id',
as: 'commentHistory'
},
userId: {
type: Sequelize.INTEGER,
required: true,
references: {
model: 'CommentHistories',
key: 'id',
as: 'commentHistory'
},
},
commentId: {
type: Sequelize.INTEGER,
required: true,
references: {
model: 'CommentHistories',
key: 'id',
as: 'commentHistory'
},
commentId: {
type: Sequelize.INTEGER,
required: true,
references: {
model: 'CommentHistories',
key: 'id',
as: 'commentHistory'
},
},
commentBody: {
type: Sequelize.TEXT,
required: true,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('CommentHistories');
}
};

},
commentBody: {
type: Sequelize.STRING,
required: true,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('CommentHistories');
}
};
18 changes: 17 additions & 1 deletion src/helpers/findItem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Article, User, Comment } from '../db/models';
import { Article, User, Comment, Category } from '../db/models';
/**
*
*@description This class checks a table for the presence or absence of a row
Expand Down Expand Up @@ -204,6 +204,22 @@ class FindItem {
res.locals.comment = comment;
return next();
}

/**
*@description This function finds a category using id
* @param {integer} id - the id of the article to be found
* @returns {object} category
* @memberof FindItem
*/
static async findCategoryById(id) {
const category = await Category.findOne({
where: {
id
}
});
return category;
}

}

export default FindItem;
26 changes: 26 additions & 0 deletions src/middleware/checkCategoryExists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import FindItem from '../helpers/findItem';

const { findCategoryById } = FindItem;

/**
* @description - checks if the category of the article exists
* @async
* @param {object} req - request object
* @param {object} res - response object
* @param {function} next - the next function
* @returns {object} error response object
*/
const checkCategoryExists = async (req, res, next) => {
const { catId } = req.body;
const existingCategory = await findCategoryById(catId);

if (!existingCategory) {
return res.status(404).json({
status: 404,
message: `The category does not exist`
});
}
next();
};

export default checkCategoryExists;
6 changes: 3 additions & 3 deletions src/routes/articleRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import validate from '../middleware/validate';
import upload from '../helpers/profilePic';
import verify from '../helpers/verifyToken';
import findItem from '../helpers/findItem';
import checkCategoryExists from '../middleware/checkCategoryExists';

const router = express.Router();
const { create, getArticle } = ArticleController;
const { createArticle, getArticle } = ArticleController;
const { detailsValidator } = articleValidator;

router.get('/articles', validateSearchInput, ArticleController.search);

router.post('/articles/', verify, upload.array('images', 10), detailsValidator, validate, create);
router.get(
'/articles/:slug',
findItem.findArticle,
getArticle
);
router.post('/articles/', verify, upload.array('images', 10), detailsValidator, validate, checkCategoryExists, createArticle);

export default router;
13 changes: 13 additions & 0 deletions test/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,18 @@ describe('Articles', () => {
done();
});
});

it('should return 404 error if catId does not exist', (done) => {
const partialArticleDetails = { ...testArticle, catId: 90 };
chai.request(app).post('/api/v1/articles')
.set('token', testToken)
.send(partialArticleDetails)
.end((err, res) => {
res.should.have.status(404);
res.body.should.be.an('object');
res.body.should.have.property('message').equal('The category does not exist');
done();
});
});
});
});

0 comments on commit 9ebeac8

Please sign in to comment.