Browse Source

Finished MVP

pull/4/head
jrtechs 6 years ago
parent
commit
a9af8a98fb
3 changed files with 92 additions and 42 deletions
  1. +9
    -11
      admin/addCategory.html
  2. +82
    -30
      admin/addCategory.js
  3. +1
    -1
      admin/newPost.html

+ 9
- 11
admin/addCategory.html View File

@ -1,13 +1,11 @@
<div class="w3-third w3-container">
<h1 class="w3-text-teal w3-center">Add Category</h1>
<h1 class="w3-text-teal w3-center">Add Category</h1>
<form action="/admin/" method ="post" class="w3-container w3-card-4">
<div class="w3-group w3-padding-16">
<input class="w3-input" type="text" name="add_category" required>
<label class="w3-label w3-validate">Category</label>
</div>
<p><input type="submit" name="submit" value="Add"
class="w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align"/></p>
</form>
</div>
<form action="/admin/" method ="post" class="w3-container w3-card-4">
<div class="w3-group w3-padding-16">
<input class="w3-input" type="text" name="add_category" required>
<label class="w3-label w3-validate">Category</label>
</div>
<p><input type="submit" name="submit" value="Add"
class="w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align"/></p>
</form>

+ 82
- 30
admin/addCategory.js View File

@ -5,44 +5,96 @@ const qs = require('querystring');
var Promise = require('promise');
/**
* Displays all the categories in the database
* @param res
* @return {*|Promise}
*/
var printCategories = function(res)
{
res.write("<h1 class=\"w3-text-teal w3-center\">Categories</h1>");
res.write("<div class=\"w3-responsive w3-card-4\"><table class=\"w3-table w3-striped w3-bordered\"><thead>");
res.write("<tr class=\"w3-teal\">");
res.write("<td>Name</td><td>URL</td><td>Edit</td>");
res.write("</tr></thead><tbody>");
return new Promise(function(resolve, reject)
{
sql.getCategories().then(function(categories)
{
categories.forEach(function(c)
{
res.write("<tr>");
res.write("<td>" + c.name + "</td>");
res.write("<td>" + c.url + "</td>");
res.write("<td>" + c.category_id + "</td>");
res.write("</tr>");
});
res.write("</tbody></table></div>");
console.log("resolved");
resolve();
})
});
};
/**
* 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 res
* @param postData
* @return {*|Promise}
*/
var processPost = function(res, postData)
{
return new Promise(function(resolve, reject)
{
var post = qs.parse(postData);
if(post.add_category)
{
var url = post.add_category.replace(/ /i, "-");
url = url.toLowerCase();
var 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(postData);
});
};
module.exports=
{
main: function(res, postData)
{
utils.include(res, "./admin/addCategory.html");
return this.processPost(res, postData);
},
/**
* 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 res
* @param postData
* @return {*|Promise}
*/
processPost: function(res, postData)
{
res.write("<div class=\"w3-third w3-container\">");
return new Promise(function(resolve, reject)
{
var post = qs.parse(postData);
if(post.add_category)
utils.include(res, "./admin/addCategory.html");
printCategories(res).then(function()
{
//console.write("categories finished");
return processPost(res, postData);
}).then(function()
{
var url = post.add_category.replace(/ /i, "-");
var q = "insert into categories (name, url) values " +
"('" + post.add_category + "','" + url + "')";
console.log(q);
if(sql.insert(q) != 0)
{
console.log("category added");
}
else
{
console.log("error adding category");
}
}
resolve(postData);
res.write("</div>");
resolve();
}).catch(function(err)
{
console.log(err);
})
});
}
};

+ 1
- 1
admin/newPost.html View File

@ -14,7 +14,7 @@
</div>
<!-- Post header picture -->
<div class="w3-group w3-padding-16">
<input class="w3-input" type="text" name="add_post_picture" required>
<input class="w3-input" type="text" name="add_post_picture" value="n/a" required>
<label class="w3-label w3-validate">Picture</label>
</div>

Loading…
Cancel
Save