-
Notifications
You must be signed in to change notification settings - Fork 6
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
17 changed files
with
954 additions
and
168 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
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,155 @@ | ||
import { Comment, User } from '../models'; | ||
|
||
/** | ||
* @class CommentController | ||
* @override | ||
* @export | ||
* | ||
*/ | ||
export default class CommentController { | ||
/** | ||
* @description - Creates an article comment | ||
* @static | ||
* | ||
* @param {object} req - HTTP Request | ||
* @param {object} res - HTTP Response | ||
* | ||
* @memberof CommentController | ||
* | ||
* @returns {object} Article comment | ||
*/ | ||
static async createComment(req, res) { | ||
const { id } = req.user; | ||
const { slug: articleSlug } = req.params; | ||
const { comment } = req.body; | ||
try { | ||
const newComment = await Comment.create({ | ||
userId: id, articleSlug, comment | ||
}); | ||
return res.status(205).json({ | ||
success: true, | ||
message: 'New article comment created successfully', | ||
comment: newComment, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
success: false, | ||
errors: [error.message] | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @description - Gets an article's comments | ||
* @static | ||
* | ||
* @param {object} req - HTTP Request | ||
* @param {object} res - HTTP Response | ||
* | ||
* @memberof CommentController | ||
* | ||
* @returns {object} Article comment | ||
*/ | ||
static async getComments(req, res) { | ||
const { slug: articleSlug } = req.params; | ||
try { | ||
const findComment = await Comment.findAll({ | ||
raw: true, | ||
where: { | ||
articleSlug | ||
}, | ||
include: [ | ||
{ | ||
model: User, | ||
attributes: ['username', 'bio', 'email'] | ||
} | ||
] | ||
}); | ||
return res.status(205).json({ | ||
success: true, | ||
message: 'Comments returned successfully', | ||
comments: findComment, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
success: false, | ||
errors: [error.message] | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @description - Edit an article comment | ||
* @static | ||
* | ||
* @param {object} req - HTTP Request | ||
* @param {object} res - HTTP Response | ||
* | ||
* @memberof CommentController | ||
* | ||
* @returns {object} Updated article comment | ||
*/ | ||
static async editComment(req, res) { | ||
const { id } = req.params; | ||
const { comment } = req.body; | ||
try { | ||
const updatedComment = await Comment.update( | ||
{ | ||
comment | ||
}, | ||
{ | ||
returning: true, | ||
raw: true, | ||
where: { | ||
id | ||
} | ||
} | ||
); | ||
const newComment = updatedComment[1][0]; | ||
|
||
return res.status(205).json({ | ||
success: true, | ||
message: 'Comment updated successfully', | ||
body: newComment | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
success: false, | ||
errors: [error.message] | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @description - Delete an article comment | ||
* @static | ||
* | ||
* @param {object} req - HTTP Request | ||
* @param {object} res - HTTP Response | ||
* | ||
* @memberof CommentController | ||
* | ||
* @returns {string} Comment delete status | ||
*/ | ||
static async deleteComment(req, res) { | ||
const { id } = req.params; | ||
try { | ||
const rowsDeleted = await Comment.destroy({ | ||
where: { | ||
id | ||
} | ||
}); | ||
if (rowsDeleted === 1) { | ||
return res.status(205).json({ | ||
success: true, | ||
message: 'Comment deleted successfully', | ||
}); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ | ||
success: false, | ||
errors: [error.message] | ||
}); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.