static text + progresslist post and edit + postauthor post and edit

This commit is contained in:
mrjvs 2018-10-14 18:00:35 +02:00
parent 5c5772b76d
commit d41967816c
11 changed files with 474 additions and 21 deletions

View File

@ -8,7 +8,7 @@ file containing the model file for a blog post
// imports
const mongoose = require('mongoose');
const common = require('../helpers/common');
//const postAuthor = require('./post-author').postAuthorModel;
const postAuthor = require('./post-author').postAuthorModel;
const showdown = require('showdown');
const converter = new showdown.Converter();
converter.setFlavor('github');
@ -53,14 +53,17 @@ const blogPostSchema = new mongoose.Schema({
blogPostSchema.methods.getContentAsHTML = function() {
return this.content;
};
blogPostSchema.methods.getBlogPostTemplateReady = function() {
return {
content: this.content,
title: this.name,
date: this.meta.date,
category: this.meta.category/*,
author: postAuthor.findById(this.meta.author).getPostAuthorTemplateReady()*/
};
blogPostSchema.methods.getBlogPostTemplateReady = function(callback) {
const self = this;
postAuthor.findById(this.meta.author, function (err, author) {
callback(err, {
content: self.content,
title: self.name,
date: self.meta.date,
category: self.meta.category,
author: author.getPostAuthorTemplateReady()
});
});
};
blogPostSchema.statics.convertMarkdownToHtml = function(markdown) {

View File

@ -1,7 +1,7 @@
/*
blog-post.js -
file containing the model file for a blog post
post-author.js -
file containing the model for authors
*/
@ -28,9 +28,9 @@ const postAuthorSchema = new mongoose.Schema({
postAuthorSchema.methods.getPostAuthorTemplateReady = function() {
return {
authorName: this.name,
authorDescription: this.description,
authorProfilePicture: this.image
name: this.name,
description: this.description,
profilePicture: this.image
};
};

36
models/progress-list.js Normal file
View File

@ -0,0 +1,36 @@
/*
progress-list.js -
file containing the model for progress list
*/
// imports
const mongoose = require('mongoose');
// progress text schema
const progressListSchema = new mongoose.Schema({
state: {
type: Number, /* 0 - nothing, 1 - no support, 2 - partially working, 3 - works */
default: 0
},
isGame: {
type: Boolean, /* true - is game list, false - is backend service */
required: [true, 'isGame is required']
},
title: {
type: String,
required: [true, 'Title is required']
},
description: {
type: String,
required: [true, 'Description is required']
}
});
const progressListModel = mongoose.model('progress', progressListSchema);
module.exports = {
progressListModel,
progressListSchema
};

View File

@ -12,6 +12,8 @@ const common = require('../helpers/common');
const adminUserMiddleware = require('../middleware/admin-authentication');
const adminUser = require('../models/admin-user');
const blogPost = require('../models/blog-post');
const postAuthor = require('../models/post-author');
const progressList = require('../models/progress-list');
// display admin panel
router.get('/admin', (req, res) => {
@ -174,6 +176,216 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
});
});
/*
* /admin/api/v1/editpost
*
* edits a blog post
*
* post {
* id - id of the blog post to be edited
* content - content of the blog post IN HTML
* 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/editpost', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
if (!req.body) return common.sendApiGenericError(res);
const { id, content, title, author, category } = req.body;
blogPost.blogPostModel.findByIdAndUpdate(id, {
'content': content,
'name': title,
'meta.author': author,
'meta.category': category
}, (err, post) => {
if (err) return common.sendApiError(res, 500, [err]);
common.sendApiReturn(res, {
url: common.convertDateToString(post.meta.date) + '/' + post.meta.urlTitle
});
});
});
/*
* /admin/api/v1/newauthor
*
* creates new author
*
* post {
* name - author name
* description - author description
* image - image url for profile picture
* }
* return {
* code: http code
* success: boolean - true if author succesfull
* id: string | undefined - id of the new author if successfull
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/newauthor', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
if (!req.body) return common.sendApiGenericError(res);
const { name, description, image } = req.body;
const newAuthor = new postAuthor.postAuthorModel({
name,
description,
image
});
newAuthor.save().then((author) => {
// successfull
common.sendApiReturn(res, {
id: author.id
});
}).catch((rejection) => {
// TODO format exception so it doesnt have a huge list of errors
common.sendApiError(res, 500, [rejection]);
return;
});
});
/*
* /admin/api/v1/editauthor
*
* edit an existing author
*
* post {
* id - id of author to edit
* name - author name
* description - author description
* image - image url for profile picture
* }
* return {
* code: http code
* success: boolean - true if author succesfull
* id: String - id of the edited author
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/editauthor', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
if (!req.body) return common.sendApiGenericError(res);
const { id, name, description, image } = req.body;
postAuthor.postAuthorModel.findByIdAndUpdate(id, {
name,
description,
image
}, (err, author) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return common.sendApiError(res, 500, [err]);
common.sendApiReturn(res, {
id: author.id
});
});
});
/*
* /admin/api/v1/newprogress
*
* creates a new progress entry
*
* post {
* title - progress entry name
* description - progress entry description
* state - 0: backend service entry, 1: no support, 2: partial support, 3: fullly working
* }
* return {
* code: http code
* success: boolean - true if progress succesfull
* id: String | undefined - sends if successfull
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
if (!req.body) return common.sendApiGenericError(res);
const { title, description } = req.body;
let { state } = req.body;
let isGame = false;
if (state != '1' && state != '2' && state != '3') {
state = undefined;
} else {
state = parseInt(state);
isGame = true;
}
const newProgress = new progressList.progressListModel({
title,
description,
isGame,
state
});
newProgress.save().then((progress) => {
// successfull
common.sendApiReturn(res, {
id: progress.id
});
}).catch((rejection) => {
// TODO format exception so it doesnt have a huge list of errors
common.sendApiError(res, 500, [rejection]);
return;
});
});
/*
* /admin/api/v1/editprogress
*
* edit an existing progress entry
*
* post {
* title - progress entry name
* description - progress entry description
* state - 0: backend service entry, 1: no support, 2: partial support, 3: fullly working
* id - id of entry you want to edit
* }
* return {
* code: http code
* success: boolean - true if progress succesfull
* id: String - id of the edited progress entry
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
if (!req.body) return common.sendApiGenericError(res);
const { title, description, id } = req.body;
let { state } = req.body;
let isGame = false;
if (state != '1' && state != '2' && state != '3') {
state = undefined;
} else {
state = parseInt(state);
isGame = true;
}
progressList.progressListModel.findByIdAndUpdate(id, {
title,
description,
state,
isGame
}, (err, progress) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return common.sendApiError(res, 500, [err]);
common.sendApiReturn(res, {
id: progress.id
});
});
});
// configure api 404
router.use('/admin/api', (req, res) => {
common.sendApi404(res);

View File

@ -9,6 +9,7 @@ file for handling routes regarding blog posts.
const router = require('express').Router();
const common = require('../helpers/common');
const blogPostModel = require('../models/blog-post').blogPostModel;
const postAuthorModel = require('../models/post-author').postAuthorModel;
// display blog post
router.get('/news/:date/:title', (req, res) => {
@ -23,8 +24,11 @@ router.get('/news/:date/:title', (req, res) => {
}
// render blogpost
res.render('post', {
post: post.getBlogPostTemplateReady()
post.getBlogPostTemplateReady((err, postTemplate) => {
if (err) return common.sendDefault404(res);
res.render('post', {
post: postTemplate
});
});
});
} else {
@ -33,5 +37,49 @@ router.get('/news/:date/:title', (req, res) => {
}
});
/*
* /api/v1/listauthors
*
* gets a list of all authors
*
* return {
* code: http code
* success: boolean - true if author succesfull
* authorList: Objects[{_id, name, description, image}] - list of authors with information
* errors: Strings[messages]
* }
*/
router.get('/api/v1/listauthors', function (req, res) {
postAuthorModel.find({}, (err, authors) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return common.sendApiError(res, 500, [err]);
common.sendApiReturn(res, {
authorList: authors
});
});
});
/*
* /api/v1/listblog
*
* gets a list of all posts
*
* return {
* code: http code
* success: boolean - true if post succesfull
* postList: Objects[{_id, content, meta}] - list of posts with information
* errors: Strings[messages]
* }
*/
router.get('/api/v1/listblog', function (req, res) {
blogPostModel.find({}, (err, posts) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return common.sendApiError(res, 500, [err]);
common.sendApiReturn(res, {
postList: posts
});
});
});
// export router
module.exports = router;

60
routes/progress.js Normal file
View File

@ -0,0 +1,60 @@
/*
progress.js -
file for handling routes regarding progress
*/
// imports
const router = require('express').Router();
const common = require('../helpers/common');
const staticText = require('../static-text.json');
const progressListModel = require('../models/progress-list').progressListModel;
// display progress
router.get('/progress', (req, res) => {
progressListModel.find({}, (err, progress) => {
if (err) return common.sendDefault404(res);
console.log(progress);
let games = [];
let backends = [];
for (const i in progress) {
if (progress[i].isGame)
games += progress[i];
else
backends += progress[i];
}
console.log(games + backends);
res.render('progress', {
games,
backends,
summary: staticText.progressSummary
});
});
});
/*
* /api/v1/listprogress
*
* gets a list of progress
*
* return {
* code: http code
* success: boolean - true if progress succesfull
* progressList: Objects[{_id, title, description, state}] - list of progress with information
* errors: Strings[messages]
* }
*/
router.get('/api/v1/listprogress', function (req, res) {
progressListModel.find({}, (err, progress) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return common.sendApiError(res, 500, [err]);
common.sendApiReturn(res, {
progressList: progress
});
});
});
// export router
module.exports = router;

View File

@ -59,7 +59,8 @@ app.set('view engine', '.hbs');
const locations = {
home: require('./routes/home'),
posts: require('./routes/blog'),
admin: require('./routes/admin')
admin: require('./routes/admin'),
progress: require('./routes/progress')
};
// static files
@ -68,6 +69,7 @@ app.use('/assets', express.static('assets'));
app.use('/', locations.home);
app.use('/', locations.posts);
app.use('/', locations.admin);
app.use('/', locations.progress);
app.use((req, res) => {
common.sendDefault404(res);
});

4
static-text.json Normal file
View File

@ -0,0 +1,4 @@
{
"progressSummary": "This is a progress summary",
"headerText": "A work-in-progress server replacement for nintendo 3ds and wiiu"
}

View File

@ -30,8 +30,80 @@
<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>
<button>create post</button>
</form>
<h2>edit blog post</h2>
<form action="/admin/api/v1/editpost" method="POST">
<p>content</p>
<textarea name="content" 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>
<input type="text" name="author">
<button>edit post</button>
</form>
<h2>new author</h2>
<form action="/admin/api/v1/newauthor" method="POST">
<p>name</p>
<input type="text" name="name">
<p>description</p>
<input type="text" name="description">
<p>image link</p>
<input type="text" name="image">
<button>submit author</button>
</form>
<h2>edit author</h2>
<form action="/admin/api/v1/editauthor" method="POST">
<p>id</p>
<input type="text" name="id">
<p>name</p>
<input type="text" name="name">
<p>description</p>
<input type="text" name="description">
<p>image link</p>
<input type="text" name="image">
<button>edit author</button>
</form>
<h2>new progress entry</h2>
<form action="/admin/api/v1/newprogress" method="POST">
<p>title</p>
<input type="text" name="title">
<p>description</p>
<input type="text" name="description">
<p>state</p>
<p>0 - backend service<br>1 - no support<br>2 - partial support<br>3 - fully working</p>
<input type="text" name="state">
<button>submit progress</button>
</form>
<h2>edit progress entry</h2>
<form action="/admin/api/v1/editprogress" method="POST">
<p>id</p>
<input type="text" name="id">
<p>title</p>
<input type="text" name="title">
<p>description</p>
<input type="text" name="description">
<p>state</p>
<p>0 - backend service<br>1 - no support<br>2 - partial support<br>3 - fully working</p>
<input type="text" name="state">
<button>edit progress</button>
</form>
<br>
<a href="/api/v1/listblog">List of blog posts</a>
<br>
<a href="/api/v1/listprogress">List of progress</a>
<br>
<a href="/api/v1/listauthors">List of authors</a>
<br>
<a href="/admin/api/v1/check">Check if logged in</a>
<br>

View File

@ -5,7 +5,7 @@
<body>
{{> navbar }}
<h1>blog post</h1>
<h1>{{ post.title }}</h1>
<p>{{ post.date }}</p>
<p>{{ post.category }}</p>
@ -13,9 +13,10 @@
{{{ post.content }}}
</article>
<!--<p>{{ author.name }} - {{ author.description }}</p>
<img src="{{ author.image }}">-->
<p>{{ post.author.name }} - {{ post.author.description }}</p>
<img src="{{ post.author.profilePicture }}">
{{> footer }}
</body>
</html>

15
views/progress.hbs Normal file
View File

@ -0,0 +1,15 @@
<h1>progress summary</h1>
<p>{{ summary }}</p>
<br>
<h2>game support</h2>
{{#each games }}
<strong>{{ this.title }}</strong>
<p>{{ this.description }}</p>
<p>state: {{ this.state }}</p>
{{/each}}
<br><br>
<h2>backend support</h2>
{{#each backends }}
<strong>{{ this.title }}</strong>
<p>{{ this.description }}</p>
{{/each}}