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.
48 lines
1.1 KiB
48 lines
1.1 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?blogInhalt[blogInhalt.length - 1].id+1 : 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 = parseInt(req.params.postID);
|
|
console.log(blogID);
|
|
let found = blogInhalt.find(element => element.id === blogID);
|
|
|
|
found?res.send(found):res.send('nichts gefunden');
|
|
}
|
|
|
|
module.exports = {
|
|
getAllPosts,
|
|
createPost,
|
|
getPost
|
|
}
|