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