Skip to content

Commit

Permalink
Merge 1796290 into 90a95e6
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamchineme committed Feb 12, 2019
2 parents 90a95e6 + 1796290 commit 78324aa
Show file tree
Hide file tree
Showing 83 changed files with 4,197 additions and 72 deletions.
601 changes: 601 additions & 0 deletions dist/api/controllers/article.js

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions dist/api/controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.updateComment = exports.postComment = void 0;

var _models = require("../../models");

const {
Op
} = _models.Sequelize;

const postComment = async (req, res) => {
const {
params: {
articleId
},
body: {
commentBody
},
user: {
id: userId
}
} = req;

try {
const newComment = await _models.Comment.create({
commentBody,
articleId,
userId
});
return res.status(201).send({
status: 'success',
data: newComment
});
} catch (error) {
return res.status(500).send({
status: 'error',
message: 'Error saving comment',
data: error
});
}
};

exports.postComment = postComment;

const updateComment = async (req, res) => {
const {
params: {
articleId,
commentId
},
body: {
commentBody
}
} = req;

try {
const foundArticle = await _models.Article.findByPk(articleId);

if (!foundArticle) {
res.status(404).send({
status: 'fail',
message: 'Article not found'
});
}

const foundComments = await foundArticle.getComments({
where: {
id: {
[Op.eq]: commentId
}
}
});
const foundComment = foundComments[0];

if (!foundComment) {
res.status(404).send({
status: 'fail',
message: 'Comment not found under this article'
});
} // update the comment


const updatedComment = await foundComment.update({
commentBody
}, {
returning: true,
plain: true
}); // save the old version of the comment in the comment history

await updatedComment.createCommentHistory({
commentId: foundComment.id,
commentBody: foundComment.commentBody
});
const oldComments = await updatedComment.getCommentHistories();
const comment = updatedComment.toJSON();
comment.oldComments = oldComments;
res.status(200).send({
status: 'success',
message: 'Comment updated',
data: comment
});
} catch (error) {
return res.status(500).send({
status: 'error',
message: 'Error updating comment'
});
}
};

exports.updateComment = updateComment;
Loading

0 comments on commit 78324aa

Please sign in to comment.