You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
52 lines
1.2 KiB
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
|
|
}
|