Browse Source

Added downloads widgets to the admin page.

pull/4/head
jrtechs 6 years ago
parent
commit
0aea706da4
8 changed files with 247 additions and 8 deletions
  1. +6
    -2
      README.md
  2. +19
    -0
      admin/addDownload.html
  3. +199
    -0
      admin/addDownload.js
  4. +4
    -2
      admin/admin.js
  5. +1
    -1
      admin/editPost.js
  6. +1
    -1
      downloads/downloads.js
  7. +4
    -0
      server.js
  8. +13
    -2
      utils/sql.js

+ 6
- 2
README.md View File

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

+ 19
- 0
admin/addDownload.html View File

@ -0,0 +1,19 @@
<div class="blogPost">
<h1 class="text-center">Add Download</h1>
<form action="/admin/" method ="post" class="p-2">
<div class="form-group">
<input class="form-control" type="text" name="add_download_name" required>
<label>Download Name</label>
</div>
<div class="form-group">
<input class="form-control" type="text" name="add_download_file" required>
<label>File name</label>
</div>
<div class="text-center">
<input type="submit" name="add_download" value="Add Download"
class="btn btn-lg btn-secondary"/>
</div>
</form>
</div>
<br>

+ 199
- 0
admin/addDownload.js View File

@ -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("<div class=\"col-md-6\">");
return new Promise(function(resolve, reject)
{
addDownloadPostData(res, postData).then(function()
{
return utils.include(res, "./admin/addDownload.html");
}).then(function()
{
res.write("</div>");
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("<tr>");
//download name
result.write("<td>" + download.name + "</td>");
//file name
result.write("<td>" + download.file + "</td>");
//download count
result.write("<td>" + download.download_count + "</td>");
//edit
result.write("<td><form action=\"/admin/\" method =\"post\" >\n" +
" <input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
" class=\"btn btn-secondary\"/>\n" +
"<input type='hidden' name='delete_download' value='" + download.download_id + "'/>"+
"</form></td>");
result.write("</tr>");
resolve();
});
};
/**
* Displays all the download information in a table
* @param res
* @param postData
* @returns {*|Promise}
*/
var displayDownloads = function(res, postData)
{
res.write("<div class=\"col-md-6\">");
return new Promise(function(resolve, reject)
{
displayDownloadsPostData(res, postData).then(function()
{
res.write("<div class='blogPost p-2'>");
res.write("<h1 class=\"text-center\">Downloads</h1>");
res.write("<div class=\"\"><table class=\"table table-striped\">");
res.write("<thead class=\"thead-dark\"><tr>");
res.write("<td>Download Name</td><td>File</td><td>Download Count</td><td>Delete</td>");
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()
{
resolveDownload();
}).catch(function(error)
{
reject(error);
})
}));
});
Promise.all(downloadPromises).then(function()
{
res.write("</tbody></table></div></div><br>");
res.write("</div>");
console.log("got to the end of downloads table");
resolve(postData);
});
}).catch(function(error)
{
reject(error);
});
});
});
};
module.exports=
{
main: function(res, postData)
{
res.write("<div class=\"row\">");
return new Promise(function(resolve, reject)
{
addDownload(res, postData).then(function()
{
return displayDownloads(res, postData);
}).then(function()
{
res.write("</div>");
resolve(postData);
}).catch(function(err)
{
console.log(err);
reject(err);
})
});
}
};

+ 4
- 2
admin/admin.js View File

@ -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("</div>"); //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

+ 1
- 1
admin/editPost.js View File

@ -202,7 +202,7 @@ module.exports=
return postsTable(result);
}).then(function()
{
resolve();
resolve(postData);
}).catch(function(error)
{
console.log("Error in edit post module");

+ 1
- 1
downloads/downloads.js View File

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

+ 4
- 0
server.js View File

@ -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 = "";

+ 13
- 2
utils/sql.js View File

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

Loading…
Cancel
Save