@ -0,0 +1,131 @@ | |||
/** | |||
* File which deals with adding and removing downloads from | |||
* the admin section of the website. | |||
* | |||
* @author Jeffery Russell 6-30-18 | |||
*/ | |||
const TEMPLATE_FILE = "admin/adminDownloads.html"; | |||
//file IO | |||
const utils = require('../utils/utils.js'); | |||
const includes = require('../includes/includes.js'); | |||
//updates db | |||
const sql = require('../utils/sql'); | |||
//parses post data | |||
const qs = require('querystring'); | |||
/** | |||
* Processes post requests from the addDownload form | |||
* | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const addDownloadPostData = function(postData) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
const post = qs.parse(postData); | |||
if(post.add_download) | |||
{ | |||
sql.addDownload(post.add_download_name, post.add_download_file) | |||
.then(function() | |||
{ | |||
resolve(); | |||
}).catch(function(error) | |||
{ | |||
reject(error); | |||
}) | |||
} | |||
else | |||
{ | |||
resolve(); | |||
} | |||
}); | |||
}; | |||
/** | |||
* Handel form requests from the downloads table | |||
* | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const removeDownloads = function(postData) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
const post = qs.parse(postData); | |||
if(post.delete_download) | |||
{ | |||
sql.removeDownload(post.delete_download).then(function() | |||
{ | |||
resolve(); | |||
}).catch(function(err) | |||
{ | |||
reject(err); | |||
}); | |||
} | |||
else | |||
{ | |||
resolve(); | |||
} | |||
}); | |||
}; | |||
/** | |||
* Displays all the download information in a table | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const displayDownloads = function(postData, templateContext) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
sql.getAllDownloads().then(function(downloads) | |||
{ | |||
templateContext.downloads = downloads; | |||
resolve(); | |||
}).catch(function(error) | |||
{ | |||
reject(error); | |||
}); | |||
}); | |||
}; | |||
module.exports= | |||
{ | |||
/** | |||
* Renders tha download section of the admin page | |||
* | |||
* @param postData | |||
* @returns {Promise} | |||
*/ | |||
main: function(postData, templateContext) | |||
{ | |||
console.log(postData); | |||
console.log("downloads page called"); | |||
return new Promise(function(resolve, reject) | |||
{ | |||
Promise.all([includes.fetchTemplate(TEMPLATE_FILE), | |||
addDownloadPostData(postData), | |||
removeDownloads(postData), | |||
displayDownloads(postData, templateContext)]).then(function(template) | |||
{ | |||
resolve(template[0]); | |||
}).catch(function(error) | |||
{ | |||
console.log("error in add downloads.js"); | |||
reject(error); | |||
}); | |||
}); | |||
} | |||
}; |
@ -1,19 +0,0 @@ | |||
<div class="blogPost"> | |||
<h1 class="text-center">Add Download</h1> | |||
<form action="/admin" method ="post" class="p-2"> | |||
<div class="form-group"> | |||
<input class="form-control" type="text" name="add_download_name" required> | |||
<label>Download Name</label> | |||
</div> | |||
<div class="form-group"> | |||
<input class="form-control" type="text" name="add_download_file" required> | |||
<label>File name</label> | |||
</div> | |||
<div class="text-center"> | |||
<input type="submit" name="add_download" value="Add Download" | |||
class="btn btn-lg btn-secondary"/> | |||
</div> | |||
</form> | |||
</div> | |||
<br> |
@ -1,191 +0,0 @@ | |||
/** | |||
* File which deals with adding and removing downloads from | |||
* the admin section of the website. | |||
* | |||
* @author Jeffery Russell 6-30-18 | |||
*/ | |||
//file IO | |||
const utils = require('../../utils/utils.js'); | |||
//updates db | |||
const sql = require('../../utils/sql'); | |||
//parses post data | |||
const qs = require('querystring'); | |||
/** | |||
* Processes post requests from the addDownload form | |||
* | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const addDownloadPostData = function(postData) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
const post = qs.parse(postData); | |||
if(post.add_download) | |||
{ | |||
sql.addDownload(post.add_download_name, post.add_download_file) | |||
.then(function() | |||
{ | |||
resolve(""); | |||
}).catch(function(error) | |||
{ | |||
reject(error); | |||
}) | |||
} | |||
else | |||
{ | |||
resolve(""); | |||
} | |||
}); | |||
}; | |||
/** | |||
* Displays the addDownload form the the user | |||
* | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const addDownload = function(postData) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
Promise.all([addDownloadPostData(postData), | |||
utils.include("./admin/downloads/addDownload.html")]).then(function(html) | |||
{ | |||
resolve("<div class=\"col-md-6\">" + html.join('') + "</div>"); | |||
}).catch(function(error) | |||
{ | |||
reject(error); | |||
}) | |||
}); | |||
}; | |||
/** | |||
* Handel form requests from the downloads table | |||
* | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const displayDownloadsPostData = function(postData) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
const post = qs.parse(postData); | |||
if(post.delete_download) | |||
{ | |||
sql.removeDownload(post.delete_download).then(function() | |||
{ | |||
resolve(postData); | |||
}).catch(function(err) | |||
{ | |||
reject(err); | |||
}); | |||
} | |||
else | |||
{ | |||
resolve(postData); | |||
} | |||
}); | |||
}; | |||
/** | |||
* Renders a single download row in the downloads table | |||
* | |||
* @param download | |||
* @returns {*|Promise} | |||
*/ | |||
const renderDownloadRow = function(download) | |||
{ | |||
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=\"Delete\"\n" + | |||
" class=\"btn btn-secondary\"/>\n" + | |||
"<input type='hidden' name='delete_download' value='" + | |||
download.download_id + "'/>"+ | |||
"</form></td>" + | |||
"</tr>"; | |||
}; | |||
/** | |||
* Displays all the download information in a table | |||
* @param postData | |||
* @returns {*|Promise} | |||
*/ | |||
const displayDownloads = function(postData) | |||
{ | |||
var html = "<div class=\"col-md-6\">"; | |||
return new Promise(function(resolve, reject) | |||
{ | |||
displayDownloadsPostData(postData).then(function() | |||
{ | |||
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) | |||
{ | |||
var downloadPromises = []; | |||
downloads.forEach(function(download) | |||
{ | |||
downloadPromises.push(renderDownloadRow(download)); | |||
}); | |||
Promise.all(downloadPromises).then(function(htmls) | |||
{ | |||
const htmlafter = "</tbody></table></div></div><br>" + | |||
"</div>"; | |||
resolve(html + htmls.join('') + htmlafter); | |||
}); | |||
}).catch(function(error) | |||
{ | |||
reject(error); | |||
}); | |||
}); | |||
}); | |||
}; | |||
module.exports= | |||
{ | |||
/** | |||
* Renders tha download section of the admin page | |||
* | |||
* @param postData | |||
* @returns {Promise} | |||
*/ | |||
main: function(postData) | |||
{ | |||
return new Promise(function(resolve, reject) | |||
{ | |||
Promise.all([addDownload(postData), | |||
displayDownloads(postData)]).then(function(html) | |||
{ | |||
resolve("<div class=\"row\">" + html.join('') + "</div>"); | |||
}).catch(function(error) | |||
{ | |||
console.log("error in add downloads.js"); | |||
reject(error); | |||
}); | |||
}); | |||
} | |||
}; |
@ -0,0 +1,103 @@ | |||
<div class="row"> | |||
<div class="col-md-6"> | |||
<div class="blogPost"> | |||
<h1 class="text-center">Server Controls</h1> | |||
<div class="text-center"> | |||
<form action="/admin" method="post"> | |||
<input type="submit" name="clearCache" value="Clear Cache" class="btn btn-lg btn-secondary" /> | |||
<input type="hidden" name="clear_cache" value="true"> | |||
</form> | |||
<br> | |||
<form action="/admin" method="post"> | |||
<input type="submit" name="gitPull" value="Pull from Git" class="btn btn-lg btn-secondary" /> | |||
<input type="hidden" name="git_pull" value="true"> | |||
</form> | |||
</div> | |||
</div> | |||
</div> | |||
<div class="col-md-6"> | |||
<div class="blogPost"> | |||
<h1 class="text-center">Add Category</h1> | |||
<form action="/admin" method ="post" class="p-2"> | |||
<div class="form-group"> | |||
<input class="form-control" type="text" name="add_category" required> | |||
<label>Category</label> | |||
</div> | |||
<div class="text-center"> | |||
<input type="submit" name="submit" value="Add" | |||
class="btn btn-lg btn-secondary"/> | |||
</div> | |||
</form> | |||
</div> | |||
<br> | |||
</div> | |||
</div> | |||
<div class="row"> | |||
<div class="col-md-6"> | |||
<div class="blogPost"> | |||
<h1 class="text-center">New Post</h1> | |||
<form action="/admin" method ="post" class="p-2"> | |||
<!-- Post category --> | |||
<div class="form-group"> | |||
<input class="form-control" type="text" name="add_post_category" required> | |||
<label class="w3-label w3-validate">Category</label> | |||
</div> | |||
<!-- Post name --> | |||
<div class="form-group"> | |||
<input class="form-control" type="text" name="add_post_name" required> | |||
<label class="w3-label w3-validate">Name</label> | |||
</div> | |||
<!-- Post header picture --> | |||
<div class="form-group"> | |||
<input class="form-control" type="text" name="add_post_picture" value="n/a" required> | |||
<label class="w3-label w3-validate">Picture</label> | |||
</div> | |||
<!-- Post date --> | |||
<div class="form-group"> | |||
<input class="w3-input" type="date" name="add_post_date" required> | |||
<label class="w3-label w3-validate">Date</label> | |||
</div> | |||
<div class="text-center"> | |||
<input type="submit" name="submit" value="Add" | |||
class="btn btn-lg btn-secondary"/> | |||
</div> | |||
</form> | |||
</div> | |||
</div> | |||
<div class="col-md-6"> | |||
<h1 class="text-center">Categories</h1> | |||
<div class="blogPost"> | |||
<table class="table table-striped"> | |||
<thead class="thead-dark"> | |||
<tr> | |||
<td>Name</td> | |||
<td>URL</td> | |||
<td>Edit</td> | |||
</tr> | |||
</thead> | |||
<tbody> | |||
{for cat in categories} | |||
<tr> | |||
<td>{cat.name}</td> | |||
<td>{cat.url}</td> | |||
<td>{cat.category_id}</td> | |||
</tr>> | |||
{/for} | |||
</tbody> | |||
</table> | |||
</div> | |||
</div> | |||
</div> |