|
|
@ -1,29 +1,36 @@ |
|
|
|
/** |
|
|
|
* 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'); |
|
|
|
const Promise = require('promise'); |
|
|
|
|
|
|
|
/** |
|
|
|
* @author Jeffery Russell 6-30-18 |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Processes post requests from the addDownload form |
|
|
|
* |
|
|
|
* @param postData |
|
|
|
* @returns {*|Promise} |
|
|
|
*/ |
|
|
|
var addDownloadPostData = function(postData) |
|
|
|
const addDownloadPostData = function(postData) |
|
|
|
{ |
|
|
|
return new Promise(function(resolve, reject) |
|
|
|
{ |
|
|
|
var post = qs.parse(postData); |
|
|
|
const post = qs.parse(postData); |
|
|
|
if(post.add_download) |
|
|
|
{ |
|
|
|
console.log(post); |
|
|
|
|
|
|
|
sql.addDownload(post.add_download_name, post.add_download_file).then(function() |
|
|
|
sql.addDownload(post.add_download_name, post.add_download_file) |
|
|
|
.then(function() |
|
|
|
{ |
|
|
|
resolve(""); |
|
|
|
}).catch(function(error) |
|
|
@ -47,16 +54,14 @@ var addDownloadPostData = function(postData) |
|
|
|
*/ |
|
|
|
const addDownload = function(postData) |
|
|
|
{ |
|
|
|
//res.write("<div class=\"col-md-6\">");
|
|
|
|
return new Promise(function(resolve, reject) |
|
|
|
{ |
|
|
|
Promise.all([addDownloadPostData(postData), utils.include("./admin/addDownload.html")]).then(function(html) |
|
|
|
Promise.all([addDownloadPostData(postData), |
|
|
|
utils.include("./admin/addDownload.html")]).then(function(html) |
|
|
|
{ |
|
|
|
console.log("add download is good"); |
|
|
|
resolve("<div class=\"col-md-6\">" + html.join('') + "</div>"); |
|
|
|
}).catch(function(error) |
|
|
|
{ |
|
|
|
console.log(error); |
|
|
|
reject(error); |
|
|
|
}) |
|
|
|
}); |
|
|
@ -73,13 +78,21 @@ const displayDownloadsPostData = function(postData) |
|
|
|
{ |
|
|
|
return new Promise(function(resolve, reject) |
|
|
|
{ |
|
|
|
|
|
|
|
var post = qs.parse(postData); |
|
|
|
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); |
|
|
|
} |
|
|
|
resolve(postData); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
@ -97,9 +110,10 @@ const renderDownloadRow = function(download) |
|
|
|
"<td>" + download.file + "</td>" + |
|
|
|
"<td>" + download.download_count + "</td>" + |
|
|
|
"<td><form action=\"/admin/\" method =\"post\" >\n" + |
|
|
|
" <input type=\"submit\" name=\"submit\" value=\"Edit\"\n" + |
|
|
|
" <input type=\"submit\" name=\"submit\" value=\"Delete\"\n" + |
|
|
|
" class=\"btn btn-secondary\"/>\n" + |
|
|
|
"<input type='hidden' name='delete_download' value='" + download.download_id + "'/>"+ |
|
|
|
"<input type='hidden' name='delete_download' value='" + |
|
|
|
download.download_id + "'/>"+ |
|
|
|
"</form></td>" + |
|
|
|
"</tr>"; |
|
|
|
}; |
|
|
@ -121,7 +135,8 @@ const displayDownloads = function(postData) |
|
|
|
"<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>" + |
|
|
|
"<td>Download Name</td><td>File</td>" + |
|
|
|
"<td>Download Count</td><td>Delete</td>" + |
|
|
|
"</tr></thead><tbody>"; |
|
|
|
|
|
|
|
|
|
|
@ -136,30 +151,34 @@ const displayDownloads = function(postData) |
|
|
|
|
|
|
|
Promise.all(downloadPromises).then(function(htmls) |
|
|
|
{ |
|
|
|
var htmlafter = "</tbody></table></div></div><br>" + |
|
|
|
const 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); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
Promise.all([addDownload(postData), |
|
|
|
displayDownloads(postData)]).then(function(html) |
|
|
|
{ |
|
|
|
resolve("<div class=\"row\">" + html.join('') + "</div>"); |
|
|
|
}).catch(function(error) |