diff --git a/admin/addCategory.js b/admin/addCategory.js
index d266c64..e4d2283 100644
--- a/admin/addCategory.js
+++ b/admin/addCategory.js
@@ -2,7 +2,7 @@ const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
const qs = require('querystring');
-var Promise = require('promise');
+const Promise = require('promise');
/**
@@ -34,7 +34,6 @@ var printCategories = function(res)
res.write("");
});
res.write("");
- console.log("resolved");
resolve();
})
});
@@ -56,8 +55,7 @@ var processPost = function(res, postData)
var post = qs.parse(postData);
if(post.add_category)
{
- var url = post.add_category.replace(/ /i, "-");
- url = url.toLowerCase();
+ var url = post.add_category.split(" ").join("-").toLowerCase();
var q = "insert into categories (name, url) values " +
"('" + post.add_category + "','" + url + "')";
if(sql.insert(q) != 0)
@@ -68,7 +66,6 @@ var processPost = function(res, postData)
{
console.log("error adding category");
}
-
}
resolve(postData);
});
@@ -88,9 +85,8 @@ module.exports=
return processPost(res, postData);
}).then(function()
{
-
res.write("");
- resolve();
+ resolve(postData);
}).catch(function(err)
{
console.log(err);
diff --git a/admin/admin.js b/admin/admin.js
index 56bf32e..35f3176 100644
--- a/admin/admin.js
+++ b/admin/admin.js
@@ -26,10 +26,17 @@ module.exports=
}).then(function(postData)
{
return require("../admin/addCategory.js").main(result, postData);
- }).then(function()
+ }).then(function(postData)
{
result.write("");
+ console.log("hmmm");
+ return require("../admin/editPost.js").main(result, postData);
+ }).then(function()
+ {
resolve();
+ }).catch(function(error)
+ {
+ throw error;
});
}
else
diff --git a/admin/editPost.js b/admin/editPost.js
new file mode 100644
index 0000000..7933e91
--- /dev/null
+++ b/admin/editPost.js
@@ -0,0 +1,212 @@
+/** File which renders the edit form for the posts and processes
+ * the post data generated by edit forms.
+ *
+ * @type {Promise|*}
+ */
+
+const Promise = require('promise');
+const qs = require('querystring');
+const sql = require('../utils/sql');
+
+/**
+ * Displays a single row in the posts view
+ * @param result
+ * @param post
+ */
+var renderPostRow = function(result, post)
+{
+ return new Promise(function(resolve, reject)
+ {
+ result.write("
");
+
+ //category
+ result.write("" + post.category_id + " | ");
+
+ //name
+ result.write("" + post.name + " | ");
+
+ //picture
+ result.write("" + post.picture_url + " | ");
+
+ //date
+ result.write("" + post.published + " | ");
+
+ //edit
+ result.write(" | ");
+
+ result.write("
");
+
+ resolve();
+ });
+};
+
+/**
+ * Displays all the posts in a table
+ * @param result
+ */
+var postsTable = function(result)
+{
+ result.write("");
+ result.write("
Posts
");
+ result.write("
");
+ result.write("");
+ result.write("Category # | Name | Header Picture | Date | Edit | ");
+ result.write("
");
+ return new Promise(function(resolve, reject)
+ {
+ sql.getAllPosts().then(function(posts)
+ {
+ var postPromises = [];
+ posts.forEach(function(post)
+ {
+ postPromises.push(new Promise(function(res, rej)
+ {
+ renderPostRow(result, post).then(function()
+ {
+ res();
+ }).catch(function(error)
+ {
+ console.log("error rendering " + post);
+ rej(error);
+ })
+ }));
+ });
+ Promise.all(postPromises).then(function()
+ {
+ result.write("
");
+ resolve();
+ }).catch(function(error)
+ {
+ console.log(error);
+ console.log("error rendering posts");
+ reject(error);
+ });
+ }).catch(function(error)
+ {
+ console.log("error with sql query");
+ reject(error);
+ })
+ });
+};
+
+/**
+ * Displays the edit form for edit posts
+ * @param result
+ * @param post_id
+ */
+var displayRenderForm = function(result, post_id)
+{
+ return new Promise(function(resolve, reject)
+ {
+ sql.getPostById(post_id).then(function(post)
+ {
+ result.write(""+
+ "
Edit Post
"+
+ "
"+
+ "
"
+ );
+ resolve();
+ }).catch(function(error)
+ {
+ console.log(error);
+ console.log("error getting post from sql in display Reender Form");
+ reject(error);
+ });
+
+ });
+};
+
+/**
+ * Detects if the post data came from the edit form in posts table or edit post
+ * in the edit post form. Based on this, this function will call one of two functions
+ * @param result
+ * @param postData
+ */
+var processPost = function(result, postData)
+{
+ return new Promise(function(resolve, reject)
+ {
+ var postParsed = qs.parse(postData);
+
+ if(postParsed.edit_post)
+ {
+ //display edit form
+ displayRenderForm(result, postParsed.edit_post).then(function()
+ {
+ resolve();
+ }).catch(function(error)
+ {
+ console.log(error);
+ console.log("error processing the edit post data");
+ });
+ }
+ else if(postParsed.edit_post_2)
+ {
+ //insert edit into sql
+
+ sql.editPost(postParsed).then(function()
+ {
+ resolve();
+ }).catch(function(error)
+ {
+ console.log("error inserting edit post data into sql");
+ });
+ }
+ else
+ {
+ resolve();
+ }
+ });
+};
+
+
+module.exports=
+{
+ /**
+ * Method which calls helper functions which processes post data for editing posts
+ * and calls a function which displays all the posts in a table
+ * @param result
+ * @param postData
+ */
+ main: function(result, postData)
+ {
+ return new Promise(function(resolve, reject)
+ {
+ processPost(result, postData).then(function()
+ {
+ return postsTable(result);
+ }).then(function()
+ {
+ resolve();
+ }).catch(function(error)
+ {
+ console.log("Error in edit post module");
+ reject(error);
+ });
+ });
+ }
+};
\ No newline at end of file
diff --git a/includes/header.html b/includes/header.html
index 25cdabd..9611146 100644
--- a/includes/header.html
+++ b/includes/header.html
@@ -11,6 +11,7 @@
+
diff --git a/server.js b/server.js
index b976797..ceb749b 100644
--- a/server.js
+++ b/server.js
@@ -19,7 +19,7 @@ const utils = require('./utils/utils.js');
//
// var forceSsl = require('express-force-ssl');
-var map = require('./utils/generateSiteMap');
+var map = require('./utils/generateSiteMap.js');
map.main();
var app = express();
diff --git a/utils/sql.js b/utils/sql.js
index 83556fd..d46d92f 100644
--- a/utils/sql.js
+++ b/utils/sql.js
@@ -36,7 +36,8 @@ var fetch = function(sqlStatement)
{
if(err)
{
- reject();
+ console.log(err);
+ reject(err);
}
resolve(result);
});
@@ -69,6 +70,27 @@ module.exports=
})
},
+ /**
+ * function which fetches the sql info on a post based on it's sql id
+ * @param id
+ * @returns {Array}
+ */
+ getPostById: function(id)
+ {
+ console.log("select * from posts where post_id='" + id + "' limit 1");
+
+ return new Promise(function(resolve, reject)
+ {
+ fetch("select * from posts where post_id='" + id + "' limit 1").then(function(post)
+ {
+ resolve(post[0]);
+ }).catch(function(error)
+ {
+ reject(error);
+ });
+ });
+ },
+
/**
* Not to be mistaken for getPostData() in @file utils/utils.js,
* this function extracts a post entry from the sql server
@@ -259,6 +281,21 @@ module.exports=
});
},
+ /**
+ * Fetches a promise containing every post in the database
+ * @returns {Array}
+ */
+ getAllPosts: function()
+ {
+ return fetch("select * from posts order by published desc");
+ },
+
+
+ /**
+ * Fetches the sql category information based on it's id
+ * @param categoryId
+ * @returns {Array}
+ */
getCategory: function(categoryId)
{
return fetch("select * from categories where category_id='"
@@ -272,6 +309,24 @@ module.exports=
return fetch(q);
},
+ editPost: function(postData)
+ {
+ var url = postData.edit_name_new.split(" ").join("-").toLowerCase();
+ var q = "update posts ";
+ q+= "set category_id='" + postData.edit_cat_num + "' ";
+ q+= ",name='" + postData.edit_name_new + "' ";
+ q+= ",url='" + url + "' ";
+ q+= ",picture_url='" + postData.edit_pic + "' ";
+ q+= ",published='" + postData.edit_date + "' ";
+ q+= " where post_id='" + postData.edit_post_2 + "'";
+ return module.exports.insert(q);
+ },
+
+ /**
+ * Function which returns a promise which contains the string of the
+ * entire sitemap for the blog.
+ * @returns {Promise|*}
+ */
getSiteMap: function()
{
return new Promise(function(resolve, reject)