//file io
const utils = require('../../utils/utils.js');
//update db
const sql = require('../../utils/sql');
//parse post data
const qs = require('querystring');
/**
* Displays all the categories in the database
* @return {*|Promise}
*/
const printCategories = function()
{
var html = "
" +
"
Categories
" +
"
" +
"" +
"" +
"Name | URL | Edit | " +
"
";
return new Promise(function(resolve, reject)
{
sql.getCategories().then(function(categories)
{
categories.forEach(function(c)
{
html +="" +
"" + c.name + " | " +
"" + c.url + " | " +
"" + c.category_id + " | " +
"
";
});
resolve(html + "
");
}).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=
{
main: function(postData)
{
return new Promise(function(resolve, reject)
{
Promise.all([utils.include("./admin/addCategory.html"),
printCategories(),
processPost(postData)]).then(function(html)
{
resolve("" +
html.join('') +
"
");
}).catch(function(error)
{
console.log("error in addCategory.js");
reject(error);
})
});
}
};