Browse Source

Updated downloads to update downloads count in database

pull/4/head
jrtechs 6 years ago
parent
commit
ed5c1911a7
5 changed files with 75 additions and 12 deletions
  1. +1
    -1
      README.md
  2. +1
    -1
      admin/addCategory.js
  3. +10
    -7
      admin/addDownload.js
  4. +1
    -1
      admin/editPost.js
  5. +62
    -2
      utils/sql.js

+ 1
- 1
README.md View File

@ -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)
);

+ 1
- 1
admin/addCategory.js View File

@ -12,7 +12,7 @@ const Promise = require('promise');
*/
var printCategories = function(res)
{
res.write("<div class=\"blogPost p-2 \">");
res.write("<div class=\"blogPost\">");
res.write("<h1 class=\"text-center\">Categories</h1>");
res.write("<div class=\"\"><table class=\"table table-striped\">");
res.write("<thead class=\"thead-dark\">");

+ 10
- 7
admin/addDownload.js View File

@ -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("<div class='blogPost p-2'>");
res.write("<div class='blogPost'>");
res.write("<h1 class=\"text-center\">Downloads</h1>");
res.write("<div class=\"\"><table class=\"table table-striped\">");
res.write("<thead class=\"thead-dark\"><tr>");
@ -137,15 +144,12 @@ var displayDownloads = function(res, postData)
res.write("</tr></thead><tbody>");
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("</tbody></table></div></div><br>");
res.write("</div>");
console.log("got to the end of downloads table");
resolve(postData);
});
}).catch(function(error)

+ 1
- 1
admin/editPost.js View File

@ -104,7 +104,7 @@ var displayRenderForm = function(result, post_id)
{
sql.getPostById(post_id).then(function(post)
{
result.write("<div class='blogPost p-2'>"+
result.write("<div class='blogPost'>"+
"<h1 class=\"text-center\">Edit Post</h1>"+
"<form action=\"/admin/\" method =\"post\" >"+
" <div class=\"form-group\">\n" +

+ 62
- 2
utils/sql.js View File

@ -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

Loading…
Cancel
Save