diff --git a/README.md b/README.md
index 19e505e..2980998 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,9 @@ Purple:
- Primary: #513E7D
- Secondary: #D2C0FF
+
+006688
+
## MYSQL Information
![](blogSql.svg)
@@ -58,8 +61,9 @@ primary key(post_id)
create table downloads(
download_id mediumint unsigned not null AUTO_INCREMENT,
-url varchar(20) not null,
-file varchar(20) not null,
+file varchar(40) not null,
+name varchar(20) not null,
+download_count mediumint unsigned null,
primary key(download_id)
);
diff --git a/admin/addDownload.html b/admin/addDownload.html
new file mode 100644
index 0000000..44c80b2
--- /dev/null
+++ b/admin/addDownload.html
@@ -0,0 +1,19 @@
+
+
\ No newline at end of file
diff --git a/admin/addDownload.js b/admin/addDownload.js
new file mode 100644
index 0000000..683f205
--- /dev/null
+++ b/admin/addDownload.js
@@ -0,0 +1,199 @@
+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("addind post to db");
+ console.log(post);
+ }
+ 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(" | ");
+
+ 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("Download Name | File | Download Count | Delete | ");
+ res.write("
");
+
+
+
+ sql.getAllDownloads().then(function(downloads)
+ {
+ console.log("sql thing finished");
+ var downloadPromises = [];
+
+ downloads.forEach(function(download)
+ {
+ console.log("push elements");
+ 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("
");
+ res.write("
");
+ console.log("got to the end of downloads table");
+ 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);
+ })
+ });
+ }
+};
\ No newline at end of file
diff --git a/admin/admin.js b/admin/admin.js
index dffeb92..0b8207b 100644
--- a/admin/admin.js
+++ b/admin/admin.js
@@ -1,6 +1,5 @@
const utils = require('../utils/utils.js');
var Promise = require('promise');
-var session = require('client-sessions');
module.exports=
{
@@ -28,12 +27,15 @@ module.exports=
{
result.write(""); //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)
{
- throw error;
+ reject(error);
});
}
else
diff --git a/admin/editPost.js b/admin/editPost.js
index bb6f208..6ead383 100644
--- a/admin/editPost.js
+++ b/admin/editPost.js
@@ -202,7 +202,7 @@ module.exports=
return postsTable(result);
}).then(function()
{
- resolve();
+ resolve(postData);
}).catch(function(error)
{
console.log("Error in edit post module");
diff --git a/downloads/downloads.js b/downloads/downloads.js
index b6b71d5..2691475 100644
--- a/downloads/downloads.js
+++ b/downloads/downloads.js
@@ -22,13 +22,13 @@ module.exports=
*/
main: function(res, requestURL, request)
{
- res.setHeader('Content-disposition', 'attachment; filename=dramaticpenguin.MOV');
return new Promise(function(resolve, reject)
{
var urlSplit = requestURL.split("/");
console.log(urlSplit);
if(urlSplit.length == 3 || urlSplit.length == 4)
{
+ console.log(urlSplit[2]);
sql.getDownload(urlSplit[2]).then(function(result)
{
console.log(result);
diff --git a/server.js b/server.js
index 22dba00..23e35a7 100644
--- a/server.js
+++ b/server.js
@@ -46,6 +46,10 @@ app.use(function(request, res)
{
includes.sendCSS(res, filename)
}
+ else if(filename.includes("/downloads/"))
+ {
+ require("./downloads/downloads.js").main(res, filename, request);
+ }
else
{
var file = "";
diff --git a/utils/sql.js b/utils/sql.js
index b0ca613..aee76bd 100644
--- a/utils/sql.js
+++ b/utils/sql.js
@@ -320,7 +320,7 @@ module.exports=
},
- /**TODO work on website downloads
+ /**Returns download information associated with a download name
*
* @param downloadURL
* @returns {Array}
@@ -328,10 +328,21 @@ module.exports=
getDownload: function(downloadURL)
{
var cleanD = sanitizer.sanitize(downloadURL);
- var q = "select * from downloads where url='" + cleanD + "' limit 1";
+ var q = "select * from downloads where file='" + cleanD + "' limit 1";
return fetch(q);
},
+
+ /**
+ * Fetches all the downloads from the database
+ *
+ * @returns {Array}
+ */
+ getAllDownloads: function()
+ {
+ return fetch("select * from downloads");
+ },
+
/**
* Based on the post data submitted by the user this function updates
* the information on the post in the database