-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature 165412941] add edit history
- Loading branch information
1 parent
15eaa79
commit afbfb03
Showing
7 changed files
with
269 additions
and
21 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
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,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 | ||
}); | ||
} | ||
} |
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,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') | ||
}; |
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,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; | ||
}; |
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,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(); | ||
}); | ||
}); | ||
}); |
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