/** 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);
|
|
});
|
|
});
|
|
}
|
|
};
|