Skip to content

Commit

Permalink
[feature 165412941] add edit history
Browse files Browse the repository at this point in the history
  • Loading branch information
MBenedicte committed May 20, 2019
1 parent 15eaa79 commit afbfb03
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 21 deletions.
20 changes: 1 addition & 19 deletions src/controllers/CommentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import status from '../config/status';
import create from '../queries/comments/createComment';
import getAll from '../queries/comments/getAllComments';
import deleteElement from '../queries/comments/deleteComment';
import db from '../models';
// import db from '../models';
/**
* comment controller class
*/
Expand Down Expand Up @@ -50,24 +50,6 @@ export default class CommentController {
});
}

/**
* Edit one comment
* @param { object } req the request.
* @param { object } res The response.
* @returns { object } the return object.
*/
static async edit(req, res) {
await db.Comment.update(
{ body: req.body.body },
{
where: { id: req.params.id }
}
);
return res.status(status.OK).send({
message: 'Comment edited successfully'
});
}

/**
* Delete one comment.
* @param { object } req the request.
Expand Down
72 changes: 72 additions & 0 deletions src/controllers/CommentEditController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import status from '../config/status';
import db from '../models';
import getSingleComment from '../queries/comments/getSingleComment';

/**
* A class to control the comments' edit
*/
export default class CommentEditController {
// eslint-disable-next-line valid-jsdoc
/**
*
* @param {object} req
* @param {object} res
*/
static async edit(req, res) {
const userId = req.userId || req.body.userId || req.params.userId || null;
const condition = {
id: req.params.id,
articleSlug: req.params.articleSlug
};
const comment = await getSingleComment(condition);

await db.CommentEdit.create({
userId,
articleSlug: req.params.articleSlug,
body: comment.body,
commentId: comment.id
});
await db.Comment.update(
{ body: req.body.body },
{
where: { id: req.params.id }
}
);
return res.status(status.OK).send({
message: 'Comment edited successfully'
});
}

/**
* Create a comment
* @param { object } req the request.
* @param { object } res The response.
* @returns { object } the return object.
*/
static async getAll(req, res) {
const userId = req.userId || req.body.userId || req.params.userId || null;
const response = await db.CommentEdit.findAll({
where: {
articleSlug: req.params.articleSlug,
commentId: req.params.id,
userId
}
});
const response2 = await db.Comment.findOne({
where: {
articleSlug: req.params.articleSlug,
id: req.params.id,
userId
}
});
if (response.length === 0) {
return res.status(status.OK).send({
Comment: response2
});
}
return res.status(status.OK).send({
message: 'Comments fetched successfully',
comment: response
});
}
}
47 changes: 47 additions & 0 deletions src/migrations/20190519211901-create-comment-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('CommentEdits', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
articleSlug: {
type: Sequelize.STRING,
allowNull: false,
references: {
model: 'Articles',
key: 'slug'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
userId: {
type: Sequelize.INTEGER,
allowNull: true,
references: {
model: 'Users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
},
body: {
type: Sequelize.TEXT,
allowNull: false
},
commentId: {
allowNull: null,
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('CommentEdits')
};
56 changes: 56 additions & 0 deletions src/models/commentedit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export default (sequelize, DataTypes) => {
const CommentEdit = sequelize.define(
'CommentEdit',
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
articleSlug: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: 'Articles',
key: 'slug'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
userId: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'Users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
},
body: {
type: DataTypes.TEXT,
allowNull: false
},
commentId: {
allowNull: null,
type: DataTypes.INTEGER
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
}
},
{}
);
CommentEdit.associate = (models) => {
CommentEdit.belongsTo(models.User, { foreignKey: 'userId', as: 'user' });
CommentEdit.belongsTo(models.Article, { foreignKey: 'articleSlug', as: 'article' });
CommentEdit.belongsTo(models.Comment, { foreignKey: 'commentId', as: 'comment' });
};
return CommentEdit;
};
7 changes: 5 additions & 2 deletions src/routes/api/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import Validation from '../../middlewares/validateComment';
import checkArticle from '../../middlewares/checkArticle';
import checkComment from '../../middlewares/checkComment';
import asyncHandler from '../../middlewares/asyncHandler';
import CommentEditController from '../../controllers/CommentEditController';

const router = Router();

router.post('/:articleSlug/comments', Validation, checkArticle, CommentController.create);
router.get('/:articleSlug/comments', asyncHandler(CommentController.getAll));

router.delete('/:articleSlug/comments/:id', checkArticle, checkComment, CommentController.delete);
router.put(
'/:articleSlug/comments/:id',
Validation,
checkArticle,
checkComment,
CommentController.edit
CommentEditController.edit
);
router.delete('/:articleSlug/comments/:id', checkArticle, checkComment, CommentController.delete);
router.get('/:articleSlug/comments/:id/edits', checkArticle, checkComment, CommentEditController.getAll);

export default router;
87 changes: 87 additions & 0 deletions src/tests/routes/commentEdit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import * as Factory from '../../helpers/factory';
import status from '../../config/status';
import db from '../../models';
import app from '../../app';

chai.use(chaiHttp);
chai.should();

let createdUser = {};
let createdArticle = {};
let createdComment = {};
let commentId;
let articleSlug;

const newUser = Factory.user.build();
const newArticle = Factory.article.build();
const newComment = Factory.comment.build();

delete newUser.id;
delete newArticle.id;
delete newComment.id;

describe('Comments edits', () => {
before(async () => {
try {
await db.User.destroy({
truncate: true,
cascade: true,
logging: false
});
await db.Article.destroy({
truncate: true,
cascade: true,
logging: false
});
await db.Comment.destroy({
truncate: true,
cascade: true,
logging: false
});
createdUser = await db.User.create(newUser, { logging: false });
newArticle.userId = createdUser.id;
createdArticle = await db.Article.create(newArticle, { logging: false });
newComment.articleSlug = createdArticle.slug;
createdComment = await db.Comment.create(newComment, { logging: false });
articleSlug = createdComment.articleSlug;
commentId = createdComment.id;
} catch (err) {
throw err;
}
});
it('Should let the user get a comment with that has not been edited yet', (done) => {
chai
.request(app)
.get(`/api/v1/articles/${articleSlug}/comments/${commentId}/edits`)
.send()
.end((err, res) => {
res.should.have.status(status.OK);
done();
});
});
it('Should let the user edit a comment', (done) => {
const comment = {
body: 'this is an edit'
};
chai
.request(app)
.put(`/api/v1/articles/${articleSlug}/comments/${commentId}`)
.send(comment)
.end((err, res) => {
res.should.have.status(status.OK);
done();
});
});
it('Should let the user get history of all versions of edits of a comment', (done) => {
chai
.request(app)
.get(`/api/v1/articles/${articleSlug}/comments/${commentId}/edits`)
.send()
.end((err, res) => {
res.should.have.status(status.OK);
done();
});
});
});
1 change: 1 addition & 0 deletions src/tests/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import './commentlike.test';
import './articleLike.test';
import './articleDislike.test';
import './report.test';
import './commentEdit.test';

0 comments on commit afbfb03

Please sign in to comment.