const utils = require('../utils/utils.js'); const sql = require('../utils/sql'); const qs = require('querystring'); const Promise = require('promise'); /** * @author Jeffery Russell 6-30-18 */ /** * Processes post requests from the addDownload form * @param res * @param postData * @returns {*|Promise} */ var addDownloadPostData = function(res, postData) { return new Promise(function(resolve, reject) { var post = qs.parse(postData); if(post.add_download) { console.log(post); sql.addDownload(post.add_download_name, post.add_download_file).then(function() { resolve(postData); }); } else { resolve(postData); } }); }; /** * Displays the addDownload form the the user * * @param res * @param postData * @returns {*|Promise} */ var addDownload = function(res, postData) { res.write("
"); return new Promise(function(resolve, reject) { addDownloadPostData(res, postData).then(function() { return utils.include(res, "./admin/addDownload.html"); }).then(function() { res.write("
"); resolve(postData); }).catch(function(err) { console.log(err); reject(err); }); }); }; /** * Handel form requests from the downloads table * * @param res * @param postData * @returns {*|Promise} */ var displayDownloadsPostData = function(res, postData) { return new Promise(function(resolve, reject) { var post = qs.parse(postData); if(post.delete_download) { } resolve(postData); }); }; /** * Renders a single download row in the downloads table * * @param result * @param download * @returns {*|Promise} */ var renderDownloadRow = function(result, download) { return new Promise(function(resolve, reject) { result.write(""); //download name result.write("" + download.name + ""); //file name result.write("" + download.file + ""); //download count result.write("" + download.download_count + ""); //edit result.write("
\n" + " \n" + ""+ "
"); result.write(""); resolve(); }); }; /** * Displays all the download information in a table * @param res * @param postData * @returns {*|Promise} */ var displayDownloads = function(res, postData) { res.write("
"); return new Promise(function(resolve, reject) { displayDownloadsPostData(res, postData).then(function() { res.write("
"); res.write("

Downloads

"); res.write("
"); res.write(""); res.write(""); res.write(""); sql.getAllDownloads().then(function(downloads) { var downloadPromises = []; downloads.forEach(function(download) { downloadPromises.push(new Promise(function(resolveDownload, reject) { renderDownloadRow(res, download).then(function() { resolveDownload(); }).catch(function(error) { reject(error); }) })); }); Promise.all(downloadPromises).then(function() { res.write("
Download NameFileDownload CountDelete

"); res.write("
"); resolve(postData); }); }).catch(function(error) { reject(error); }); }); }); }; module.exports= { main: function(res, postData) { res.write("
"); return new Promise(function(resolve, reject) { addDownload(res, postData).then(function() { return displayDownloads(res, postData); }).then(function() { res.write("
"); resolve(postData); }).catch(function(err) { console.log(err); reject(err); }) }); } };