Files
blog/api/webserver/controllers/postController.js

51 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
};