Browse Source

Updated admin CMS to be able to edit posts after they were entered

pull/4/head
jrtechs 6 years ago
parent
commit
47bc41c38f
6 changed files with 281 additions and 10 deletions
  1. +3
    -7
      admin/addCategory.js
  2. +8
    -1
      admin/admin.js
  3. +212
    -0
      admin/editPost.js
  4. +1
    -0
      includes/header.html
  5. +1
    -1
      server.js
  6. +56
    -1
      utils/sql.js

+ 3
- 7
admin/addCategory.js View File

@ -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("</tr>");
});
res.write("</tbody></table></div>");
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("</div>");
resolve();
resolve(postData);
}).catch(function(err)
{
console.log(err);

+ 8
- 1
admin/admin.js View File

@ -26,10 +26,17 @@ module.exports=
}).then(function(postData)
{
return require("../admin/addCategory.js").main(result, postData);
}).then(function()
}).then(function(postData)
{
result.write("</div>");
console.log("hmmm");
return require("../admin/editPost.js").main(result, postData);
}).then(function()
{
resolve();
}).catch(function(error)
{
throw error;
});
}
else

+ 212
- 0
admin/editPost.js View File

@ -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("<tr>");
//category
result.write("<td>" + post.category_id + "</td>");
//name
result.write("<td>" + post.name + "</td>");
//picture
result.write("<td>" + post.picture_url + "</td>");
//date
result.write("<td>" + post.published + "</td>");
//edit
result.write("<td><form action=\"/admin/\" method =\"post\" >\n" +
" <input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
" class=\"w3-teal w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align\"/>\n" +
"<input type='hidden' name='edit_post' value='" + post.post_id + "'/>"+
"</form></td>");
result.write("</tr>");
resolve();
});
};
/**
* Displays all the posts in a table
* @param result
*/
var postsTable = function(result)
{
result.write("<div class='w3-row'>");
result.write("<h1 class=\"w3-text-teal w3-center\">Posts</h1>");
result.write("<div class=\"w3-responsive w3-card-4\"><table class=\"w3-table w3-striped w3-bordered\"><thead>");
result.write("<tr class=\"w3-teal\">");
result.write("<td>Category #</td><td>Name</td><td>Header Picture</td><td>Date</td><td>Edit</td>");
result.write("</tr></thead><tbody>");
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("</tbody></table></div></div>");
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("<div class='w3-row'>"+
"<h1 class=\"w3-text-teal w3-center\">Edit Post</h1>"+
"<form action=\"/admin/\" method =\"post\" class=\"w3-container w3-card-4\">"+
" <div class=\"w3-group w3-padding-16\">\n" +
" <input class=\"w3-input\" type=\"text\" name=\"edit_cat_num\" value='" + post.category_id + "' required>\n" +
" <label class=\"w3-label w3-validate\">Category Number</label>\n" +
" </div>"+
" <div class=\"w3-group w3-padding-16\">\n" +
" <input class=\"w3-input\" type=\"text\" name=\"edit_name_new\" value='" + post.name + "' required>\n" +
" <label class=\"w3-label w3-validate\">Post Title</label>\n" +
" </div>"+
" <div class=\"w3-group w3-padding-16\">\n" +
" <input class=\"w3-input\" type=\"text\" name=\"edit_pic\" value='" + post.picture_url + "' required>\n" +
" <label class=\"w3-label w3-validate\">Picture URL</label>\n" +
" </div>"+
" <div class=\"w3-group w3-padding-16\">\n" +
" <input class=\"w3-input\" type=\"date\" name=\"edit_date\" value='" + post.published.toISOString().split('T')[0] + "' required>\n" +
" <label class=\"w3-label w3-validate\">Published Date</label>\n" +
" </div>"+
" <p><input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
" class=\"w3-teal w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align\"/></p>"+
"<input type='hidden' name='edit_post_2' value='" + post_id + "'/>"+
"</form>"+
"</div>"
);
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);
});
});
}
};

+ 1
- 0
includes/header.html View File

@ -11,6 +11,7 @@
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<meta name="description" content="Blog of Jeffery Russell, featuring technical guides, and projects.">
<meta name="author" content="Jeffery Russell">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">

+ 1
- 1
server.js View File

@ -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();

+ 56
- 1
utils/sql.js View File

@ -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)

Loading…
Cancel
Save