Minor Optimization/Refactoring

This commit is contained in:
Tina_Azure
2023-04-22 23:01:44 +02:00
parent 78e1544321
commit 11ecf6452e
3 changed files with 72 additions and 62 deletions

View File

@ -1,5 +1,4 @@
#include <string>
#include <sstream>
#include <list>
#include <iostream>
#include <vector>
@ -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<crow::json::wvalue> jsonVector;
for (int row = 0; row < result.size(); ++row) {
crow::json::wvalue jsonVectorItem;