Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

213 lines
6.7 KiB

/** 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=\"btn btn-secondary\"/>\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='blogPost p-2'>");
result.write("<h1 class=\"text-center\">Posts</h1>");
result.write("<div class=\"\"><table class=\"table table-striped\">");
result.write("<thead class=\"thead-dark\"><tr>");
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><br>");
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='blogPost p-2'>"+
"<h1 class=\"text-center\">Edit Post</h1>"+
"<form action=\"/admin/\" method =\"post\" >"+
" <div class=\"form-group\">\n" +
" <input class=\"form-control\" 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=\"form-group\">\n" +
" <input class=\"form-control\" type=\"text\" name=\"edit_name_new\" value='" + post.name + "' required>\n" +
" <label class=\"w3-label w3-validate\">Post Title</label>\n" +
" </div>"+
" <div class=\"form-group\">\n" +
" <input class=\"form-control\" type=\"text\" name=\"edit_pic\" value='" + post.picture_url + "' required>\n" +
" <label class=\"w3-label w3-validate\">Picture URL</label>\n" +
" </div>"+
" <div class=\"form-group\">\n" +
" <input class=\"form-control\" 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>"+
" <div><input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
" class=\"btn btn-lg btn-secondary\"/></div>"+
"<input type='hidden' name='edit_post_2' value='" + post_id + "'/>"+
"</form>"+
"</div><br>"
);
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)
{
result.write("<br>");
processPost(result, postData).then(function()
{
return postsTable(result);
}).then(function()
{
resolve();
}).catch(function(error)
{
console.log("Error in edit post module");
reject(error);
});
});
}
};