diff --git a/controllers/blogController.js b/controllers/blogController.js new file mode 100644 index 0000000..784a76d --- /dev/null +++ b/controllers/blogController.js @@ -0,0 +1,52 @@ +var fs = require('fs'); +//erstellt JSONObjekt +let rawdata = fs.readFileSync('./public/models/blog.json'); +//wandelt JSONObjekt in JsObjekt +let blogInhalt = JSON.parse(rawdata); + +var getAllPosts=(req, res) => { + for (i = 0; i < blogInhalt.length; i++){ + console.log(blogInhalt[i].id, "." , blogInhalt[i].title); + } + res.send({ blogInhalt }); +} + +var createPost = (req, res) => { + let blogeintrag = { + id : blogInhalt.length + 1, + title : req.body.title, + username : req.body.username, + date : req.body.date, + text : req.body.text + } + + console.log(blogeintrag); + + //hänge blogeintrag an blogInhalt + blogInhalt.push(blogeintrag); + + let date = JSON.stringify(blogInhalt, null, 2); + + fs.writeFileSync('./public/models/blog.json', date); + + res.render('blogPost', { title: 'Nächster Post' }) +} + +var getPost = (req, res) => { + let blogID = req.params.postID; + + if(blogID > blogInhalt.length){ + console.log("kein entsprechender Eintrag vorhanden"); + res.send("kein entsprechender Eintrag vorhanden") + } else { + console.log(blogInhalt[blogID - 1]); + res.send(blogInhalt[blogID - 1]); + } + +} + +module.exports = { + getAllPosts, + createPost, + getPost +} \ No newline at end of file