diff --git a/admin/addCategory.html b/admin/addCategory.html index 0bcb92b..9830d4e 100644 --- a/admin/addCategory.html +++ b/admin/addCategory.html @@ -1,13 +1,11 @@ -
-

Add Category

+

Add Category

-
-
- - -
-

-
-
\ No newline at end of file +
+
+ + +
+

+
\ No newline at end of file diff --git a/admin/addCategory.js b/admin/addCategory.js index 60d8e5b..d266c64 100644 --- a/admin/addCategory.js +++ b/admin/addCategory.js @@ -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("

Categories

"); + res.write("
"); + res.write(""); + res.write(""); + res.write(""); + return new Promise(function(resolve, reject) + { + sql.getCategories().then(function(categories) + { + categories.forEach(function(c) + { + res.write(""); + + res.write(""); + + res.write(""); + + res.write(""); + + res.write(""); + }); + res.write("
NameURLEdit
" + c.name + "" + c.url + "" + c.category_id + "
"); + 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("
"); 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("
"); + resolve(); + }).catch(function(err) + { + console.log(err); + }) }); } }; \ No newline at end of file diff --git a/admin/newPost.html b/admin/newPost.html index 6bb80d8..5f46c62 100644 --- a/admin/newPost.html +++ b/admin/newPost.html @@ -14,7 +14,7 @@
- +