mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-08-23 00:57:21 -05:00
blog post posting and reading finished. now just edit, sort and delete
This commit is contained in:
@@ -63,11 +63,19 @@ function sendApiError(res, code, errors) {
|
||||
});
|
||||
}
|
||||
|
||||
// convert date to string
|
||||
function convertDateToString(date) {
|
||||
return date.getUTCFullYear() + '-' +
|
||||
('0' + (date.getUTCMonth()+1)).slice(-2) + '-' +
|
||||
('0' + date.getUTCDate()).slice(-2);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendDefault404,
|
||||
sendApiReturn,
|
||||
sendApi404,
|
||||
sendApiGenericError,
|
||||
sendApiError,
|
||||
sendApiAuthError
|
||||
sendApiAuthError,
|
||||
convertDateToString
|
||||
};
|
||||
@@ -7,9 +7,11 @@ file containing the model file for a blog post
|
||||
|
||||
// imports
|
||||
const mongoose = require('mongoose');
|
||||
const postAuthor = require('./post-author').postAuthorModel;
|
||||
const common = require('../helpers/common');
|
||||
//const postAuthor = require('./post-author').postAuthorModel;
|
||||
const showdown = require('showdown');
|
||||
const converter = new showdown.Converter();
|
||||
converter.setFlavor('github');
|
||||
|
||||
// admin user database layout
|
||||
const blogPostSchema = new mongoose.Schema({
|
||||
@@ -36,7 +38,9 @@ const blogPostSchema = new mongoose.Schema({
|
||||
},
|
||||
date: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
default: () => {
|
||||
return new Date(common.convertDateToString(new Date()));
|
||||
}
|
||||
},
|
||||
category: {
|
||||
type: String,
|
||||
@@ -54,8 +58,8 @@ blogPostSchema.methods.getBlogPostTemplateReady = function() {
|
||||
content: this.content,
|
||||
title: this.name,
|
||||
date: this.meta.date,
|
||||
category: this.meta.category,
|
||||
author: postAuthor.findById(this.meta.author).getPostAuthorTemplateReady()
|
||||
category: this.meta.category/*,
|
||||
author: postAuthor.findById(this.meta.author).getPostAuthorTemplateReady()*/
|
||||
};
|
||||
};
|
||||
|
||||
@@ -64,10 +68,8 @@ blogPostSchema.statics.convertMarkdownToHtml = function(markdown) {
|
||||
};
|
||||
blogPostSchema.statics.getPost = function(date, urlTitle, callback) {
|
||||
return blogPostModel.findOne({
|
||||
meta: {
|
||||
date,
|
||||
urlTitle
|
||||
}
|
||||
'meta.date': date,
|
||||
'meta.urlTitle': urlTitle
|
||||
}, callback);
|
||||
};
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ const passport = require('passport');
|
||||
const common = require('../helpers/common');
|
||||
const adminUserMiddleware = require('../middleware/admin-authentication');
|
||||
const adminUser = require('../models/admin-user');
|
||||
const blogPost = require('../models/blog-post');
|
||||
|
||||
// display admin panel
|
||||
router.get('/admin', (req, res) => {
|
||||
@@ -123,6 +124,56 @@ router.get('/admin/api/v1/logout', adminUserMiddleware.adminAuthenticationRequir
|
||||
common.sendApiReturn(res, {});
|
||||
});
|
||||
|
||||
/*
|
||||
* /admin/api/v1/newpost
|
||||
*
|
||||
* posts a new blog post
|
||||
*
|
||||
* post {
|
||||
* content - content of the blog post in markdown
|
||||
* title - title of the blog post
|
||||
* author - id of the author
|
||||
* category - category name of the blog post
|
||||
*
|
||||
* }
|
||||
* return {
|
||||
* code: http code
|
||||
* success: boolean - true if login succesfull
|
||||
* url: string | undefined - url of the blog post if successfull
|
||||
* errors: Strings[messages]
|
||||
* }
|
||||
*/
|
||||
router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
|
||||
|
||||
if (!req.body) return common.sendApiGenericError(res);
|
||||
|
||||
const { content, title, author, category } = req.body;
|
||||
const newBlogPost = new blogPost.blogPostModel({
|
||||
content: blogPost.blogPostModel.convertMarkdownToHtml(content),
|
||||
name: title,
|
||||
meta: {
|
||||
author,
|
||||
category,
|
||||
urlTitle: title
|
||||
.trim()
|
||||
.replace(/\s/g, '-')
|
||||
.replace(/[^A-z0-9-]/g, '')
|
||||
.toLowerCase()
|
||||
}
|
||||
});
|
||||
|
||||
newBlogPost.save().then((post) => {
|
||||
// successfull
|
||||
common.sendApiReturn(res, {
|
||||
url: common.convertDateToString(post.meta.date) + '/' + post.meta.urlTitle
|
||||
});
|
||||
}).catch((rejection) => {
|
||||
// TODO format exception so it doesnt have a huge list of errors
|
||||
common.sendApiError(res, 500, [rejection]);
|
||||
return;
|
||||
});
|
||||
});
|
||||
|
||||
// configure api 404
|
||||
router.use('/admin/api', (req, res) => {
|
||||
common.sendApi404(res);
|
||||
|
||||
@@ -12,15 +12,20 @@ const blogPostModel = require('../models/blog-post').blogPostModel;
|
||||
|
||||
// display blog post
|
||||
router.get('/news/:date/:title', (req, res) => {
|
||||
if (/[0-9]{2}-[0-9]{2}-[0-9]{4}/.test(req.params.date) && /([a-z]|[0-9]|-)+/.test(req.params.title.toLowerCase())) {
|
||||
// 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())) {
|
||||
// params are correct format
|
||||
|
||||
blogPostModel.getPost(req.params.date, req.params.title.toLowerCase(), (err, post) => {
|
||||
blogPostModel.getPost(new Date(req.params.date), req.params.title.toLowerCase(), (err, post) => {
|
||||
// error exists or no post exists with the date and name
|
||||
if (err || !post) return common.sendDefault404(res);
|
||||
if (err || !post) {
|
||||
console.log('error: ' + err + ' and post: ' + post);
|
||||
return common.sendDefault404(res);
|
||||
}
|
||||
|
||||
// render blogpost
|
||||
res.render('post', post.getBlogPostTemplateReady());
|
||||
res.render('post', {
|
||||
post: post.getBlogPostTemplateReady()
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// params are incorrect
|
||||
|
||||
@@ -20,7 +20,22 @@
|
||||
<button>submit login</button>
|
||||
</form>
|
||||
|
||||
<h2>create blog post</h2>
|
||||
<form action="/admin/api/v1/newpost" method="POST">
|
||||
<p>content</p>
|
||||
<textarea name="content" 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>
|
||||
<input type="text" name="author">
|
||||
<button>submit login</button>
|
||||
</form>
|
||||
<br>
|
||||
<a href="/admin/api/v1/check">Check if logged in</a>
|
||||
<br>
|
||||
<a href="/admin/api/v1/logout">Logout</a>
|
||||
|
||||
{{> footer }}
|
||||
</body>
|
||||
|
||||
@@ -6,15 +6,15 @@
|
||||
{{> navbar }}
|
||||
|
||||
<h1>blog post</h1>
|
||||
<p>{{ date }}</p>
|
||||
<p>{{ category }}</p>
|
||||
<p>{{ post.date }}</p>
|
||||
<p>{{ post.category }}</p>
|
||||
|
||||
<article>
|
||||
{{{ content }}}
|
||||
{{{ post.content }}}
|
||||
</article>
|
||||
|
||||
<p>{{ author.name }} - {{ author.description }}</p>
|
||||
<img src="{{ author.image }}">
|
||||
<!--<p>{{ author.name }} - {{ author.description }}</p>
|
||||
<img src="{{ author.image }}">-->
|
||||
|
||||
{{> footer }}
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user