diff --git a/admin/admin.js b/admin/admin.js
index d9882a1..9599f4f 100644
--- a/admin/admin.js
+++ b/admin/admin.js
@@ -21,7 +21,7 @@ module.exports=
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)])
+ require("./manageDownloads.js").main(postData)])
.then(function(content)
{
resolve(content.join(''));
@@ -43,7 +43,6 @@ module.exports=
console.log(err);
})
}
-
});
}
};
\ No newline at end of file
diff --git a/admin/addDownload.js b/admin/manageDownloads.js
similarity index 74%
rename from admin/addDownload.js
rename to admin/manageDownloads.js
index f9e2d81..96f8840 100644
--- a/admin/addDownload.js
+++ b/admin/manageDownloads.js
@@ -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("
");
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("
" + html.join('') + "
");
}).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)
"
" + download.file + " | " +
"
" + download.download_count + " | " +
"
| " +
"";
};
@@ -121,7 +135,8 @@ const displayDownloads = function(postData)
"
Downloads
" +
"
" +
"" +
- "Download Name | File | Download Count | Delete | " +
+ "Download Name | File | " +
+ "Download Count | Delete | " +
"
";
@@ -136,30 +151,34 @@ const displayDownloads = function(postData)
Promise.all(downloadPromises).then(function(htmls)
{
- var htmlafter = "
" +
+ const htmlafter = "
" +
"";
- 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("" + html.join('') + "
");
}).catch(function(error)
diff --git a/admin/newPost.js b/admin/newPost.js
index e627e13..0a2462f 100644
--- a/admin/newPost.js
+++ b/admin/newPost.js
@@ -60,7 +60,6 @@ module.exports=
resolve(html.join(''));
}).catch(function(error)
{
- console.log(error);
reject(error);
})
});
diff --git a/server.js b/server.js
index 1863754..3046fd0 100644
--- a/server.js
+++ b/server.js
@@ -128,9 +128,7 @@ app.use(function(request, result)
sql.logTraffic(getClientAddress, filename);
}
catch (e)
- {
-
- }
+ { }
}
}
else
diff --git a/utils/sql.js b/utils/sql.js
index a558c57..207fc43 100644
--- a/utils/sql.js
+++ b/utils/sql.js
@@ -94,6 +94,7 @@ module.exports=
});
},
+
/**
* Not to be mistaken for getPostData() in @file utils/utils.js,
* this function extracts a post entry from the sql server
@@ -137,6 +138,8 @@ module.exports=
});
},
+
+
/**
* Function used to retrieve all categories when making the sidebar
*
@@ -148,6 +151,7 @@ module.exports=
return fetch(q);
},
+
/**
* Function which currently returns all posts of a particular
* category from the database
@@ -185,6 +189,7 @@ module.exports=
return fetch("select * from posts order by post_id desc limit 10");
},
+
/**
* Helper method which returns a list of objects which contains the url
* and name of thee ten most recent posts
@@ -242,6 +247,8 @@ module.exports=
});
});
},
+
+
/**
* Function which checks to see if a user successfully logged in based on
* the post data which they sent
@@ -299,6 +306,7 @@ module.exports=
});
},
+
/**
* Fetches a promise containing every post in the database
* @returns {Array}
@@ -398,12 +406,25 @@ module.exports=
*/
addDownload: function(name, file)
{
- var q = "insert into downloads (name, file, download_count) " +
+ const q = "insert into downloads (name, file, download_count) " +
"values('" + name + "', '" + file + "', '0')";
return module.exports.insert(q);
},
+
+ /**
+ *
+ * @param id
+ */
+ removeDownload: function(id)
+ {
+ const q = "delete from downloads where download_id='" + id + "'";
+
+ return module.exports.insert(q);
+ },
+
+
/**
* Based on the post data submitted by the user this function updates
* the information on the post in the database
@@ -423,6 +444,7 @@ module.exports=
return module.exports.insert(q);
},
+
/**
* Function which returns a promise which contains the string of the
* entire sitemap for the blog.