-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
166816212-feature(user, article, comment): comment on articles
- add comment role migration - add comment controller - add comments route - add validation - add tests - add documentation [Delivers #166816212]
- Loading branch information
1 parent
7103c35
commit 86200c0
Showing
9 changed files
with
228 additions
and
1 deletion.
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,30 @@ | ||
import db from '../../db/models'; | ||
|
||
export default { | ||
addComment: async (req, res) => { | ||
const { params: { slug }, body: { content }, user } = req; | ||
try { | ||
const foundArticle = await db.Article.findOne({ | ||
where: { slug } | ||
}); | ||
if (!foundArticle) { | ||
return res.status(404).send({ | ||
error: 'Article does not exist' | ||
}); | ||
} | ||
const comment = await user.createComment({ | ||
articleId: foundArticle.id, | ||
content | ||
}); | ||
return res.status(201).json({ | ||
message: 'Comment added successfully', | ||
comment | ||
}); | ||
} catch (e) { | ||
/* istanbul ignore next */ | ||
return res.status(500).json({ | ||
message: 'something went wrong' | ||
}); | ||
} | ||
} | ||
}; |
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,42 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Comments', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
required: true, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'user' | ||
} | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
required: true, | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'article' | ||
} | ||
}, | ||
content: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => 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,21 @@ | ||
|
||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const Comment = sequelize.define('Comment', { | ||
userId: DataTypes.INTEGER, | ||
articleId: DataTypes.INTEGER, | ||
content: DataTypes.STRING | ||
}, {}); | ||
Comment.associate = (models) => { | ||
Comment.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
as: 'user' | ||
}); | ||
Comment.belongsTo(models.Article, { | ||
foreignKey: 'articleId', | ||
as: 'article', | ||
cascade: true | ||
}); | ||
}; | ||
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
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