#ifndef DATABASE_CPP #define DATABASE_CPP #include #include #include #include #include #include #include "crow.h" #include "databaseStatementConstCollection.cpp" using namespace DatabaseStatementConstCollection; /* * Database Manager */ namespace Database { //Valid names for the JSON const static std::string JSON_ITEM_NAMES[] ={"id", "customerName", "customerEmailAddress", "customerContactDetails", "freelancerID", "templateID", "currencyPreference", "priceUpFront", "priceOnDeliver", "requestDescription", "accepted", "upFrontInvoiceID", "onDeliverInvoiceID", "upFrontPaid", "onDeliverPaid", "completed"}; /* * Struct Representing an item in Requests */ struct requestsItem { int id = 0; std::string customerName; std::string customerEmailAddress; std::string customerContactDetails; int freelancerID = 0; int templateID = 0; std::string currencyPreference; std::string priceUpFront; std::string priceOnDeliver; std::string requestDescription; bool accepted = false; std::string upFrontInvoiceID; std::string onDeliverInvoiceID; bool upFrontPaid = false; bool onDeliverPaid = false; bool completed = false; /* * outputs request item in a JSON String */ std::string toJSONString() const{ std::string outputString; outputString = R"({"requestItem:":{)"; outputString += R"("id": )" + std::to_string(id) + ","; outputString += R"("customerName": ")" + customerName + R"(",)"; outputString += R"("customerEmailAddress": ")" + customerEmailAddress + R"(",)"; outputString += R"("customerContactDetails": ")" + customerContactDetails + R"(",)"; outputString += R"("freelancerID": )" + std::to_string(freelancerID) + ","; outputString += R"("templateID": )" + std::to_string(templateID) + ","; outputString += R"("currencyPreference": ")" + currencyPreference + R"(",)"; outputString += R"("priceUpFront": ")" + priceUpFront + R"(",)"; outputString += R"("priceOnDeliver": ")" + priceOnDeliver + R"(",)"; outputString += R"("requestDescription": ")" + requestDescription + R"(",)"; outputString += R"("accepted": )" + std::to_string(accepted) + ","; outputString += R"("invoiceID": ")" + upFrontInvoiceID + R"(",)"; outputString += R"("onDeliverInvoiceID": ")" + onDeliverInvoiceID + R"(",)"; outputString += R"("upFrontPaid": )" + std::to_string(upFrontPaid) + ","; outputString += R"("onDeliverPaid": )" + std::to_string(onDeliverPaid) + ","; outputString += R"("completed": )" + std::to_string(completed); outputString += "}}"; return outputString; } /* * prints request item into standard out */ void outputItem() const{ std::cout << "id = " << id << std::endl; std::cout << "customerName = " << customerName << std::endl; std::cout << "customerEmailAddress = " << customerEmailAddress << std::endl; std::cout << "customerContactDetails = " << customerContactDetails << std::endl; std::cout << "freelancerID = " << freelancerID << std::endl; std::cout << "templateID = " << templateID << std::endl; std::cout << "currencyPreference = " << currencyPreference << std::endl; std::cout << "priceUpFront = " << priceUpFront << std::endl; std::cout << "priceOnDeliver = " << priceOnDeliver << std::endl; std::cout << "requestDescription = " << requestDescription << std::endl; std::cout << "accepted = " << accepted << std::endl; std::cout << "InvoiceID = " << upFrontInvoiceID << std::endl; std::cout << "onDeliverInvoiceID = " << onDeliverInvoiceID << std::endl; std::cout << "upFrontPaid = " << upFrontPaid << std::endl; std::cout << "onDeliverPaid = " << onDeliverPaid << std::endl; std::cout << "completed = " << completed << std::endl; } /* * Parses a JSON and fills the Item with the delivered values */ void insertJsonIntoItem(const crow::json::rvalue& itemJson){ if (itemJson.has(JSON_ITEM_NAMES[0])) id = itemJson[JSON_ITEM_NAMES[0]].i(); if (itemJson.has(JSON_ITEM_NAMES[1])) customerName = itemJson[JSON_ITEM_NAMES[1]].s(); if (itemJson.has(JSON_ITEM_NAMES[2])) customerEmailAddress = itemJson[JSON_ITEM_NAMES[2]].s(); if (itemJson.has(JSON_ITEM_NAMES[3])) customerContactDetails = itemJson[JSON_ITEM_NAMES[3]].s(); if (itemJson.has(JSON_ITEM_NAMES[4])) freelancerID = itemJson[JSON_ITEM_NAMES[4]].i(); if (itemJson.has(JSON_ITEM_NAMES[5])) templateID = itemJson[JSON_ITEM_NAMES[5]].i(); if (itemJson.has(JSON_ITEM_NAMES[6])) currencyPreference = itemJson[JSON_ITEM_NAMES[6]].s(); if (itemJson.has(JSON_ITEM_NAMES[7])) priceUpFront = itemJson[JSON_ITEM_NAMES[7]].s(); if (itemJson.has(JSON_ITEM_NAMES[8])) priceOnDeliver = itemJson[JSON_ITEM_NAMES[8]].s(); if (itemJson.has(JSON_ITEM_NAMES[9])) requestDescription = itemJson[JSON_ITEM_NAMES[9]].s(); if (itemJson.has(JSON_ITEM_NAMES[10])) accepted = itemJson[JSON_ITEM_NAMES[10]].b(); if (itemJson.has(JSON_ITEM_NAMES[11])) upFrontInvoiceID = itemJson[JSON_ITEM_NAMES[11]].s(); if (itemJson.has(JSON_ITEM_NAMES[12])) onDeliverInvoiceID = itemJson[JSON_ITEM_NAMES[12]].s(); if (itemJson.has(JSON_ITEM_NAMES[13])) upFrontPaid = itemJson[JSON_ITEM_NAMES[13]].b(); if (itemJson.has(JSON_ITEM_NAMES[14])) onDeliverPaid = itemJson[JSON_ITEM_NAMES[14]].b(); if (itemJson.has(JSON_ITEM_NAMES[15])) completed = itemJson[JSON_ITEM_NAMES[15]].b(); } }; /* * Executes an SQL query and returns results * Takes an open pqxx::connection */ pqxx::result executeSQL(pqxx::connection &connection, const std::string& sqlQuery) { pqxx::work work(connection); pqxx::result result = work.exec(sqlQuery); work.commit(); return result; } /* * Executes the prepared statement INSERT_ITEM_IN_REQUESTS * Takes an open pqxx::connection and the requestsItem to insert * returns errorLevel * 0 = no error * 1 = query error * 2 = critical error */ int executePreparedStatement_INSERT_ITEM_IN_REQUESTS(pqxx::connection &connection, const requestsItem& item) { try { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, item.customerEmailAddress, item.freelancerID, item.templateID, item.currencyPreference, item.priceUpFront, item.priceOnDeliver, item.requestDescription, item.accepted, item.upFrontInvoiceID, item.onDeliverInvoiceID, item.upFrontPaid, item.onDeliverPaid, item.completed, item.customerContactDetails, item.customerName); work.commit(); } catch (pqxx::sql_error const &e) { std::cerr << "Database error: " << e.what() << std::endl << "Query was: " << e.query() << std::endl; return 1; } catch (std::exception const &e) { std::cerr << e.what() << std::endl; return 2; } return 0; } /* * Executes the prepared statement INSERT_NEW_FREELANCER * Takes an open pqxx::connection and the name, email, salt and hash * returns errorLevel * 0 = no error * 1 = query error * 2 = critical error */ int executePreparedStatement_INSERT_NEW_FREELANCER(pqxx::connection &connection, const std::string& email, const std::string& name, const std::string& salt, const std::string& hash) { try { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_INSERT_NEW_FREELANCER, email, name, salt, hash); work.commit(); } catch (pqxx::sql_error const &e) { std::cerr << "Database error: " << e.what() << std::endl << "Query was: " << e.query() << std::endl; return 1; } catch (std::exception const &e) { std::cerr << e.what() << std::endl; return 2; } return 0; } /* * Executes the SELECT_FREELANCERS_WITHCOMMISSIONSSTATE statement * Takes an open pqxx::connection */ pqxx::result executeStatement_SELECT_FREELANCERS_WITHCOMMISSIONSSTATE(pqxx::connection &connection) { pqxx::work work(connection); pqxx::result result = work.exec(SQL_Statement_SELECT_FREELANCERS_WITHCOMMISSIONSSTATE); work.commit(); return result; } /* * Executes the SELECT_FREELANCERS_WITHCOMMISSIONSSTATE statement * Takes an open pqxx::connection */ pqxx::result executeStatement_SELECT_FREELANCERS_COUNT(pqxx::connection &connection) { pqxx::work work(connection); pqxx::result result = work.exec(SQL_Statement_SELECT_FREELANCERS_COUNT); work.commit(); return result; } /* * Executes the SELECT_FREELANCERS_WITHCOMMISSIONSSTATE_LIMITED_AND_OFFSET statement * Takes an open pqxx::connection the limit and the offset */ pqxx::result executePreparedStatement_SELECT_FREELANCERS_WITHCOMMISSIONSSTATE_LIMITED_AND_OFFSET(pqxx::connection &connection, int limit, int offset) { if (offset < 0) offset = 0; pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCERS_WITHCOMMISSIONSSTATE_LIMITED_AND_OFFSET, limit, offset); work.commit(); return result; } /* * Executes the PURGE_EXPIRED_FREELANCER_RESET_KEYS statement * Takes an open pqxx::connection */ void executeStatement_STATEMENT_PURGE_EXPIRED_FREELANCER_RESET_KEYS(pqxx::connection &connection) { pqxx::work work(connection); work.exec(SQL_STATEMENT_PURGE_EXPIRED_FREELANCER_RESET_KEYS); work.commit(); } /* * Executes the PURGE_EXPIRED_LOGIN_LOCKOUTS statement * Takes an open pqxx::connection */ void executeStatement_STATEMENT_PURGE_EXPIRED_LOGIN_LOCKOUTS(pqxx::connection &connection) { pqxx::work work(connection); work.exec(SQL_STATEMENT_PURGE_EXPIRED_LOGIN_LOCKOUTS); work.commit(); } /* * Executes the prepared statement SELECT_ITEM_BY_ID * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_ITEM_BY_ID(pqxx::connection &connection, int id) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ITEM_BY_ID, id); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER(pqxx::connection &connection, int freelancerID) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER, freelancerID); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER_COMMISSION_STATE * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(pqxx::connection &connection, int freelancerID) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE, freelancerID); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER_EMAIL * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_EMAIL(pqxx::connection &connection, int freelancerID) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL, freelancerID); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER_SALT * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_SALT(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_SALT, freelancerEmail); connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_SALT); return result; } /* * Executes the prepared statement SELECT_FREELANCER_ID * Takes an open pqxx::connection and the email to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_ID(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_ID, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER_NAME * Takes an open pqxx::connection and the email to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_NAME(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_NAME, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement SELECT_CHECK_EMAIL_EXISTS * Takes an open pqxx::connection and the emailAddress to check * Delivers count of emailaddress occurrence 0 for none 1+ for more */ pqxx::result executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement SELECT_CHECK_FREELANCER_LOGIN_STATE * Takes an open pqxx::connection the loginKey and the id to check * Delivers count of loginValidationKey occurrence 0 for none 1+ for more */ pqxx::result executePreparedStatement_SELECT_CHECK_FREELANCER_LOGIN_STATE(pqxx::connection &connection, const std::string& freelancerEmail, const std::string& loginKey) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_FREELANCER_LOGIN_STATE, freelancerEmail, loginKey); work.commit(); return result; } /* * Executes the prepared statement SELECT_CHECK_HASH_VALID * Takes an open pqxx::connection and the emailAddress and hash to check * Delivers 0 if email + hash are not valid 1 if they are */ pqxx::result executePreparedStatement_SELECT_CHECK_HASH_VALID(pqxx::connection &connection, const std::string& emailAddress, const std::string& hash) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID, emailAddress, hash); work.commit(); return result; } /* * Executes the prepared statement SELECT_CHECK_LOGIN_LOCK_OUT * Takes an open pqxx::connection and the emailAddress * Delivers true if the login for the given email is locked */ pqxx::result executePreparedStatement_SELECT_CHECK_LOGIN_LOCK_OUT(pqxx::connection &connection, const std::string& emailAddress) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_LOGIN_LOCK_OUT, emailAddress); work.commit(); return result; } /* * Executes the prepared statement SELECT_GET_LOGIN_LOCK_OUT_MINUTES * Takes an open pqxx::connection and the emailAddress * Delivers minutes until the login lock out expires */ pqxx::result executePreparedStatement_SELECT_GET_LOGIN_LOCK_OUT_MINUTES(pqxx::connection &connection, const std::string& emailAddress) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_GET_LOGIN_LOCK_OUT_MINUTES, emailAddress); work.commit(); return result; } /* * Executes the prepared statement INSERT_LOGIN_LOCK_OUT * Takes an open pqxx::connection and the emailAddress */ void executePreparedStatement_INSERT_LOGIN_LOCK_OUT(pqxx::connection &connection, const std::string& emailAddress) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_INSERT_LOGIN_LOCK_OUT, emailAddress); work.commit(); } /* * Executes the prepared statement UPDATE_INCREMENT_LOGIN_LOCK_OUT_ATTEMPTS * Takes an open pqxx::connection and the emailAddress */ void executePreparedStatement_UPDATE_INCREMENT_LOGIN_LOCK_OUT_ATTEMPTS(pqxx::connection &connection, const std::string& emailAddress) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_INCREMENT_LOGIN_LOCK_OUT_ATTEMPTS, emailAddress); work.commit(); } /* * Executes the prepared statement CHECK_LOGIN_LOCK_OUT_ATTEMPTS * Takes an open pqxx::connection the emailAddress and the max attempts * returns true if the lock out attempts, exceed or equate the given max attempts */ pqxx::result executePreparedStatement_CHECK_LOGIN_LOCK_OUT_ATTEMPTS(pqxx::connection &connection, const std::string& emailAddress, int maxAttempts) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS, emailAddress, maxAttempts); work.commit(); return result; } /* * Executes the prepared statement UPDATE_EXPIRATION_LOGIN_LOCK_OUT * Takes an open pqxx::connection the emailAddress and the additive lock out in seconds */ void executePreparedStatement_UPDATE_EXPIRATION_LOGIN_LOCK_OUT(pqxx::connection &connection, const std::string& emailAddress, int lockOutSeconds) { pqxx::work work(connection); std::string lockOutString = std::to_string(lockOutSeconds) + " second"; pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_EXPIRATION_LOGIN_LOCK_OUT, emailAddress, lockOutSeconds); work.commit(); } /* * Executes the prepared statement SELECT_TEMPLATE * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_TEMPLATE(pqxx::connection &connection, int templateID) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE, templateID); work.commit(); return result; } /* * Executes the prepared statement SELECT_TEMPLATE_FLAT * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_TEMPLATE_FLAT(pqxx::connection &connection, int templateID) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT, templateID); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER_TEMPLATES * Takes an open pqxx::connection and the id to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_TEMPLATES(pqxx::connection &connection, int freelancerID) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES, freelancerID); work.commit(); return result; } /* * Executes the prepared statement SELECT_ALIAS * Takes an open pqxx::connection and the alias name to select by */ pqxx::result executePreparedStatement_SELECT_ALIAS(pqxx::connection &connection, const std::string& aliasName) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ALIAS, aliasName); work.commit(); return result; } /* * Executes the prepared statement INSERT_FREELANCER_ALIAS * Takes an open pqxx::connection and aliasname, freelanceremail, route, routeparameter, routevalue * returns errorLevel * 0 = no error * 1 = query error * 2 = critical error */ int executePreparedStatement_INSERT_FREELANCER_ALIAS(pqxx::connection &connection, const std::string& aliasname, const std::string& freelanceremail, const std::string& route, const std::string& routeparameter, const std::string& routevalue) { try { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_INSERT_FREELANCER_ALIAS, aliasname, freelanceremail, route, routeparameter, routevalue); work.commit(); } catch (pqxx::sql_error const &e) { std::cerr << "Database error: " << e.what() << std::endl << "Query was: " << e.query() << std::endl; return 1; } catch (std::exception const &e) { std::cerr << e.what() << std::endl; return 2; } return 0; } /* * Executes the prepared statement SELECT_FREELANCER_ALIAS * Takes an open pqxx::connection and the freelancer email to select by */ pqxx::result executePreparedStatement_SELECT_FREELANCER_ALIAS(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_ALIAS, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement DELETE_FREELANCER_ALIAS * Deletes the entry for a reset key based on a freelancer email and the alias name * Takes an open pqxx::connection and the freelancers email */ void executePreparedStatement_DELETE_FREELANCER_ALIAS(pqxx::connection &connection, const std::string& aliasName, const std::string& freelancerEmail) { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_DELETE_FREELANCER_ALIAS, aliasName, freelancerEmail); work.commit(); } /* * Executes the prepared statement SELECT_CHECK_FREELANCER_ALIAS * Takes an open pqxx::connection and the alias name * Delivers true if the alias is already used */ bool executePreparedStatement_SELECT_CHECK_FREELANCER_ALIAS(pqxx::connection &connection, const std::string& aliasName) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_FREELANCER_ALIAS, aliasName); work.commit(); std::string extractedResult = result.at(0).at(0).c_str(); return extractedResult == "t"; } /* * Executes the prepared statement UPDATE_LOGIN_VALIDATION_KEY * Takes an open pqxx::connection, the freelancerID and the loginKey to update the entry */ pqxx::result executePreparedStatement_UPDATE_LOGIN_VALIDATION_KEY(pqxx::connection &connection, const std::string& loginKey, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_LOGIN_VALIDATION_KEY, loginKey, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement UPDATE_FREELANCER_PASSWORD_HASH * Takes an open pqxx::connection, the freelancer email * returns errorLevel * 0 = no error * 1 = query error * 2 = critical error */ int executePreparedStatement_UPDATE_FREELANCER_PASSWORD_HASH(pqxx::connection &connection, const std::string& hash, const std::string& salt, const std::string& freelancerEmail) { try { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH, hash, salt, freelancerEmail); work.commit(); } catch (pqxx::sql_error const &e) { std::cerr << "Database error: " << e.what() << std::endl << "Query was: " << e.query() << std::endl; return 1; } catch (std::exception const &e) { std::cerr << e.what() << std::endl; return 2; } return 0; } /* * Executes the prepared statement SELECT_CHECK_FREELANCER_RESET_KEY * Takes an open pqxx::connection and the password reset key * returns 1 if email has reset key */ pqxx::result executePreparedStatement_SELECT_CHECK_FREELANCER_RESET_KEY(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_FREELANCER_RESET_KEY, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement SELECT_CHECK_FREELANCER_RESET_KEY_EXPIRED * Takes an open pqxx::connection and the password reset key * returns 0 if reset key is not expired */ pqxx::result executePreparedStatement_SELECT_CHECK_FREELANCER_RESET_KEY_EXPIRED(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_FREELANCER_RESET_KEY_EXPIRED, freelancerEmail); work.commit(); return result; } /* * Executes the prepared statement SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY * Takes an open pqxx::connection and the password reset key * returns email if reset key exists */ pqxx::result executePreparedStatement_SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY(pqxx::connection &connection, const std::string& passwordResetKey) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL_FROM_PASSWORD_RESET_KEY, passwordResetKey); work.commit(); return result; } /* * Executes the prepared statement DELETE_FREELANCER_RESET_KEY * Deletes the entry for a reset key based on a freelancer email * Takes an open pqxx::connection and the freelancers email */ void executePreparedStatement_DELETE_FREELANCER_RESET_KEY(pqxx::connection &connection, const std::string& freelancerEmail) { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_DELETE_FREELANCER_RESET_KEY, freelancerEmail); work.commit(); } /* * Executes the prepared statement INSERT_FREELANCER_RESET_KEY * Creates a new entry for a password reset key for a freelancer * Takes an open pqxx::connection the freelancers email and the password reset key */ void executePreparedStatement_INSERT_FREELANCER_RESET_KEY(pqxx::connection &connection, const std::string& freelancerEmail, const std::string& passwordResetKey) { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_INSERT_FREELANCER_RESET_KEY, freelancerEmail, passwordResetKey); work.commit(); } /* * Executes the prepared statement INSERT_FREELANCER_TEMPLATE * Takes an open pqxx::connection and the strings name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, freelancerEmail * returns errorLevel * 0 = no error * 1 = query error * 2 = critical error */ int executePreparedStatement_INSERT_FREELANCER_TEMPLATE(pqxx::connection &connection, const std::string& name, const std::string& content, const std::string& contactdata, const std::string& contactinformation, const std::string& currencypreference, const std::string& priceupfront, const std::string& priceondeliver, const std::string& freelancerEmail) { try { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_INSERT_FREELANCER_TEMPLATE, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, freelancerEmail); work.commit(); } catch (pqxx::sql_error const &e) { std::cerr << "Database error: " << e.what() << std::endl << "Query was: " << e.query() << std::endl; return 1; } catch (std::exception const &e) { std::cerr << e.what() << std::endl; return 2; } return 0; } /* * Executes the prepared statement UPDATE_EDIT_FREELANCER_TEMPLATE * Takes an open pqxx::connection name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, templateid and the freelancers email * returns true if update occured */ bool executePreparedStatement_UPDATE_EDIT_FREELANCER_TEMPLATE(pqxx::connection &connection, const std::string& name, const std::string& content, const std::string& contactdata, const std::string& contactinformation, const std::string& currencypreference, const std::string& priceupfront, const std::string& priceondeliver, int templateid, const std::string& emailAddress) { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, templateid, emailAddress); work.commit(); return result.affected_rows() != 0; } /* * Executes the prepared statement DELETE_FREELANCER_TEMPLATE * Deletes a templated based on the id and validated with the freelancer email * Takes an open pqxx::connection the template id and the freelancers email */ void executePreparedStatement_DELETE_FREELANCER_TEMPLATE(pqxx::connection &connection, int templateID, const std::string& freelancerEmail) { pqxx::work work(connection); work.exec_prepared(PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE, templateID, freelancerEmail); work.commit(); } /* * Prepares a statement based on ID * Takes an open pqxx::connection, the statement id */ void prepareStatement(pqxx::connection &connection, int statementID) { const std::string& preparedStatementName = preparedStatementNameCollection.at(statementID); connection.prepare(preparedStatementName, preparedStatementCollection.at(preparedStatementName)); } /* * Prepares a statement based on Statement name * Takes an open pqxx::connection, the statement name */ void prepareStatement(pqxx::connection &connection, const std::string& PREPARED_STATEMENT) { connection.prepare(PREPARED_STATEMENT, preparedStatementCollection.at(PREPARED_STATEMENT)); } /* * Prepares Statements * ID list is found as consts in DatabaseStatementConstCollection */ void prepareStatements(pqxx::connection &connection, const std::vector& statementIDsToPrepare) { for (int statementID : statementIDsToPrepare) { prepareStatement(connection, preparedStatementNameCollection.at(statementID)); } } /* * parses the result and returns a JSON * Takes a result and optionally the desired row * !the result delivers only lowercase column names, either the templates are appropriately changed * or a validation is performed at conversion. */ crow::json::wvalue convertResultRowToJSON(pqxx::result &result, int row = 0){ crow::json::wvalue returnJson; for (int i = 0; i < result.columns(); ++i) { returnJson[result.column_name(i)] = result.at(row).at(i).c_str(); } return returnJson; } /* * parses the result and returns a JSON * parses it fully and returns a JSON containing it at the top or below a variable * takes the result and optionally a name for the top level variable */ crow::json::wvalue convertResultToJSON(pqxx::result &result, const std::string& jsonName){ std::vector jsonVector; for (int row = 0; row < result.size(); ++row) { crow::json::wvalue jsonVectorItem; for (int i = 0; i < result.columns(); ++i) { jsonVectorItem[result.column_name(i)] = result.at(row).at(i).c_str(); } jsonVector.push_back(jsonVectorItem); } crow::json::wvalue returnJson; if (!jsonName.empty()){ returnJson[jsonName] = crow::json::wvalue(jsonVector); } else { returnJson = crow::json::wvalue(jsonVector); } return returnJson; } } #endif