Browse Source

Updated the downloads and home page to work with the template engine.

pull/41/head
jrtechs 6 years ago
parent
commit
dfde817425
12 changed files with 278 additions and 331 deletions
  1. +25
    -3
      admin/admin.js
  2. +131
    -0
      admin/adminDownloads.js
  3. +1
    -62
      admin/category/addCategory.js
  4. +0
    -19
      admin/downloads/addDownload.html
  5. +0
    -191
      admin/downloads/manageDownloads.js
  6. +11
    -0
      admin/login/login.js
  7. +0
    -47
      admin/posts/newPost.js
  8. +3
    -3
      includes/html/adminHeader.html
  9. +1
    -2
      sites/admin.js
  10. +3
    -3
      templates/admin/adminDownloads.html
  11. +103
    -0
      templates/admin/adminHome.html
  12. +0
    -1
      templates/admin/adminMain.html

+ 25
- 3
admin/admin.js View File

@ -16,22 +16,44 @@ module.exports=
* @param request
* @return {*|Promise}
*/
main: function(request, clientAddress, templateContext)
main: function(request, clientAddress, templateContext, filename)
{
console.log("admin main called");
return new Promise(function(resolve, reject)
{
//if logged in
if(request.session && request.session.user)
{
console.log(filename);
templateContext.loggedIn = true;
utils.getPostData(request).then(function (postData)
{
resolve();
console.log("temp 1");
var page = "./adminHome.js";
if(filename.includes('/downloads'))
{
page = "./adminDownloads.js";
console.log("downloads time")
}
else if(filename.includes("/posts"))
{
page = "./posts.js";
}
require(page).main(postData, templateContext).then(function(template)
{
templateContext.adminPage = template;
resolve();
}).catch(function(error)
{
console.log(error);
});
// console.log(postData);
// Promise.all([require("./posts/newPost.js").main(postData),
// require("./category/addCategory.js").main(postData),
// require("./posts/editPost.js").main(postData),
// require("./downloads/manageDownloads.js").main(postData)])
// require("./downloads/adminDownloads.js").main(postData)])
// .then(function(content)
// {
// resolve(content.join(''));

+ 131
- 0
admin/adminDownloads.js View File

@ -0,0 +1,131 @@
/**
* File which deals with adding and removing downloads from
* the admin section of the website.
*
* @author Jeffery Russell 6-30-18
*/
const TEMPLATE_FILE = "admin/adminDownloads.html";
//file IO
const utils = require('../utils/utils.js');
const includes = require('../includes/includes.js');
//updates db
const sql = require('../utils/sql');
//parses post data
const qs = require('querystring');
/**
* Processes post requests from the addDownload form
*
* @param postData
* @returns {*|Promise}
*/
const addDownloadPostData = function(postData)
{
return new Promise(function(resolve, reject)
{
const post = qs.parse(postData);
if(post.add_download)
{
sql.addDownload(post.add_download_name, post.add_download_file)
.then(function()
{
resolve();
}).catch(function(error)
{
reject(error);
})
}
else
{
resolve();
}
});
};
/**
* Handel form requests from the downloads table
*
* @param postData
* @returns {*|Promise}
*/
const removeDownloads = function(postData)
{
return new Promise(function(resolve, reject)
{
const post = qs.parse(postData);
if(post.delete_download)
{
sql.removeDownload(post.delete_download).then(function()
{
resolve();
}).catch(function(err)
{
reject(err);
});
}
else
{
resolve();
}
});
};
/**
* Displays all the download information in a table
* @param postData
* @returns {*|Promise}
*/
const displayDownloads = function(postData, templateContext)
{
return new Promise(function(resolve, reject)
{
sql.getAllDownloads().then(function(downloads)
{
templateContext.downloads = downloads;
resolve();
}).catch(function(error)
{
reject(error);
});
});
};
module.exports=
{
/**
* Renders tha download section of the admin page
*
* @param postData
* @returns {Promise}
*/
main: function(postData, templateContext)
{
console.log(postData);
console.log("downloads page called");
return new Promise(function(resolve, reject)
{
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
addDownloadPostData(postData),
removeDownloads(postData),
displayDownloads(postData, templateContext)]).then(function(template)
{
resolve(template[0]);
}).catch(function(error)
{
console.log("error in add downloads.js");
reject(error);
});
});
}
};

+ 1
- 62
admin/category/addCategory.js View File

@ -8,71 +8,10 @@ const sql = require('../../utils/sql');
const qs = require('querystring');
/**
* Displays all the categories in the database
* @return {*|Promise}
*/
const printCategories = function()
{
var html = "<div class=\"blogPost\">" +
"<h1 class=\"text-center\">Categories</h1>" +
"<div class=\"\"><table class=\"table table-striped\">" +
"<thead class=\"thead-dark\">" +
"<tr>" +
"<td>Name</td><td>URL</td><td>Edit</td>" +
"</tr></thead><tbody>";
return new Promise(function(resolve, reject)
{
sql.getCategories().then(function(categories)
{
categories.forEach(function(c)
{
html +="<tr>" +
"<td>" + c.name + "</td>" +
"<td>" + c.url + "</td>" +
"<td>" + c.category_id + "</td>" +
"</tr>";
});
resolve(html + "</tbody></table></div></div>");
}).catch(function(error)
{
reject(error);
})
});
};
/**
* Checks for post data regarding adding a new category.
* If a post is made with add_category, it parses the url-- replaces spaces
* with dashes -- and calls a insert method on the database
*
* @param postData
* @return {*|Promise}
*/
const processPost = function(postData)
{
return new Promise(function(resolve, reject)
{
const post = qs.parse(postData);
if(post.add_category)
{
const url = post.add_category.split(" ").join("-").toLowerCase();
const q = "insert into categories (name, url) values " +
"('" + post.add_category + "','" + url + "')";
if(sql.insert(q) != 0)
{
console.log("category added");
}
else
{
console.log("error adding category");
}
}
resolve("");
});
};
module.exports=

+ 0
- 19
admin/downloads/addDownload.html View File

@ -1,19 +0,0 @@
<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>

+ 0
- 191
admin/downloads/manageDownloads.js View File

@ -1,191 +0,0 @@
/**
* 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');
/**
* Processes post requests from the addDownload form
*
* @param postData
* @returns {*|Promise}
*/
const addDownloadPostData = function(postData)
{
return new Promise(function(resolve, reject)
{
const post = qs.parse(postData);
if(post.add_download)
{
sql.addDownload(post.add_download_name, post.add_download_file)
.then(function()
{
resolve("");
}).catch(function(error)
{
reject(error);
})
}
else
{
resolve("");
}
});
};
/**
* Displays the addDownload form the the user
*
* @param postData
* @returns {*|Promise}
*/
const addDownload = function(postData)
{
return new Promise(function(resolve, reject)
{
Promise.all([addDownloadPostData(postData),
utils.include("./admin/downloads/addDownload.html")]).then(function(html)
{
resolve("<div class=\"col-md-6\">" + html.join('') + "</div>");
}).catch(function(error)
{
reject(error);
})
});
};
/**
* Handel form requests from the downloads table
*
* @param postData
* @returns {*|Promise}
*/
const displayDownloadsPostData = function(postData)
{
return new Promise(function(resolve, reject)
{
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);
}
});
};
/**
* Renders a single download row in the downloads table
*
* @param download
* @returns {*|Promise}
*/
const renderDownloadRow = function(download)
{
return "<tr>" +
"<td>" + download.name + "</td>" +
"<td>" + download.file + "</td>" +
"<td>" + download.download_count + "</td>" +
"<td><form action=\"/admin\" method =\"post\" >\n" +
" <input type=\"submit\" name=\"submit\" value=\"Delete\"\n" +
" class=\"btn btn-secondary\"/>\n" +
"<input type='hidden' name='delete_download' value='" +
download.download_id + "'/>"+
"</form></td>" +
"</tr>";
};
/**
* Displays all the download information in a table
* @param postData
* @returns {*|Promise}
*/
const displayDownloads = function(postData)
{
var html = "<div class=\"col-md-6\">";
return new Promise(function(resolve, reject)
{
displayDownloadsPostData(postData).then(function()
{
html += "<div class='blogPost'>" +
"<h1 class=\"text-center\">Downloads</h1>" +
"<div class=\"\"><table class=\"table table-striped\">" +
"<thead class=\"thead-dark\"><tr>" +
"<td>Download Name</td><td>File</td>" +
"<td>Download Count</td><td>Delete</td>" +
"</tr></thead><tbody>";
sql.getAllDownloads().then(function(downloads)
{
var downloadPromises = [];
downloads.forEach(function(download)
{
downloadPromises.push(renderDownloadRow(download));
});
Promise.all(downloadPromises).then(function(htmls)
{
const htmlafter = "</tbody></table></div></div><br>" +
"</div>";
resolve(html + htmls.join('') + htmlafter);
});
}).catch(function(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)
{
resolve("<div class=\"row\">" + html.join('') + "</div>");
}).catch(function(error)
{
console.log("error in add downloads.js");
reject(error);
});
});
}
};

+ 11
- 0
admin/login/login.js View File

@ -6,6 +6,8 @@ const sql = require('../../utils/sql');
const qs = require('querystring');
const DEBUG = true;
/**
* Processes post data to see if the user has successfully
@ -19,6 +21,15 @@ const processLogin = function(request, clientAddress, templateContext)
{
return new Promise(function(resolve, reject)
{
if(DEBUG)
{
//what actually logs in the user
request.session.user = 1;
console.log("user has logged in");
templateContext.goodLoginAttempt = true;
resolve();
}
utils.getPostData(request).then(function(postData)
{
const post = qs.parse(postData);

+ 0
- 47
admin/posts/newPost.js View File

@ -2,56 +2,9 @@ const utils = require('../../utils/utils.js');
const sql = require('../../utils/sql');
const qs = require('querystring');
const Promise = require('promise');
/**
*
* @param postData
* @return {*|Promise}
*/
const processPost = function(postData)
{
return new Promise(function(resolve, reject)
{
var post = qs.parse(postData);
if(post.add_post_name)
{
var urls = post.add_post_name;
urls = urls.split(" ").join("-");
urls =urls.toLowerCase();
var q = "insert into posts (category_id, picture_url, published, name, url) values ";
q += "('" + post.add_post_category + "', '" + post.add_post_picture +
"', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";
sql.insert(q).then(function()
{
var map = require('../../utils/generateSiteMap');
map.main();
resolve("");
}).catch(function(error)
{
reject(error);
})
}
else if(post.clear_cache)
{
require("../../sites/blog.js").clearCache();
require("../../includes/includes.js").clearCache();
}
else if(post.git_pull)
{
const execSync = require('child_process').execSync;
code = execSync('git pull')
}
else
{
resolve("");
}
});
};
module.exports=

+ 3
- 3
includes/html/adminHeader.html View File

@ -61,13 +61,13 @@
<a class="nav-link" href="https://jrtechs.net">Live Blog<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://jrtechs.net/admin/">Admin Home</a>
<a class="nav-link" href="/admin">Admin Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://jrtechs.net/admin/posts/">Posts</a>
<a class="nav-link" href="/admin/posts">Posts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://jrtechs.net/admin/downloads/">Downloads</a>
<a class="nav-link" href="/admin/downloads">Downloads</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">

+ 1
- 2
sites/admin.js View File

@ -41,11 +41,10 @@ module.exports=
var templateContext = Object();
Promise.all([includes.printAdminHeader(templateContext),
require(file).main(request, clientAddress, templateContext),
require(file).main(request, clientAddress, templateContext, filename),
includes.printFooter(templateContext),
includes.fetchTemplate("admin/adminMain.html")]).then(function(content)
{
result.write(whiskers.render(content.join(''), templateContext));
result.end();

+ 3
- 3
templates/admin/adminDownloads.html View File

@ -5,7 +5,7 @@
<div class="blogPost">
<h1 class="text-center">Add Download</h1>
<form action="/admin" method ="post" class="p-2">
<form action="/admin/downloads/" method ="post" class="p-2">
<div class="form-group">
<input class="form-control" type="text" name="add_download_name" required>
<label>Download Name</label>
@ -51,9 +51,9 @@
{download.download_count}
</td>
<td>
<form action="/admin/downloads" method ="post" >
<form action="/admin/downloads/" method ="post" >
<input type="submit" name="submit" value="Delete" class="btn btn-secondary"/>
<input type='hidden' name='delete_download' value='{download_id}' />
<input type='hidden' name='delete_download' value='{download.download_id}' />
</form>
</td>
</tr>

+ 103
- 0
templates/admin/adminHome.html View File

@ -0,0 +1,103 @@
<div class="row">
<div class="col-md-6">
<div class="blogPost">
<h1 class="text-center">Server Controls</h1>
<div class="text-center">
<form action="/admin" method="post">
<input type="submit" name="clearCache" value="Clear Cache" class="btn btn-lg btn-secondary" />
<input type="hidden" name="clear_cache" value="true">
</form>
<br>
<form action="/admin" method="post">
<input type="submit" name="gitPull" value="Pull from Git" class="btn btn-lg btn-secondary" />
<input type="hidden" name="git_pull" value="true">
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="blogPost">
<h1 class="text-center">Add Category</h1>
<form action="/admin" method ="post" class="p-2">
<div class="form-group">
<input class="form-control" type="text" name="add_category" required>
<label>Category</label>
</div>
<div class="text-center">
<input type="submit" name="submit" value="Add"
class="btn btn-lg btn-secondary"/>
</div>
</form>
</div>
<br>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="blogPost">
<h1 class="text-center">New Post</h1>
<form action="/admin" method ="post" class="p-2">
<!-- Post category -->
<div class="form-group">
<input class="form-control" type="text" name="add_post_category" required>
<label class="w3-label w3-validate">Category</label>
</div>
<!-- Post name -->
<div class="form-group">
<input class="form-control" type="text" name="add_post_name" required>
<label class="w3-label w3-validate">Name</label>
</div>
<!-- Post header picture -->
<div class="form-group">
<input class="form-control" type="text" name="add_post_picture" value="n/a" required>
<label class="w3-label w3-validate">Picture</label>
</div>
<!-- Post date -->
<div class="form-group">
<input class="w3-input" type="date" name="add_post_date" required>
<label class="w3-label w3-validate">Date</label>
</div>
<div class="text-center">
<input type="submit" name="submit" value="Add"
class="btn btn-lg btn-secondary"/>
</div>
</form>
</div>
</div>
<div class="col-md-6">
<h1 class="text-center">Categories</h1>
<div class="blogPost">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<td>Name</td>
<td>URL</td>
<td>Edit</td>
</tr>
</thead>
<tbody>
{for cat in categories}
<tr>
<td>{cat.name}</td>
<td>{cat.url}</td>
<td>{cat.category_id}</td>
</tr>>
{/for}
</tbody>
</table>
</div>
</div>
</div>

+ 0
- 1
templates/admin/adminMain.html View File

@ -4,7 +4,6 @@
{if loggedIn}
{>adminPage}
in the system
{else}
{if goodLoginAttempt}

Loading…
Cancel
Save