Post controller and repository for interfacing db.

This commit is contained in:
2021-01-03 18:17:29 +01:00
parent 3aef0862ec
commit df0905401f
2 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
const logger = require(`${__base}/logger`);
const PostRepository = require(`${__base}/post`);
const postRepository = new PostRepository();
function getPost(req, res) {
const { id } = req.params;
return postRepository.getPost(id)
.then(post => res.json({
success: true,
post: post
}))
}
function renderPost(req, res) {
const { id } = req.params;
return postRepository.renderPost(id)
.then(markdown => res.json({
success: true,
markdown: markdown
}))
}
function updatePost(req, res) {
const { id } = req.params;
const { markdown } = req.body;
return postRepository.updatePost(id, markdown)
.then(fileUpdated => res.json({
message: "Successfully updated post " + id,
filename: fileUpdated,
success: true
}))
.catch(error => {
const { httpStatus, message } = error;
return res.status(httpStatus || 500).json({
message: message,
success: false
})
})
}
module.exports = {
getPost,
renderPost,
updatePost
};