update function names

This commit is contained in:
mrjvs 2018-10-16 16:21:08 +02:00
parent 815e5aba03
commit 887f270b1d
9 changed files with 49 additions and 52 deletions

View File

@ -6,7 +6,7 @@ common api returns
*/
// use for any api return. it has basic layout used for every return.
function sendApiReturn(res, data, errors) {
function sendReturn(res, data, errors) {
res.status(200).json(
// combine 2 objects
Object.assign({
@ -59,7 +59,7 @@ function sendApiError(res, code, errors) {
}
module.exports = {
sendApiReturn,
sendReturn,
sendApi404,
sendApiGenericError,
sendApiError,

View File

@ -9,7 +9,7 @@ Middleware file for authentication checking
const apiHelper = require('../helpers/api');
// middleware to use if admin authentication is required
function adminAuthenticationRequired(req, res, next) {
function adminAuthNeeded(req, res, next) {
if (req.isAuthenticated() && req.user.role && req.user.role === 'admin') {
return next();
} else {
@ -18,11 +18,11 @@ function adminAuthenticationRequired(req, res, next) {
}
// middleware to use if authentication is optional
function authenticationOptional(req, res, next) {
function authOptional(req, res, next) {
return next();
}
module.exports = {
adminAuthenticationRequired,
authenticationOptional
adminAuthNeeded,
authOptional
};

View File

@ -56,10 +56,7 @@ const blogPostSchema = new mongoose.Schema({
}
});
blogPostSchema.methods.getContentAsHTML = function() {
return this.content;
};
blogPostSchema.methods.getBlogPostTemplateReady = function(callback) {
blogPostSchema.methods.postTemplate = function(callback) {
const self = this;
postAuthor.findById(this.meta.author, function (err, author) {
callback(err, {
@ -67,11 +64,11 @@ blogPostSchema.methods.getBlogPostTemplateReady = function(callback) {
title: self.name,
date: self.meta.date,
category: self.meta.category,
author: author.getPostAuthorTemplateReady()
author: author.authorTemplate()
});
});
};
blogPostSchema.methods.getBlogPostShortTemplateReady = function() {
blogPostSchema.methods.postShortTemplate = function() {
return {
content: this.short,
title: this.name,
@ -79,7 +76,7 @@ blogPostSchema.methods.getBlogPostShortTemplateReady = function() {
};
};
blogPostSchema.statics.convertMarkdownToHtml = function(markdown) {
blogPostSchema.statics.markdownToHtml = function(markdown) {
return converter.makeHtml(markdown);
};
blogPostSchema.statics.getPost = function(date, slug, callback) {
@ -89,12 +86,12 @@ blogPostSchema.statics.getPost = function(date, slug, callback) {
}, callback);
};
// not tested
blogPostSchema.statics.getLatestBlogPostShortTemplateReady = function(amount, callback) {
blogPostSchema.statics.latestPostsShortTemlate = 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();
out += posts[i].postShortTemplate();
callback(err, out);
});
};

View File

@ -27,7 +27,7 @@ const postAuthorSchema = new mongoose.Schema({
}
});
postAuthorSchema.methods.getPostAuthorTemplateReady = function() {
postAuthorSchema.methods.authorTemplate = function() {
return {
name: this.name,
description: this.description,

View File

@ -43,7 +43,7 @@ router.get('/admin', (req, res) => {
// TODO make login somehow display errors in correct format.
// middleware does the authentication work. this just returns a success
router.post('/admin/api/v1/login', passport.authenticate('adminUserStrategy'), function (req, res) {
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
username: req.user.username,
role: req.user.role ? req.user.role : undefined
});
@ -67,7 +67,7 @@ router.post('/admin/api/v1/login', passport.authenticate('adminUserStrategy'), f
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthNeeded, (req, res) => {
if (!req.body) {
// no post body
apiHelper.sendApiGenericError(res);
@ -82,7 +82,7 @@ router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationReq
// saving to database
newUser.save().then(() => {
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
username: req.user.username,
role: req.user.role ? req.user.role : undefined
});
@ -109,7 +109,7 @@ router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationReq
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthNeeded, (req, res) => {
if (!req.body) {
// no post body
apiHelper.sendApiGenericError(res);
@ -120,7 +120,7 @@ router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthentication
adminUser.adminUserModel.findByIdAndDelete(id, (err) => {
if (err) return apiHelper.sendApiError(res, 500, [err]);
// successfull
apiHelper.sendApiReturn(res, {});
apiHelper.sendReturn(res, {});
});
});
@ -136,7 +136,7 @@ router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthentication
* errors: Strings[messages]
* }
*/
router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthNeeded, (req, res) => {
adminUser.adminUserModel.find({}, (err, admins) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return apiHelper.sendApiError(res, 500, [err]);
@ -148,7 +148,7 @@ router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthenticationRe
output.push(admins[i]);
}
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
admins: output
});
});
@ -167,8 +167,8 @@ router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthenticationRe
* errors: Strings[messages]
* }
*/
router.get('/admin/api/v1/check', adminUserMiddleware.authenticationOptional, (req, res) => {
apiHelper.sendApiReturn(res, {
router.get('/admin/api/v1/check', adminUserMiddleware.authOptional, (req, res) => {
apiHelper.sendReturn(res, {
isAuthed: req.user ? true : false,
role: req.user ? (req.user.role ? req.user.role : undefined) : undefined
});
@ -185,9 +185,9 @@ router.get('/admin/api/v1/check', adminUserMiddleware.authenticationOptional, (r
* errors: Strings[messages]
* }
*/
router.get('/admin/api/v1/logout', adminUserMiddleware.adminAuthenticationRequired, (req, res) => {
router.get('/admin/api/v1/logout', adminUserMiddleware.adminAuthNeeded, (req, res) => {
req.logout();
apiHelper.sendApiReturn(res, {});
apiHelper.sendReturn(res, {});
});
/*
@ -210,13 +210,13 @@ router.get('/admin/api/v1/logout', adminUserMiddleware.adminAuthenticationRequir
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthNeeded, function (req, res) {
if (!req.body) return apiHelper.sendApiGenericError(res);
const { content, title, author, category, short } = req.body;
const newBlogPost = new blogPost.blogPostModel({
content: blogPost.blogPostModel.convertMarkdownToHtml(content),
content: blogPost.blogPostModel.markdownToHtml(content),
name: title,
short,
meta: {
@ -232,7 +232,7 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
// saving post to database
newBlogPost.save().then((post) => {
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
url: moment(post.meta.date, 'YYYY-MM-DD') + '/' + post.meta.slug
});
}).catch((rejection) => {
@ -262,7 +262,7 @@ router.post('/admin/api/v1/newpost', adminUserMiddleware.adminAuthenticationRequ
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/editpost', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
router.post('/admin/api/v1/editpost', adminUserMiddleware.adminAuthNeeded, function (req, res) {
if (!req.body) return apiHelper.sendApiGenericError(res);
@ -276,7 +276,7 @@ router.post('/admin/api/v1/editpost', adminUserMiddleware.adminAuthenticationReq
}, (err, post) => {
if (err) return apiHelper.sendApiError(res, 500, [err]);
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
url: moment(post.meta.date, 'YYYY-MM-DD') + '/' + post.meta.slug
});
});
@ -299,7 +299,7 @@ router.post('/admin/api/v1/editpost', adminUserMiddleware.adminAuthenticationReq
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/newauthor', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
router.post('/admin/api/v1/newauthor', adminUserMiddleware.adminAuthNeeded, function (req, res) {
if (!req.body) return apiHelper.sendApiGenericError(res);
@ -312,7 +312,7 @@ router.post('/admin/api/v1/newauthor', adminUserMiddleware.adminAuthenticationRe
// saving author to database
newAuthor.save().then((author) => {
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
id: author.id
});
}).catch((rejection) => {
@ -340,7 +340,7 @@ router.post('/admin/api/v1/newauthor', adminUserMiddleware.adminAuthenticationRe
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/editauthor', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
router.post('/admin/api/v1/editauthor', adminUserMiddleware.adminAuthNeeded, function (req, res) {
if (!req.body) return apiHelper.sendApiGenericError(res);
@ -354,7 +354,7 @@ router.post('/admin/api/v1/editauthor', adminUserMiddleware.adminAuthenticationR
}, (err, author) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return apiHelper.sendApiError(res, 500, [err]);
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
id: author.id
});
});
@ -377,7 +377,7 @@ router.post('/admin/api/v1/editauthor', adminUserMiddleware.adminAuthenticationR
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthNeeded, function (req, res) {
if (!req.body) return apiHelper.sendApiGenericError(res);
@ -401,7 +401,7 @@ router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthentication
// saving progress to database
newProgress.save().then((progress) => {
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
id: progress.id
});
}).catch((rejection) => {
@ -429,7 +429,7 @@ router.post('/admin/api/v1/newprogress', adminUserMiddleware.adminAuthentication
* errors: Strings[messages]
* }
*/
router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthenticationRequired, function (req, res) {
router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthNeeded, function (req, res) {
if (!req.body) return apiHelper.sendApiGenericError(res);
@ -453,7 +453,7 @@ router.post('/admin/api/v1/editprogress', adminUserMiddleware.adminAuthenticatio
}, (err, progress) => {
// TODO format exception so it doesnt have a huge list of errors
if (err) return apiHelper.sendApiError(res, 500, [err]);
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
id: progress.id
});
});

View File

@ -21,12 +21,12 @@ router.get('/news/:date/:title', (req, res) => {
// error exists or no post exists with the date and name
if (err || !post) {
console.log('error: ' + err + ' and post: ' + post);
return utilHelper.sendDefault404(res);
return utilHelper.send404(res);
}
// render blogpost
post.getBlogPostTemplateReady((err, postTemplate) => {
if (err) return utilHelper.sendDefault404(res);
post.postTemplate((err, postTemplate) => {
if (err) return utilHelper.send404(res);
res.render('post', {
post: postTemplate
});
@ -34,7 +34,7 @@ router.get('/news/:date/:title', (req, res) => {
});
} else {
// params are incorrect
utilHelper.sendDefault404(res);
utilHelper.send404(res);
}
});
@ -43,13 +43,13 @@ router.get('/news', (req, res) => {
// sort blogposts on date descending
blogPostModel.find({}).sort({'meta.date': 'desc'}).exec(function(err, posts) {
if (err || !posts) {
return utilHelper.sendDefault404(res);
return utilHelper.send404(res);
}
// makes posts template ready
const postCollection = [];
for (let i = 0, l = posts.length; i < l; i++) {
postCollection.push(posts[i].getBlogPostShortTemplateReady());
postCollection.push(posts[i].postShortTemplate());
}
res.render('post-collection', {
@ -74,7 +74,7 @@ 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 apiHelper.sendApiError(res, 500, [err]);
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
authorList: authors
});
});
@ -96,7 +96,7 @@ 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 apiHelper.sendApiError(res, 500, [err]);
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
postList: posts
});
});

View File

@ -56,7 +56,7 @@ router.post('/api/v1/sendmessage', function (req, res) {
}
}, () => {
// sends success
apiHelper.sendApiReturn(res, {});
apiHelper.sendReturn(res, {});
});
// error handling

View File

@ -16,7 +16,7 @@ const progressListModel = require('../models/progress-list').progressListModel;
router.get('/progress', (req, res) => {
progressListModel.find({}, (err, progress) => {
if (err) return utilHelper.sendDefault404(res);
if (err) return utilHelper.send404(res);
// filtering games and backend
const games = progress.filter(i => i.isGame);
@ -46,7 +46,7 @@ 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 apiHelper.sendApiError(res, 500, [err]);
apiHelper.sendApiReturn(res, {
apiHelper.sendReturn(res, {
progressList: progress
});
});

View File

@ -74,7 +74,7 @@ app.use('/', locations.posts);
app.use('/', locations.admin);
app.use('/', locations.progress);
app.use((req, res) => {
utilHelper.sendDefault404(res);
utilHelper.send404(res);
});
// TODO improve error handling