diff --git a/models/blog-post.js b/models/blog-post.js index 01517ed..8ee89da 100644 --- a/models/blog-post.js +++ b/models/blog-post.js @@ -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); diff --git a/routes/admin.js b/routes/admin.js index 0590d22..b9509e0 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -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) => { diff --git a/routes/blog.js b/routes/blog.js index 3ececa6..f6f8095 100644 --- a/routes/blog.js +++ b/routes/blog.js @@ -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 * diff --git a/views/admin.hbs b/views/admin.hbs index be0f55c..50d192a 100644 --- a/views/admin.hbs +++ b/views/admin.hbs @@ -24,11 +24,13 @@
@@ -37,13 +39,15 @@ diff --git a/views/post-collection.hbs b/views/post-collection.hbs new file mode 100644 index 0000000..ca42724 --- /dev/null +++ b/views/post-collection.hbs @@ -0,0 +1,7 @@ +{{ this.content }}
+link +