added getting latest post

This commit is contained in:
mrjvs
2018-10-14 19:29:33 +02:00
parent 5c291fefff
commit 68f19d75b9
5 changed files with 59 additions and 5 deletions

View File

@@ -25,6 +25,10 @@ const blogPostSchema = new mongoose.Schema({
type: String,
required: [true, 'Name is required']
},
short: {
type: String,
required: [true, 'Short version is required']
},
meta: {
urlTitle: {
type: String,
@@ -65,6 +69,13 @@ blogPostSchema.methods.getBlogPostTemplateReady = function(callback) {
});
});
};
blogPostSchema.methods.getBlogPostShortTemplateReady = function() {
return {
content: this.short,
title: this.name,
url: common.convertDateToString(this.meta.date) + '/' + this.meta.urlTitle
};
};
blogPostSchema.statics.convertMarkdownToHtml = function(markdown) {
return converter.makeHtml(markdown);
@@ -75,6 +86,16 @@ blogPostSchema.statics.getPost = function(date, urlTitle, callback) {
'meta.urlTitle': urlTitle
}, callback);
};
// not tested
blogPostSchema.statics.getLatestBlogPostShortTemplateReady = function(amount, callback) {
blogPostModel.find({}).sort({'meta.date': 'desc'}).exec(function(err, posts) {
if (err) return callback(err);
let out = [];
for (let i = 0, l = posts.length; i < ( amount+1 < l ? amount+1 : l); i++)
out += posts[i].getBlogPostShortTemplateReady();
callback(err, out);
});
};
const blogPostModel = mongoose.model('blogPost', blogPostSchema);

View File

@@ -135,6 +135,7 @@ router.get('/admin/api/v1/logout', adminUserMiddleware.adminAuthenticationRequir
* content - content of the blog post in markdown
* title - title of the blog post
* author - id of the author
* short - short description of content in plain text
* category - category name of the blog post
*
* }
@@ -149,10 +150,11 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
if (!req.body) return common.sendApiGenericError(res);
const { content, title, author, category } = req.body;
const { content, title, author, category, short } = req.body;
const newBlogPost = new blogPost.blogPostModel({
content: blogPost.blogPostModel.convertMarkdownToHtml(content),
name: title,
short,
meta: {
author,
category,
@@ -186,6 +188,7 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
* content - content of the blog post IN HTML
* title - title of the blog post
* author - id of the author
* short - short description of content in plain text
* category - category name of the blog post
* }
* return {
@@ -199,10 +202,11 @@ router.post('/admin/api/v1/editpost', adminUserMiddleware.adminAuthenticationReq
if (!req.body) return common.sendApiGenericError(res);
const { id, content, title, author, category } = req.body;
const { id, content, title, author, category, short } = req.body;
blogPost.blogPostModel.findByIdAndUpdate(id, {
'content': content,
'name': title,
'short': short,
'meta.author': author,
'meta.category': category
}, (err, post) => {

View File

@@ -11,7 +11,7 @@ const common = require('../helpers/common');
const blogPostModel = require('../models/blog-post').blogPostModel;
const postAuthorModel = require('../models/post-author').postAuthorModel;
// display blog post
// display single blog post
router.get('/news/:date/:title', (req, res) => {
// date format DD-MM-YYY
if (/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(req.params.date) && /([a-z]|[0-9]|-)+/.test(req.params.title.toLowerCase())) {
@@ -37,6 +37,24 @@ router.get('/news/:date/:title', (req, res) => {
}
});
// display latest blogposts
router.get('/news', (req, res) => {
blogPostModel.find({}).sort({'meta.date': 'desc'}).exec(function(err, posts) {
if (err || !posts) {
return common.sendDefault404(res);
}
const postCollection = [];
for (let i = 0, l = posts.length; i < l; i++) {
postCollection.push(posts[i].getBlogPostShortTemplateReady());
}
res.render('post-collection', {
posts: postCollection
});
});
});
/*
* /api/v1/listauthors
*

View File

@@ -24,11 +24,13 @@
<form action="/admin/api/v1/newpost" method="POST">
<p>content</p>
<textarea name="content" cols="30" rows="10"></textarea>
<p>short version</p>
<textarea name="short" cols="30" rows="10"></textarea>
<p>category</p>
<input type="text" name="category">
<p>title</p>
<input type="text" name="title">
<p>author id - yea, authors dont work yet. just ignore</p>
<p>author id</p>
<input type="text" name="author">
<button>create post</button>
</form>
@@ -37,13 +39,15 @@
<form action="/admin/api/v1/editpost" method="POST">
<p>content</p>
<textarea name="content" cols="30" rows="10"></textarea>
<p>short version</p>
<textarea name="short" cols="30" rows="10"></textarea>
<p>id</p>
<input type="text" name="id">
<p>category</p>
<input type="text" name="category">
<p>title</p>
<input type="text" name="title">
<p>author id - yea, authors dont work yet. just ignore</p>
<p>author id</p>
<input type="text" name="author">
<button>edit post</button>
</form>

View File

@@ -0,0 +1,7 @@
<h1>Latest posts</h1>
{{#each posts }}
<strong>{{ this.title }}</strong>
<p>{{ this.content }}</p>
<a href="{{ this.url }}">link</a>
<br><br>
{{/each}}