Database Manager refactoring/optimization
Some minor general refactoring
This commit is contained in:
66
src/main.cpp
66
src/main.cpp
@ -52,13 +52,17 @@ int main(int argc, char *argv[]) {
|
||||
//Parses the request body and extracts the specified value
|
||||
int freelancerID = stoi(Utilities::extractSingleValueFromRequestBody(postRequest.body.c_str(), "freelancerID"));
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_FREELANCER,
|
||||
ID_SELECT_FREELANCER_TEMPLATES
|
||||
});
|
||||
pqxx::result resultFreelancer = Database::executePreparedStatement_SELECT_FREELANCER(databaseConnection, freelancerID);
|
||||
|
||||
//error handling if invalid ID was delivered
|
||||
string freelancerHTML = "customer_FreelancerListing_NOTFOUND.html";
|
||||
|
||||
crow::json::wvalue resultJsonFreelancerTemplate;
|
||||
if (resultFreelancer.size() != 0){
|
||||
if (!resultFreelancer.empty()){
|
||||
freelancerHTML = "customer_FreelancerListing.html";
|
||||
pqxx::result resultFreelancerTemplates = Database::executePreparedStatement_SELECT_FREELANCER_TEMPLATES(databaseConnection, freelancerID);
|
||||
resultJsonFreelancerTemplate = Database::convertResultToJSON(resultFreelancerTemplates, "templates");
|
||||
@ -67,7 +71,7 @@ int main(int argc, char *argv[]) {
|
||||
auto page = crow::mustache::load(freelancerHTML);
|
||||
crow::mustache::context ctx(resultJsonFreelancerTemplate);
|
||||
|
||||
if (resultFreelancer.size() != 0){
|
||||
if (!resultFreelancer.empty()){
|
||||
/*
|
||||
* puts freelancer data into context
|
||||
*/
|
||||
@ -91,6 +95,10 @@ int main(int argc, char *argv[]) {
|
||||
int templateID = stoi(Utilities::extractSingleValueFromRequestBody(postRequest.body.c_str(), "templateID"));
|
||||
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_TEMPLATE,
|
||||
ID_SELECT_FREELANCER_COMMISSION_STATE
|
||||
});
|
||||
pqxx::result resultTemplate = Database::executePreparedStatement_SELECT_TEMPLATE(databaseConnection, templateID);
|
||||
|
||||
crow::json::wvalue resultJsonTemplate = Database::convertResultRowToJSON(resultTemplate);
|
||||
@ -101,11 +109,11 @@ int main(int argc, char *argv[]) {
|
||||
crow::mustache::context ctx(resultJsonTemplate);
|
||||
|
||||
bool commissionState = false;
|
||||
if (resultTemplate.size() > 0) {
|
||||
if (!resultTemplate.empty()) {
|
||||
//use freelancerID based on SQL_STATEMENT_SELECT_TEMPLATE
|
||||
pqxx::result resultCommissionState = Database::executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(databaseConnection, stoi(resultTemplate.at(0).at(2).c_str()));
|
||||
//the commissionstate
|
||||
if (resultCommissionState.size() == 0 || stoi(resultCommissionState.at(0).at(0).c_str()) == 1)
|
||||
if (resultCommissionState.empty() || stoi(resultCommissionState.at(0).at(0).c_str()) == 1)
|
||||
commissionState = true;
|
||||
}
|
||||
ctx["ERROR_COMMISSIONS_CLOSED"] = commissionState;
|
||||
@ -121,6 +129,7 @@ int main(int argc, char *argv[]) {
|
||||
int templateID = stoi(Utilities::extractSingleValueFromRequestBody(postRequest.body.c_str(), "templateID"));
|
||||
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatement(databaseConnection, ID_SELECT_TEMPLATE);
|
||||
pqxx::result resultTemplate = Database::executePreparedStatement_SELECT_TEMPLATE(databaseConnection, templateID);
|
||||
|
||||
crow::json::wvalue resultJsonTemplate = Database::convertResultRowToJSON(resultTemplate);
|
||||
@ -150,7 +159,7 @@ int main(int argc, char *argv[]) {
|
||||
Utilities::decodeString(postRequestBody);
|
||||
vector<string> splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&');
|
||||
bool requestFillCompletion = false;
|
||||
for (string item : splitPostRequestBody) {
|
||||
for (const string& item : splitPostRequestBody) {
|
||||
vector<string> splitItem = Utilities::splitStringIntoVector(item, '=');
|
||||
if (splitItem.at(0) == "customername")
|
||||
newRequest.customerName = splitItem.at(1);
|
||||
@ -166,12 +175,17 @@ int main(int argc, char *argv[]) {
|
||||
if (newRequest.customerName != "" && (newRequest.customerEmailAddress != "" || newRequest.customerContactDetails != "") && newRequest.requestDescription != "" )
|
||||
requestFillCompletion = true;
|
||||
|
||||
|
||||
|
||||
ctx["templateid"] = newRequest.templateID;
|
||||
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_TEMPLATE_FLAT,
|
||||
ID_SELECT_FREELANCER_COMMISSION_STATE,
|
||||
ID_INSERT_ITEM_IN_REQUESTS,
|
||||
ID_SELECT_FREELANCER_EMAIL
|
||||
});
|
||||
|
||||
pqxx::result resultTemplate = Database::executePreparedStatement_SELECT_TEMPLATE_FLAT(databaseConnection, newRequest.templateID);
|
||||
if (resultTemplate.size() > 0 && requestFillCompletion) {
|
||||
if (!resultTemplate.empty() && requestFillCompletion) {
|
||||
for (int i = 0; i < resultTemplate.columns(); ++i) {
|
||||
//freelancerid
|
||||
newRequest.freelancerID = stoi(resultTemplate.at(0).at(0).c_str());
|
||||
@ -186,7 +200,7 @@ int main(int argc, char *argv[]) {
|
||||
pqxx::result resultCommissionState = Database::executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(databaseConnection, newRequest.freelancerID);
|
||||
|
||||
//the commissionstate
|
||||
if (resultCommissionState.size() == 0 || stoi(resultCommissionState.at(0).at(0).c_str()) == 1) {
|
||||
if (resultCommissionState.empty() || stoi(resultCommissionState.at(0).at(0).c_str()) == 1) {
|
||||
ctx["ERROR_COMMISSIONS_CLOSED"] = true;
|
||||
}
|
||||
else {
|
||||
@ -194,7 +208,7 @@ int main(int argc, char *argv[]) {
|
||||
if (resultInsertOperation == 0) {
|
||||
templateHTML = "customer_Freelancer_Template_Request_Fulfilment.html";
|
||||
pqxx::result resultEmailAddress = Database::executePreparedStatement_SELECT_FREELANCER_EMAIL(databaseConnection, newRequest.freelancerID);
|
||||
if (resultEmailAddress.size() > 0)
|
||||
if (!resultEmailAddress.empty())
|
||||
Utilities::sendEmail(configuration, resultEmailAddress.at(0).at(0).c_str(), "NEW REQUEST", newRequest.toJSONString());
|
||||
}
|
||||
else {
|
||||
@ -243,6 +257,7 @@ int main(int argc, char *argv[]) {
|
||||
string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
||||
if (!freelancerEmail.empty() && !loginKey.empty()) {
|
||||
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail)) {
|
||||
Database::prepareStatement(databaseConnection, ID_UPDATE_LOGIN_VALIDATION_KEY);
|
||||
Database::executePreparedStatement_UPDATE_LOGIN_VALIDATION_KEY(databaseConnection, "EXPIRED", freelancerEmail);
|
||||
ctx.set_cookie("loginKey", Utilities::generateExpiredCookie());
|
||||
ctx.set_cookie("freelancerEmail", Utilities::generateExpiredCookie());
|
||||
@ -297,6 +312,12 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
if (!email.empty()) {
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_CHECK_EMAIL_EXISTS,
|
||||
ID_SELECT_CHECK_FREELANCER_RESET_KEY,
|
||||
ID_DELETE_FREELANCER_RESET_KEY,
|
||||
ID_INSERT_FREELANCER_RESET_KEY
|
||||
});
|
||||
pqxx::result checkFreelancerExists = Database::executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(databaseConnection, email);
|
||||
int checkFreelancerExistsExtracted = stoi(checkFreelancerExists.at(0).at(0).c_str());
|
||||
if (checkFreelancerExistsExtracted == 1) {
|
||||
@ -325,6 +346,7 @@ int main(int argc, char *argv[]) {
|
||||
([&, configuration](string passwordResetKey) {
|
||||
crow::mustache::context ctx;
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatement(databaseConnection, ID_SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY);
|
||||
pqxx::result freelancerEmail = Database::executePreparedStatement_SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY(databaseConnection, passwordResetKey);
|
||||
if (!freelancerEmail.empty()) {
|
||||
ctx["freelanceremail"] = freelancerEmail.at(0).at(0).c_str();
|
||||
@ -355,8 +377,14 @@ int main(int argc, char *argv[]) {
|
||||
passwordConfirmation = splitItem.at(1);
|
||||
}
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY,
|
||||
ID_SELECT_CHECK_FREELANCER_RESET_KEY_EXPIRED,
|
||||
ID_DELETE_FREELANCER_RESET_KEY,
|
||||
ID_UPDATE_FREELANCER_PASSWORD_HASH
|
||||
});
|
||||
pqxx::result freelancerEmail = Database::executePreparedStatement_SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY(databaseConnection, passwordResetKey);
|
||||
if (!freelancerEmail.empty() && !password.empty() && !(password.compare(passwordConfirmation) == 0)) {
|
||||
if (!freelancerEmail.empty() && !password.empty() && password.compare(passwordConfirmation) != 0) {
|
||||
string email = freelancerEmail.at(0).at(0).c_str();
|
||||
pqxx::result keyExpiration = Database::executePreparedStatement_SELECT_CHECK_FREELANCER_RESET_KEY_EXPIRED(databaseConnection, email);
|
||||
if (stoi(keyExpiration.at(0).at(0).c_str()) == 0) {
|
||||
@ -387,7 +415,7 @@ int main(int argc, char *argv[]) {
|
||||
ctx["PASSWORD_EMPTY"] = true;
|
||||
if (freelancerEmail.empty())
|
||||
ctx["PASSWORD_RESET_DOES_NOT_EXIST"] = true;
|
||||
if (!(password.compare(passwordConfirmation) == 0))
|
||||
if (password.compare(passwordConfirmation) != 0)
|
||||
ctx["PASSWORD_RESET_PASS_CONFIRMATION"] = true;
|
||||
}
|
||||
auto page = crow::mustache::load("passwordReset_Fulfillment.html");
|
||||
@ -420,6 +448,12 @@ int main(int argc, char *argv[]) {
|
||||
if (!email.empty() && !password.empty()){
|
||||
//check if freelancer exists
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_CHECK_EMAIL_EXISTS,
|
||||
ID_SELECT_FREELANCER_SALT,
|
||||
ID_SELECT_CHECK_HASH_VALID,
|
||||
ID_UPDATE_LOGIN_VALIDATION_KEY
|
||||
});
|
||||
pqxx::result checkFreelancerExists = Database::executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(databaseConnection, email);
|
||||
int checkFreelancerExistsExtracted = stoi(checkFreelancerExists.at(0).at(0).c_str());
|
||||
if (checkFreelancerExistsExtracted == 1) {
|
||||
@ -505,12 +539,16 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
//check if signup data is complete
|
||||
if (!email.empty() && !name.empty() && !password.empty() && !(password.compare(passwordConfirmation) == 0))
|
||||
if (!email.empty() && !name.empty() && !password.empty() && password.compare(passwordConfirmation) != 0)
|
||||
requestFillCompletion = true;
|
||||
|
||||
if (requestFillCompletion) {
|
||||
//check if email address hasn't been registered yet
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_CHECK_EMAIL_EXISTS,
|
||||
ID_INSERT_NEW_FREELANCER
|
||||
});
|
||||
pqxx::result checkResult = Database::executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(databaseConnection, email);
|
||||
int checkResultExtracted = stoi(checkResult.at(0).at(0).c_str());
|
||||
|
||||
@ -542,7 +580,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
else {
|
||||
ctx["REGISTRATION_ERROR"] = true;
|
||||
if (!(password.compare(passwordConfirmation) == 0))
|
||||
if (password.compare(passwordConfirmation) != 0)
|
||||
ctx["REGISTRATION_ERROR_PASS_CONFIRMATION"] = true;
|
||||
ctx["REGISTRATION_ERROR_EMAIL_NAME_PASS_NOT_FILLED"] = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user