51 lines
1.0 KiB
JavaScript
51 lines
1.0 KiB
JavaScript
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, title } = req.body;
|
||
|
||
return postRepository.updatePost(id, markdown, title)
|
||
.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
|
||
};
|