From 11ecf6452e08fe2d1a1b6f9dca1bb02d41d76e88 Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Sat, 22 Apr 2023 23:01:44 +0200 Subject: [PATCH] Minor Optimization/Refactoring --- src/database.cpp | 97 ++++++++++++++++++++++++++++------------------- src/main.cpp | 4 +- src/utilities.cpp | 33 +++++++--------- 3 files changed, 72 insertions(+), 62 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index 745bd9d..b053ca7 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -30,7 +29,6 @@ namespace Database { /* * Name and Statement for prepared statement to select an item based on a given ID * Case for boolean return values because the default return value is f/t instead of 0/1 or false/true - * todo:validate */ const static std::string PREPARED_STATEMENT_SELECT_ITEM_BY_ID = "selectItemByID"; const static std::string SQL_STATEMENT_SELECT_ITEM_BY_ID = "SELECT requests.id , customerEmailAddress, freelancers.name, templates.name, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, (CASE WHEN requests.accepted THEN 1 ELSE 0 END) AS accepted, upFrontInvoiceID, onDeliverInvoiceID, (CASE WHEN requests.upFrontPaid THEN 1 ELSE 0 END) AS upFrontPaid, (CASE WHEN requests.onDeliverPaid THEN 1 ELSE 0 END) AS onDeliverPaid FROM requests INNER JOIN freelancers ON freelancers.id=requests.freelancerID INNER JOIN templates ON templates.id=requests.templateID WHERE requests.id=$1;"; @@ -108,18 +106,18 @@ namespace Database { */ struct requestsItem { int id = 0; - std::string customerName = ""; - std::string customerEmailAddress = ""; - std::string customerContactDetails = ""; + 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 = ""; + std::string currencyPreference; + std::string priceUpFront; + std::string priceOnDeliver; + std::string requestDescription; bool accepted = false; - std::string upFrontInvoiceID = ""; - std::string onDeliverInvoiceID = ""; + std::string upFrontInvoiceID; + std::string onDeliverInvoiceID; bool upFrontPaid = false; bool onDeliverPaid = false; bool completed = false; @@ -127,24 +125,25 @@ namespace Database { /* * outputs request item in a JSON String */ - std::string toJSONString(){ - std::string outputString = "{\"requestItem:\":{"; - outputString += "\"id\": " + std::to_string(id) + ","; - outputString += "\"customerName\": \"" + customerName + "\","; - outputString += "\"customerEmailAddress\": \"" + customerEmailAddress + "\","; - outputString += "\"customerContactDetails\": \"" + customerContactDetails + "\","; - outputString += "\"freelancerID\": " + std::to_string(freelancerID) + ","; - outputString += "\"templateID\": " + std::to_string(templateID) + ","; - outputString += "\"currencyPreference\": \"" + currencyPreference + "\","; - outputString += "\"priceUpFront\": \"" + priceUpFront + "\","; - outputString += "\"priceOnDeliver\": \"" + priceOnDeliver + "\","; - outputString += "\"requestDescription\": \"" + requestDescription + "\","; - outputString += "\"accepted\": " + std::to_string(accepted) + ","; - outputString += "\"invoiceID\": \"" + upFrontInvoiceID + "\","; - outputString += "\"onDeliverInvoiceID\": \"" + onDeliverInvoiceID + "\","; - outputString += "\"upFrontPaid\": " + std::to_string(upFrontPaid) + ","; - outputString += "\"onDeliverPaid\": " + std::to_string(onDeliverPaid) + ","; - outputString += "\"completed\": " + std::to_string(completed); + 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; } @@ -152,7 +151,7 @@ namespace Database { /* * prints request item into standard out */ - void outputItem(){ + void outputItem() const{ std::cout << "id = " << id << std::endl; std::cout << "customerName = " << customerName << std::endl; std::cout << "customerEmailAddress = " << customerEmailAddress << std::endl; @@ -174,7 +173,7 @@ namespace Database { /* * Parses a JSON and fills the Item with the delivered values */ - void insertJsonIntoItem(crow::json::rvalue itemJson){ + 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(); @@ -198,7 +197,7 @@ namespace Database { * Executes an SQL query and returns results * Takes an open pqxx::connection */ - pqxx::result executeSQL(pqxx::connection &connection, std::string sqlQuery) { + pqxx::result executeSQL(pqxx::connection &connection, const std::string& sqlQuery) { pqxx::work work(connection); pqxx::result result = work.exec(sqlQuery); work.commit(); @@ -213,7 +212,7 @@ namespace Database { * 1 = query error * 2 = critical error */ - int executePreparedStatement_INSERT_ITEM_IN_REQUESTS(pqxx::connection &connection, requestsItem item) { + int executePreparedStatement_INSERT_ITEM_IN_REQUESTS(pqxx::connection &connection, const requestsItem& item) { try { connection.prepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, SQL_STATEMENT_INSERT_ITEM_IN_REQUESTS); pqxx::work work(connection); @@ -235,6 +234,7 @@ namespace Database { std::cerr << e.what() << std::endl; return 2; } + connection.unprepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS); return 0; } @@ -246,7 +246,7 @@ namespace Database { * 1 = query error * 2 = critical error */ - int executePreparedStatement_INSERT_NEW_FREELANCER(pqxx::connection &connection, std::string email, std::string name, std::string salt, std::string hash) { + 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 { connection.prepare(PREPARED_STATEMENT_INSERT_NEW_FREELANCER, SQL_STATEMENT_INSERT_NEW_FREELANCER); pqxx::work work(connection); @@ -263,6 +263,7 @@ namespace Database { std::cerr << e.what() << std::endl; return 2; } + connection.unprepare(PREPARED_STATEMENT_INSERT_NEW_FREELANCER); return 0; } @@ -286,6 +287,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ITEM_BY_ID, id); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_ITEM_BY_ID); return result; } @@ -298,6 +300,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER, freelancerID); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER); return result; } @@ -310,6 +313,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE, freelancerID); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE); return result; } @@ -322,6 +326,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL, freelancerID); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL); return result; } @@ -329,11 +334,14 @@ namespace Database { * 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, std::string freelancerEmail) { + pqxx::result executePreparedStatement_SELECT_FREELANCER_SALT(pqxx::connection &connection, const std::string& freelancerEmail) { connection.prepare(PREPARED_STATEMENT_SELECT_FREELANCER_SALT, SQL_STATEMENT_SELECT_FREELANCER_SALT); pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_SALT, freelancerEmail); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_SALT); + return result; + } return result; } @@ -342,11 +350,15 @@ namespace Database { * Takes an open pqxx::connection and the emailAddress to check * Delivers count of emailaddress occurence 0 for none 1+ for more */ - pqxx::result executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(pqxx::connection &connection, std::string freelancerEmail) { + pqxx::result executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(pqxx::connection &connection, const std::string& freelancerEmail) { connection.prepare(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS, SQL_STATEMENT_SELECT_CHECK_EMAIL_EXISTS); pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS, freelancerEmail); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS); + return result; + } + return result; } @@ -355,11 +367,12 @@ namespace Database { * 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, std::string emailAddress, std::string hash) { + pqxx::result executePreparedStatement_SELECT_CHECK_HASH_VALID(pqxx::connection &connection, const std::string& emailAddress, const std::string& hash) { connection.prepare(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID, SQL_STATEMENT_SELECT_CHECK_HASH_VALID); pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID, emailAddress, hash); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID); return result; } @@ -372,6 +385,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE, templateID); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_TEMPLATE); return result; } @@ -384,6 +398,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT, templateID); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT); return result; } @@ -396,6 +411,7 @@ namespace Database { pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES, freelancerID); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES); return result; } @@ -403,11 +419,14 @@ namespace Database { * 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, std::string aliasName) { + pqxx::result executePreparedStatement_SELECT_ALIAS(pqxx::connection &connection, const std::string& aliasName) { connection.prepare(PREPARED_STATEMENT_SELECT_ALIAS, SQL_STATEMENT_SELECT_ALIAS); pqxx::work work(connection); pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ALIAS, aliasName); work.commit(); + connection.unprepare(PREPARED_STATEMENT_SELECT_ALIAS); + return result; + } return result; } @@ -430,7 +449,7 @@ namespace Database { * 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, std::string jsonName){ + 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; diff --git a/src/main.cpp b/src/main.cpp index 0147b0d..25d6164 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,7 @@ using namespace std; int main(int argc, char *argv[]) { Utilities::config configuration; - if (argc > 0) + if (argc > 1) configuration.configPath = argv[1]; if (configuration.readConfigFile()) { @@ -20,8 +20,6 @@ int main(int argc, char *argv[]) { return 1; } - string databaseURI = configuration.databaseConnectionString; - // Create app with Middleware crow::App app; diff --git a/src/utilities.cpp b/src/utilities.cpp index 78ffc6a..49feaa6 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -1,21 +1,14 @@ #include -#include #include #include #include -#include -#include -#include -#include +#include #include -#include #include -#include #include #include #include "crow.h" #include "cpp/opportunisticsecuresmtpclient.hpp" -#include "cpp/plaintextmessage.hpp" #include "cpp/htmlmessage.hpp" #include @@ -32,7 +25,7 @@ namespace Utilities { /* * Takes string to split it into a vector based on a given delimiter */ - std::vector splitStringIntoVector(std::string stringToSplit, char delimiter) { + std::vector splitStringIntoVector(const std::string& stringToSplit, char delimiter) { std::vector splitVector; std::istringstream stringStream(stringToSplit); while (!stringStream.eof()) { @@ -66,7 +59,7 @@ namespace Utilities { * validates existence of mandatory variables in config * returns 0 if successful else 1 */ - int checkMandatoryVariables() { + int checkMandatoryVariables() const { if (emailAddress.compare("") == 0 || emailPassword.compare("") == 0 || emailServerAddress.compare("") == 0 @@ -81,7 +74,7 @@ namespace Utilities { * returns 0 if successful else 1 */ int readConfigFile(){ - bool errorLevel = 0; + int errorLevel = 0; std::ifstream configFile(configPath); std::cout << "Loading Configuration: " << configPath << std::endl; @@ -162,14 +155,14 @@ namespace Utilities { /* * Takes String and cuts from the start-up to the length of valueName */ - std::string extractSingleValueFromRequestBody(std::string responseBody, std::string valueName) { + std::string extractSingleValueFromRequestBody(const std::string& responseBody, const std::string& valueName) { return responseBody.substr(valueName.length() + 1, responseBody.length()); } /* * replaces a string with another string within a string */ - void replaceString(std::string &stringToProcess, std::string from, std::string to) { + void replaceString(std::string &stringToProcess, const std::string& from, const std::string& to) { int stringPosition = stringToProcess.find(from); while (stringPosition != std::string::npos) { stringToProcess.replace(stringToProcess.find(from), std::string(from).size(), to); @@ -182,8 +175,8 @@ namespace Utilities { */ void decodeString(std::string &stringToDecode) { replaceString(stringToDecode, "+", " "); - for(auto it = HTML_URL_CODES.begin(); it != HTML_URL_CODES.end(); ++it) { - replaceString(stringToDecode, it->first, it->second); + for(const auto & it : HTML_URL_CODES) { + replaceString(stringToDecode, it.first, it.second); } } @@ -191,14 +184,14 @@ namespace Utilities { * Sends an HTML Email using CPP-SMTPClient-library * return 0 at success, 1 at client fail, 2 at critical fail. */ - int sendEmail(config configuration, std::string destinationEmail, std::string emailSubject, std::string htmlBody){ + int sendEmail(const config& configuration, const std::string& destinationEmail, std::string emailSubject, std::string htmlBody){ OpportunisticSecureSMTPClient client(configuration.emailServerAddress, configuration.emailServerPort); client.setCredentials(Credential(configuration.emailAddress, configuration.emailPassword)); try { const MessageAddress from(configuration.emailAddress, configuration.emailAddressDisplay); const auto to = { MessageAddress(destinationEmail) }; - const auto subject = emailSubject; - const auto body = htmlBody; + const auto subject = std::move(emailSubject); + const auto body = std::move(htmlBody); const std::vector cc = {}; const std::vector bcc = {}; const std::vector attachments = {}; @@ -222,7 +215,7 @@ namespace Utilities { return 0; } - std::string createHashSha512(const std::string str){ + std::string createHashSha512(const std::string& str){ unsigned char hash[SHA512_DIGEST_LENGTH]; SHA512_CTX sha512; @@ -256,7 +249,7 @@ namespace Utilities { /* * Hashes a given password with a given salt */ - std::string hashPassword(std::string pwsalt, std::string password) { + std::string hashPassword(const std::string& pwsalt, const std::string& password) { return createHashSha512(pwsalt + password); } } \ No newline at end of file