Browse Source

Updated server to serve the admin pages using better async methods.

pull/4/head
jrtechs 6 years ago
parent
commit
ed6db5989a
11 changed files with 210 additions and 279 deletions
  1. +27
    -35
      admin/addCategory.js
  2. +49
    -79
      admin/addDownload.js
  3. +15
    -22
      admin/admin.js
  4. +45
    -82
      admin/editPost.js
  5. +15
    -16
      admin/login.js
  6. +47
    -35
      admin/newPost.js
  7. +0
    -0
      downloads/content/Cube-Field-youtube-demo.zip
  8. +4
    -6
      downloads/downloads.js
  9. +2
    -0
      server.js
  10. +5
    -3
      utils/sql.js
  11. +1
    -1
      utils/utils.js

+ 27
- 35
admin/addCategory.js View File

@ -7,50 +7,48 @@ const Promise = require('promise');
/**
* Displays all the categories in the database
* @param res
* @return {*|Promise}
*/
var printCategories = function(res)
const printCategories = function()
{
res.write("<div class=\"blogPost\">");
res.write("<h1 class=\"text-center\">Categories</h1>");
res.write("<div class=\"\"><table class=\"table table-striped\">");
res.write("<thead class=\"thead-dark\">");
res.write("<tr>");
res.write("<td>Name</td><td>URL</td><td>Edit</td>");
res.write("</tr></thead><tbody>");
var html = "<div class=\"blogPost\">" +
"<h1 class=\"text-center\">Categories</h1>" +
"<div class=\"\"><table class=\"table table-striped\">" +
"<thead class=\"thead-dark\">" +
"<tr>" +
"<td>Name</td><td>URL</td><td>Edit</td>" +
"</tr></thead><tbody>";
return new Promise(function(resolve, reject)
{
sql.getCategories().then(function(categories)
{
categories.forEach(function(c)
{
res.write("<tr>");
res.write("<td>" + c.name + "</td>");
res.write("<td>" + c.url + "</td>");
res.write("<td>" + c.category_id + "</td>");
res.write("</tr>");
html +="<tr>" +
"<td>" + c.name + "</td>" +
"<td>" + c.url + "</td>" +
"<td>" + c.category_id + "</td>" +
"</tr>";
});
res.write("</tbody></table></div></div>");
resolve();
resolve(html + "</tbody></table></div></div>");
}).catch(function(error)
{
reject(error);
})
});
};
/**
* Checks for post data regarding adding a new category.
* If a post is made with add_category, it parses the url-- replaces spaces
* with dashes -- and calls a insert method on the database
*
* @param res
* @param postData
* @return {*|Promise}
*/
var processPost = function(res, postData)
const processPost = function(postData)
{
return new Promise(function(resolve, reject)
{
@ -69,30 +67,24 @@ var processPost = function(res, postData)
console.log("error adding category");
}
}
resolve(postData);
resolve("");
});
};
module.exports=
{
main: function(res, postData)
main: function(postData)
{
res.write("<div class=\"col-md-6\">");
return new Promise(function(resolve, reject)
{
utils.include(res, "./admin/addCategory.html");
printCategories(res).then(function()
{
//console.write("categories finished");
return processPost(res, postData);
}).then(function()
Promise.all([utils.include("./admin/addCategory.html"), printCategories(), processPost(postData)]).then(function(html)
{
res.write("</div>");
resolve(postData);
}).catch(function(err)
resolve("<div class=\"col-md-6\">" + html.join('') + "</div></div>");
}).catch(function(error)
{
console.log(err);
console.log("error in cat.js");
reject(error);
})
});
}

+ 49
- 79
admin/addDownload.js View File

@ -11,11 +11,10 @@ const Promise = require('promise');
/**
* Processes post requests from the addDownload form
* @param res
* @param postData
* @returns {*|Promise}
*/
var addDownloadPostData = function(res, postData)
var addDownloadPostData = function(postData)
{
return new Promise(function(resolve, reject)
{
@ -26,12 +25,15 @@ var addDownloadPostData = function(res, postData)
sql.addDownload(post.add_download_name, post.add_download_file).then(function()
{
resolve(postData);
});
resolve("");
}).catch(function(error)
{
reject(error);
})
}
else
{
resolve(postData);
resolve("");
}
});
};
@ -40,27 +42,23 @@ var addDownloadPostData = function(res, postData)
/**
* Displays the addDownload form the the user
*
* @param res
* @param postData
* @returns {*|Promise}
*/
var addDownload = function(res, postData)
const addDownload = function(postData)
{
res.write("<div class=\"col-md-6\">");
//res.write("<div class=\"col-md-6\">");
return new Promise(function(resolve, reject)
{
addDownloadPostData(res, postData).then(function()
{
return utils.include(res, "./admin/addDownload.html");
}).then(function()
Promise.all([addDownloadPostData(postData), utils.include("./admin/addDownload.html")]).then(function(html)
{
res.write("</div>");
resolve(postData);
}).catch(function(err)
console.log("add download is good");
resolve("<div class=\"col-md-6\">" + html.join('') + "</div>");
}).catch(function(error)
{
console.log(err);
reject(err);
});
console.log(error);
reject(error);
})
});
};
@ -68,11 +66,10 @@ var addDownload = function(res, postData)
/**
* Handel form requests from the downloads table
*
* @param res
* @param postData
* @returns {*|Promise}
*/
var displayDownloadsPostData = function(res, postData)
const displayDownloadsPostData = function(postData)
{
return new Promise(function(resolve, reject)
{
@ -90,58 +87,42 @@ var displayDownloadsPostData = function(res, postData)
/**
* Renders a single download row in the downloads table
*
* @param result
* @param download
* @returns {*|Promise}
*/
var renderDownloadRow = function(result, download)
const renderDownloadRow = function(download)
{
return new Promise(function(resolve, reject)
{
result.write("<tr>");
//download name
result.write("<td>" + download.name + "</td>");
//file name
result.write("<td>" + download.file + "</td>");
//download count
result.write("<td>" + download.download_count + "</td>");
//edit
result.write("<td><form action=\"/admin/\" method =\"post\" >\n" +
return "<tr>" +
"<td>" + download.name + "</td>" +
"<td>" + download.file + "</td>" +
"<td>" + download.download_count + "</td>" +
"<td><form action=\"/admin/\" method =\"post\" >\n" +
" <input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
" class=\"btn btn-secondary\"/>\n" +
"<input type='hidden' name='delete_download' value='" + download.download_id + "'/>"+
"</form></td>");
result.write("</tr>");
resolve();
});
"</form></td>" +
"</tr>";
};
/**
* Displays all the download information in a table
* @param res
* @param postData
* @returns {*|Promise}
*/
var displayDownloads = function(res, postData)
const displayDownloads = function(postData)
{
res.write("<div class=\"col-md-6\">");
var html = "<div class=\"col-md-6\">";
return new Promise(function(resolve, reject)
{
displayDownloadsPostData(res, postData).then(function()
displayDownloadsPostData(postData).then(function()
{
res.write("<div class='blogPost'>");
res.write("<h1 class=\"text-center\">Downloads</h1>");
res.write("<div class=\"\"><table class=\"table table-striped\">");
res.write("<thead class=\"thead-dark\"><tr>");
res.write("<td>Download Name</td><td>File</td><td>Download Count</td><td>Delete</td>");
res.write("</tr></thead><tbody>");
html += "<div class='blogPost'>" +
"<h1 class=\"text-center\">Downloads</h1>" +
"<div class=\"\"><table class=\"table table-striped\">" +
"<thead class=\"thead-dark\"><tr>" +
"<td>Download Name</td><td>File</td><td>Download Count</td><td>Delete</td>" +
"</tr></thead><tbody>";
sql.getAllDownloads().then(function(downloads)
@ -150,26 +131,20 @@ var displayDownloads = function(res, postData)
downloads.forEach(function(download)
{
downloadPromises.push(new Promise(function(resolveDownload, reject)
{
renderDownloadRow(res, download).then(function()
{
resolveDownload();
}).catch(function(error)
{
reject(error);
})
}));
downloadPromises.push(renderDownloadRow(download));
});
Promise.all(downloadPromises).then(function()
Promise.all(downloadPromises).then(function(htmls)
{
res.write("</tbody></table></div></div><br>");
res.write("</div>");
resolve(postData);
var htmlafter = "</tbody></table></div></div><br>" +
"</div>";
console.log("display download is good");
resolve(html + htmls.join('') + htmlafter);
});
}).catch(function(error)
{
console.log(error);
reject(error);
});
});
@ -180,23 +155,18 @@ var displayDownloads = function(res, postData)
module.exports=
{
main: function(res, postData)
main: function(postData)
{
res.write("<div class=\"row\">");
return new Promise(function(resolve, reject)
{
addDownload(res, postData).then(function()
Promise.all([addDownload(postData), displayDownloads(postData)]).then(function(html)
{
return displayDownloads(res, postData);
}).then(function()
{
res.write("</div>");
resolve(postData);
}).catch(function(err)
resolve("<div class=\"row\">" + html.join('') + "</div>");
}).catch(function(error)
{
console.log(err);
reject(err);
})
console.log("error in add downloads.js");
reject(error);
});
});
}
};

+ 15
- 22
admin/admin.js View File

@ -1,17 +1,16 @@
const utils = require('../utils/utils.js');
var Promise = require('promise');
const Promise = require('promise');
module.exports=
{
/**
* Method calls the admin widgets it correct order
*
* @param result
* @param fileName
* @param request
* @return {*|Promise}
*/
main: function(result, fileName, request)
main: function(fileName, request)
{
return new Promise(function(resolve, reject)
{
@ -19,31 +18,25 @@ module.exports=
{
utils.getPostData(request).then(function (postData)
{
return require("../admin/newPost.js").main(result, postData);
}).then(function(postData)
{
return require("../admin/addCategory.js").main(result, postData);
}).then(function(postData)
{
result.write("</div>"); //ends main row
return require("../admin/editPost.js").main(result, postData);
}).then(function(postData)
{
return require("../admin/addDownload.js").main(result, postData);
}).then(function()
{
resolve();
}).catch(function(error)
{
reject(error);
Promise.all([require("../admin/newPost.js").main(postData),
require("../admin/addCategory.js").main(postData),
require("../admin/editPost.js").main(postData),
require("../admin/addDownload.js").main(postData)])
.then(function(content)
{
resolve(content.join(''));
}).catch(function(error)
{
reject(error);
});
});
}
else
{
//login page
require("../admin/login.js").main(result, request).then(function()
require("../admin/login.js").main(request).then(function(html)
{
resolve();
resolve(html);
}).catch(function(err)
{
console.log(err);

+ 45
- 82
admin/editPost.js View File

@ -8,54 +8,39 @@ const Promise = require('promise');
const qs = require('querystring');
const sql = require('../utils/sql');
/**
* Displays a single row in the posts view
* @param result
*
* @param post
*/
var renderPostRow = function(result, post)
const renderPostRow = function(post)
{
return new Promise(function(resolve, reject)
{
result.write("<tr>");
//category
result.write("<td>" + post.category_id + "</td>");
//name
result.write("<td>" + post.name + "</td>");
//picture
result.write("<td>" + post.picture_url + "</td>");
//date
result.write("<td>" + post.published + "</td>");
//edit
result.write("<td><form action=\"/admin/\" method =\"post\" >\n" +
" <input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
return "<tr>" +
"<td>" + post.category_id + "</td>" +
"<td>" + post.name + "</td>" +
"<td>" + post.picture_url + "</td>" +
"<td>" + post.published + "</td>" +
"<td><form action=\"/admin/\" method =\"post\" >\n" +
"<input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
" class=\"btn btn-secondary\"/>\n" +
"<input type='hidden' name='edit_post' value='" + post.post_id + "'/>"+
"</form></td>");
result.write("</tr>");
resolve();
});
"</form></td>" +
"</tr>";
};
/**
* Displays all the posts in a table
* @param result
*/
var postsTable = function(result)
const postsTable = function()
{
result.write("<div class='blogPost p-2'>");
result.write("<h1 class=\"text-center\">Posts</h1>");
result.write("<div class=\"\"><table class=\"table table-striped\">");
result.write("<thead class=\"thead-dark\"><tr>");
result.write("<td>Category #</td><td>Name</td><td>Header Picture</td><td>Date</td><td>Edit</td>");
result.write("</tr></thead><tbody>");
var html = "<div class='blogPost p-2'>" +
"<h1 class=\"text-center\">Posts</h1>" +
"<div class=\"\"><table class=\"table table-striped\">" +
"<thead class=\"thead-dark\"><tr>" +
"<td>Category #</td><td>Name</td><td>Header Picture</td><td>Date</td><td>Edit</td>" +
"</tr></thead><tbody>";
return new Promise(function(resolve, reject)
{
sql.getAllPosts().then(function(posts)
@ -63,48 +48,35 @@ var postsTable = function(result)
var postPromises = [];
posts.forEach(function(post)
{
postPromises.push(new Promise(function(res, rej)
{
renderPostRow(result, post).then(function()
{
res();
}).catch(function(error)
{
console.log("error rendering " + post);
rej(error);
})
}));
postPromises.push(renderPostRow(post));
});
Promise.all(postPromises).then(function()
Promise.all(postPromises).then(function(htmls)
{
result.write("</tbody></table></div></div><br>");
resolve();
resolve(html + htmls.join('') + "</tbody></table></div></div><br>");
}).catch(function(error)
{
console.log(error);
console.log("error rendering posts");
reject(error);
});
}).catch(function(error)
{
console.log("error with sql query");
reject(error);
})
});
};
/**
* Displays the edit form for edit posts
* @param result
* @param post_id
*/
var displayRenderForm = function(result, post_id)
const displayRenderForm = function(post_id)
{
return new Promise(function(resolve, reject)
{
sql.getPostById(post_id).then(function(post)
{
result.write("<div class='blogPost'>"+
var html = "<div class='blogPost'>"+
"<h1 class=\"text-center\">Edit Post</h1>"+
"<form action=\"/admin/\" method =\"post\" >"+
" <div class=\"form-group\">\n" +
@ -127,26 +99,24 @@ var displayRenderForm = function(result, post_id)
" class=\"btn btn-lg btn-secondary\"/></div>"+
"<input type='hidden' name='edit_post_2' value='" + post_id + "'/>"+
"</form>"+
"</div><br>"
);
resolve();
"</div><br>";
resolve(html);
}).catch(function(error)
{
console.log(error);
console.log("error getting post from sql in display Reender Form");
reject(error);
});
});
};
/**
* Detects if the post data came from the edit form in posts table or edit post
* in the edit post form. Based on this, this function will call one of two functions
* @param result
* @param postData
*/
var processPost = function(result, postData)
const processPost = function(postData)
{
return new Promise(function(resolve, reject)
{
@ -155,30 +125,27 @@ var processPost = function(result, postData)
if(postParsed.edit_post)
{
//display edit form
displayRenderForm(result, postParsed.edit_post).then(function()
displayRenderForm(postParsed.edit_post).then(function(html)
{
resolve();
resolve(html);
}).catch(function(error)
{
console.log(error);
console.log("error processing the edit post data");
reject(error);
});
}
else if(postParsed.edit_post_2)
{
//insert edit into sql
sql.editPost(postParsed).then(function()
sql.editPost(postParsed).then(function(html)
{
resolve();
resolve(html);
}).catch(function(error)
{
console.log("error inserting edit post data into sql");
reject(error);
});
}
else
{
resolve();
resolve("");
}
});
};
@ -189,25 +156,21 @@ module.exports=
/**
* Method which calls helper functions which processes post data for editing posts
* and calls a function which displays all the posts in a table
* @param result
* @param postData
*/
main: function(result, postData)
main: function(postData)
{
return new Promise(function(resolve, reject)
{
result.write("<br>");
processPost(result, postData).then(function()
Promise.all([processPost(postData),
postsTable()]).then(function(html)
{
return postsTable(result);
}).then(function()
{
resolve(postData);
resolve("<br>" + html.join(''));
}).catch(function(error)
{
console.log("Error in edit post module");
console.log("error in edit post.js");
reject(error);
});
})
});
}
};

+ 15
- 16
admin/login.js View File

@ -2,7 +2,7 @@ const utils = require('../utils/utils.js');
const Promise = require('promise');
const sql = require('../utils/sql');
var processLogin = function(result, request)
const processLogin = function(request)
{
return new Promise(function(resolve, reject)
{
@ -14,13 +14,16 @@ var processLogin = function(result, request)
if(loginResult.pass)
{
request.session.user = loginResult.user;
result.write("<meta http-equiv=\"refresh\" content=\"0\">");
console.log("user has logged in");
resolve("<meta http-equiv=\"refresh\" content=\"0\">");
}
else
{
resolve("");
}
resolve();
}).catch(function(err)
{
console.log(err);
resolve();
reject(err);
})
});
};
@ -28,22 +31,18 @@ var processLogin = function(result, request)
module.exports=
{
main: function(result, request)
main: function(request)
{
console.log("main of login.js");
return new Promise(function(resolve, reject)
{
utils.include(result, './admin/login.html').then(function()
{
console.log("got login html");
return require("../sidebar/sidebar.js").main(result);
}).then(function()
Promise.all([utils.include('./admin/login.html'),
require("../sidebar/sidebar.js").main(),
processLogin(request)]).then(function(html)
{
return processLogin(result, request);
}).then(function()
resolve(html.join('') + "</div>");
}).catch(function(err)
{
result.write("</div>");
resolve();
reject(err);
})
});
},

+ 47
- 35
admin/newPost.js View File

@ -2,55 +2,67 @@ const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
const qs = require('querystring');
var Promise = require('promise');
const Promise = require('promise');
/**
*
* @param postData
* @return {*|Promise}
*/
const processPost = function(postData)
{
return new Promise(function(resolve, reject)
{
var post = qs.parse(postData);
if(post.add_post_name)
{
var urls = post.add_post_name;
urls = urls.split(" ").join("-");
urls =urls.toLowerCase();
var q = "insert into posts (category_id, picture_url, published, name, url) values ";
q += "('" + post.add_post_category + "', '" + post.add_post_picture +
"', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";
sql.insert(q).then(function()
{
var map = require('../utils/generateSiteMap');
map.main();
resolve("");
}).catch(function(error)
{
reject(error);
})
}
else
{
resolve("");
}
});
};
module.exports=
{
/**
*
* @param res
* @param postData
* @return {*}
*/
main: function(res, postData)
{
utils.include(res, "./admin/newPost.html");
return this.processPost(res, postData);
},
/**
*
* @param res
* @param postData
* @return {*|Promise}
*/
processPost: function(res, postData)
main: function(postData)
{
return new Promise(function(resolve, reject)
{
var post = qs.parse(postData);
if(post.add_post_name)
Promise.all([utils.include("./admin/newPost.html"), processPost(postData)]).then(function(html)
{
var urls = post.add_post_name;
urls = urls.split(" ").join("-");
urls =urls.toLowerCase();
var q = "insert into posts (category_id, picture_url, published, name, url) values ";
q += "('" + post.add_post_category + "', '" + post.add_post_picture +
"', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";
sql.insert(q).then(function()
{
var map = require('../utils/generateSiteMap');
map.main();
resolve();
})
}
else
resolve(html.join(''));
}).catch(function(error)
{
resolve(postData);
}
console.log(error);
reject(error);
})
});
}
};

downloads/content/Cube-Field-youtube-domo.zip → downloads/content/Cube-Field-youtube-demo.zip View File


+ 4
- 6
downloads/downloads.js View File

@ -3,9 +3,7 @@
including html files easier for me programming.
*/
const fs = require('fs');
var Promise = require('promise');
const Promise = require('promise');
const utils = require('../utils/utils.js');
@ -34,7 +32,7 @@ module.exports=
console.log(result);
if(result.length == 1)
{
var file = './downloads/content/' + result[0].file;
const file = './downloads/content/' + result[0].file;
res.download(file);
}
else
@ -48,9 +46,9 @@ module.exports=
}
else
{
utils.print404(res).then(function()
utils.print404().then(function(content)
{
resolve();
resolve(content);
})
}
});

+ 2
- 0
server.js View File

@ -26,6 +26,7 @@ app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxA
const port = 8000;
app.use(express.static(__dirname + './', { maxAge: 86400000 }));
/**
* Parses the request url and calls correct JS files
@ -83,6 +84,7 @@ app.use(function(request, res)
}).catch(function(err)
{
console.log(err);
throw err;
});
}
}

+ 5
- 3
utils/sql.js View File

@ -10,7 +10,7 @@ const qs = require('querystring');
const utils = require('../utils/utils.js');
var con = mysql.createConnection({
const con = mysql.createConnection({
host: "localhost",
user: "blog_user",
password: utils.getFileLine('../sql_secret'),
@ -22,13 +22,14 @@ con.connect(function(err) {
if (err) throw err;
});
/**
* Function used to query the database for records
*
* @param sqlStatement
* @returns {Array}
*/
var fetch = function(sqlStatement)
const fetch = function(sqlStatement)
{
return new Promise(function(resolve, reject)
{
@ -44,6 +45,7 @@ var fetch = function(sqlStatement)
});
};
module.exports=
{
/**
@ -251,7 +253,7 @@ module.exports=
*/
checkLogin: function(postData)
{
var post = qs.parse(postData);
const post = qs.parse(postData);
return new Promise(function(resolve, reject)
{
var result = Object();

+ 1
- 1
utils/utils.js View File

@ -5,7 +5,7 @@
const fs = require('fs');
var Promise = require('promise');
const Promise = require('promise');
module.exports=
{

Loading…
Cancel
Save