diff --git a/README.md b/README.md index 2980998..1174b37 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ create table downloads( download_id mediumint unsigned not null AUTO_INCREMENT, file varchar(40) not null, name varchar(20) not null, -download_count mediumint unsigned null, +download_count mediumint not null, primary key(download_id) ); diff --git a/admin/addCategory.js b/admin/addCategory.js index 267158f..fea0bec 100644 --- a/admin/addCategory.js +++ b/admin/addCategory.js @@ -12,7 +12,7 @@ const Promise = require('promise'); */ var printCategories = function(res) { - res.write("
"); + res.write("
"); res.write("

Categories

"); res.write("
"); res.write(""); diff --git a/admin/addDownload.js b/admin/addDownload.js index 683f205..a5bbdab 100644 --- a/admin/addDownload.js +++ b/admin/addDownload.js @@ -22,10 +22,17 @@ var addDownloadPostData = function(res, postData) var post = qs.parse(postData); if(post.add_download) { - console.log("addind post to db"); console.log(post); + + sql.addDownload(post.add_download_name, post.add_download_file).then(function() + { + resolve(postData); + }); + } + else + { + resolve(postData); } - resolve(postData); }); }; @@ -129,7 +136,7 @@ var displayDownloads = function(res, postData) { displayDownloadsPostData(res, postData).then(function() { - res.write("
"); + res.write("
"); res.write("

Downloads

"); res.write("
"); res.write(""); @@ -137,15 +144,12 @@ var displayDownloads = function(res, postData) 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() @@ -162,7 +166,6 @@ var displayDownloads = function(res, postData) { res.write("

"); res.write("
"); - console.log("got to the end of downloads table"); resolve(postData); }); }).catch(function(error) diff --git a/admin/editPost.js b/admin/editPost.js index 6ead383..7d48c5f 100644 --- a/admin/editPost.js +++ b/admin/editPost.js @@ -104,7 +104,7 @@ var displayRenderForm = function(result, post_id) { sql.getPostById(post_id).then(function(post) { - result.write("
"+ + result.write("
"+ "

Edit Post

"+ "
"+ "
\n" + diff --git a/utils/sql.js b/utils/sql.js index aee76bd..f78da4e 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -328,8 +328,52 @@ module.exports= getDownload: function(downloadURL) { var cleanD = sanitizer.sanitize(downloadURL); - var q = "select * from downloads where file='" + cleanD + "' limit 1"; - return fetch(q); + var q = "select * from downloads where name='" + cleanD + "' limit 1"; + + return new Promise(function(resolve, reject) + { + fetch(q).then(function(sqlData) + { + return module.exports.incrementDownloadCount(sqlData); + }).then(function(sqlData) + { + resolve(sqlData) + }).catch(function(error) + { + reject(error); + }) + }); + }, + + + /** Increments the download count in the database + * + * @param sqlRow + * @returns {*|Promise} + */ + incrementDownloadCount: function(sqlRow) + { + return new Promise(function(resolve, reject) + { + if(sqlRow.length == 1) + { + var q = "update downloads set download_count='" + + (sqlRow[0].download_count + 1) + "' where download_id='" + + sqlRow[0].download_id + "'"; + console.log(q); + module.exports.insert(q).then(function(r) + { + resolve(sqlRow); + }).catch(function(err) + { + reject(err); + }) + } + else + { + resolve(sqlRow); + } + }); }, @@ -343,6 +387,22 @@ module.exports= return fetch("select * from downloads"); }, + + /** + * Inserts a download row into the database + * + * @param name of the download + * @param file name of file + * @returns {*|the} + */ + addDownload: function(name, file) + { + var q = "insert into downloads (name, file, download_count) " + + "values('" + name + "', '" + file + "', '0')"; + + 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