-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import db from '../models'; | ||
import { | ||
successResponse, | ||
errorResponse, | ||
validate, | ||
validationErrorResponse | ||
} from '../utils/helpers'; | ||
import { commentSchema } from '../utils/validators'; | ||
|
||
const { Comment, Recipe } = db; | ||
|
||
// const userFilter = { | ||
// attributes: { | ||
// include: [], | ||
// exclude: [] | ||
// } | ||
// }; | ||
|
||
/** | ||
* The controllers for comment route | ||
* | ||
* @class CommentController | ||
*/ | ||
class CommentController { | ||
/** | ||
* Comment registeration controller | ||
* @static | ||
* @param {*} req Express request object | ||
* @param {*} res Express request object | ||
* @memberof RecipeController | ||
* @returns {undefined} | ||
*/ | ||
static async create(req, res) { | ||
const { | ||
params: { slug }, | ||
body: { body }, | ||
user: { id: userId } | ||
} = req; | ||
try { | ||
await validate(req.body, commentSchema); | ||
const recipe = await Recipe.findOne({ where: { slug } }); | ||
if (!recipe) { | ||
return errorResponse(res, 'does not exist', 400); | ||
} | ||
const { | ||
dataValues: { id: recipeId } | ||
} = recipe; | ||
Comment.create({ userId, recipeId, body }, { returning: true }) | ||
.then(({ dataValues }) => { | ||
successResponse(res, dataValues, 201); | ||
}) | ||
.catch(err => errorResponse(res, err, 400)); | ||
} catch (err) { | ||
return validationErrorResponse(res, err.details); | ||
} | ||
} | ||
} | ||
|
||
export default CommentController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => | ||
queryInterface.createTable('Comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
body: { | ||
type: Sequelize.TEXT | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface /* Sequelize */) => queryInterface.dropTable('Comments') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Comment = sequelize.define( | ||
'Comment', | ||
{ | ||
body: DataTypes.TEXT | ||
}, | ||
{} | ||
); | ||
Comment.associate = models => { | ||
Comment.belongsTo(models.Recipe, { | ||
foreignKey: 'recipeId', | ||
onDelete: 'SET NULL' | ||
}); | ||
Comment.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'SET NULL' | ||
}); | ||
}; | ||
return Comment; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import chai, { expect } from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import server from '../server'; | ||
import { generateToken } from '../server/utils/helpers'; | ||
import { comment, commentTestUser, recipe } from './fixtures'; | ||
|
||
chai.use(chaiHttp); | ||
|
||
const runtimeFixture = {}; | ||
|
||
describe('Comments', () => { | ||
before(() => | ||
chai | ||
.request(server) | ||
.post('/api/users') | ||
.send(commentTestUser) | ||
.then(res => { | ||
const { token } = res.body.user; | ||
runtimeFixture.token = token; | ||
}) | ||
); | ||
describe('POST /api/recipes/:slug/comments', () => { | ||
before(done => { | ||
chai | ||
.request(server) | ||
.post('/api/recipes') | ||
.set({ authorization: runtimeFixture.token }) | ||
.send(recipe) | ||
.end((err, res) => { | ||
const { slug } = res.body.recipe; | ||
runtimeFixture.slug = slug; | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should create new recipe comment when a token is provided', done => { | ||
chai | ||
.request(server) | ||
.post(`/api/recipes/${runtimeFixture.slug}/comments`) | ||
.set({ authorization: runtimeFixture.token }) | ||
.send(comment) | ||
.end((err, res) => { | ||
const { body } = res; | ||
expect(res).to.have.status(201); | ||
expect(body.body).to.equal(body.body); | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should not create a comment when token is invalid', done => { | ||
chai | ||
.request(server) | ||
.post(`/api/recipes/${runtimeFixture.slug}/comments`) | ||
.set({ authorization: generateToken({ id: 1000 }) }) | ||
.send(comment) | ||
.end((err, res) => { | ||
const { body } = res; | ||
expect(res).to.have.status(404); | ||
expect(body.errors).to.contain('User does not exist'); | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should not create a comment when token is absent', done => { | ||
chai | ||
.request(server) | ||
.post(`/api/recipes/${runtimeFixture.slug}/comments`) | ||
.set({}) | ||
.send(comment) | ||
.end((err, res) => { | ||
const { body } = res; | ||
expect(res).to.have.status(400); | ||
expect(body.errors).to.contain('Token is not provided'); | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should not create comment when body is not present', done => { | ||
chai | ||
.request(server) | ||
.post(`/api/recipes/${runtimeFixture.slug}/comments`) | ||
.set({ authorization: runtimeFixture.token }) | ||
.send() | ||
.end((err, res) => { | ||
const { body } = res; | ||
expect(res).to.have.status(400); | ||
expect(body.errors).to.haveOwnProperty('body'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters