Minor Optimization/Refactoring
This commit is contained in:
@ -1,5 +1,4 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -30,7 +29,6 @@ namespace Database {
|
|||||||
/*
|
/*
|
||||||
* Name and Statement for prepared statement to select an item based on a given ID
|
* 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
|
* 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 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;";
|
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 {
|
struct requestsItem {
|
||||||
int id = 0;
|
int id = 0;
|
||||||
std::string customerName = "";
|
std::string customerName;
|
||||||
std::string customerEmailAddress = "";
|
std::string customerEmailAddress;
|
||||||
std::string customerContactDetails = "";
|
std::string customerContactDetails;
|
||||||
int freelancerID = 0;
|
int freelancerID = 0;
|
||||||
int templateID = 0;
|
int templateID = 0;
|
||||||
std::string currencyPreference = "";
|
std::string currencyPreference;
|
||||||
std::string priceUpFront = "";
|
std::string priceUpFront;
|
||||||
std::string priceOnDeliver = "";
|
std::string priceOnDeliver;
|
||||||
std::string requestDescription = "";
|
std::string requestDescription;
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
std::string upFrontInvoiceID = "";
|
std::string upFrontInvoiceID;
|
||||||
std::string onDeliverInvoiceID = "";
|
std::string onDeliverInvoiceID;
|
||||||
bool upFrontPaid = false;
|
bool upFrontPaid = false;
|
||||||
bool onDeliverPaid = false;
|
bool onDeliverPaid = false;
|
||||||
bool completed = false;
|
bool completed = false;
|
||||||
@ -127,24 +125,25 @@ namespace Database {
|
|||||||
/*
|
/*
|
||||||
* outputs request item in a JSON String
|
* outputs request item in a JSON String
|
||||||
*/
|
*/
|
||||||
std::string toJSONString(){
|
std::string toJSONString() const{
|
||||||
std::string outputString = "{\"requestItem:\":{";
|
std::string outputString;
|
||||||
outputString += "\"id\": " + std::to_string(id) + ",";
|
outputString = R"({"requestItem:":{)";
|
||||||
outputString += "\"customerName\": \"" + customerName + "\",";
|
outputString += R"("id": )" + std::to_string(id) + ",";
|
||||||
outputString += "\"customerEmailAddress\": \"" + customerEmailAddress + "\",";
|
outputString += R"("customerName": ")" + customerName + R"(",)";
|
||||||
outputString += "\"customerContactDetails\": \"" + customerContactDetails + "\",";
|
outputString += R"("customerEmailAddress": ")" + customerEmailAddress + R"(",)";
|
||||||
outputString += "\"freelancerID\": " + std::to_string(freelancerID) + ",";
|
outputString += R"("customerContactDetails": ")" + customerContactDetails + R"(",)";
|
||||||
outputString += "\"templateID\": " + std::to_string(templateID) + ",";
|
outputString += R"("freelancerID": )" + std::to_string(freelancerID) + ",";
|
||||||
outputString += "\"currencyPreference\": \"" + currencyPreference + "\",";
|
outputString += R"("templateID": )" + std::to_string(templateID) + ",";
|
||||||
outputString += "\"priceUpFront\": \"" + priceUpFront + "\",";
|
outputString += R"("currencyPreference": ")" + currencyPreference + R"(",)";
|
||||||
outputString += "\"priceOnDeliver\": \"" + priceOnDeliver + "\",";
|
outputString += R"("priceUpFront": ")" + priceUpFront + R"(",)";
|
||||||
outputString += "\"requestDescription\": \"" + requestDescription + "\",";
|
outputString += R"("priceOnDeliver": ")" + priceOnDeliver + R"(",)";
|
||||||
outputString += "\"accepted\": " + std::to_string(accepted) + ",";
|
outputString += R"("requestDescription": ")" + requestDescription + R"(",)";
|
||||||
outputString += "\"invoiceID\": \"" + upFrontInvoiceID + "\",";
|
outputString += R"("accepted": )" + std::to_string(accepted) + ",";
|
||||||
outputString += "\"onDeliverInvoiceID\": \"" + onDeliverInvoiceID + "\",";
|
outputString += R"("invoiceID": ")" + upFrontInvoiceID + R"(",)";
|
||||||
outputString += "\"upFrontPaid\": " + std::to_string(upFrontPaid) + ",";
|
outputString += R"("onDeliverInvoiceID": ")" + onDeliverInvoiceID + R"(",)";
|
||||||
outputString += "\"onDeliverPaid\": " + std::to_string(onDeliverPaid) + ",";
|
outputString += R"("upFrontPaid": )" + std::to_string(upFrontPaid) + ",";
|
||||||
outputString += "\"completed\": " + std::to_string(completed);
|
outputString += R"("onDeliverPaid": )" + std::to_string(onDeliverPaid) + ",";
|
||||||
|
outputString += R"("completed": )" + std::to_string(completed);
|
||||||
outputString += "}}";
|
outputString += "}}";
|
||||||
return outputString;
|
return outputString;
|
||||||
}
|
}
|
||||||
@ -152,7 +151,7 @@ namespace Database {
|
|||||||
/*
|
/*
|
||||||
* prints request item into standard out
|
* prints request item into standard out
|
||||||
*/
|
*/
|
||||||
void outputItem(){
|
void outputItem() const{
|
||||||
std::cout << "id = " << id << std::endl;
|
std::cout << "id = " << id << std::endl;
|
||||||
std::cout << "customerName = " << customerName << std::endl;
|
std::cout << "customerName = " << customerName << std::endl;
|
||||||
std::cout << "customerEmailAddress = " << customerEmailAddress << 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
|
* 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[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[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[2])) customerEmailAddress = itemJson[JSON_ITEM_NAMES[2]].s();
|
||||||
@ -198,7 +197,7 @@ namespace Database {
|
|||||||
* Executes an SQL query and returns results
|
* Executes an SQL query and returns results
|
||||||
* Takes an open pqxx::connection
|
* 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::work work(connection);
|
||||||
pqxx::result result = work.exec(sqlQuery);
|
pqxx::result result = work.exec(sqlQuery);
|
||||||
work.commit();
|
work.commit();
|
||||||
@ -213,7 +212,7 @@ namespace Database {
|
|||||||
* 1 = query error
|
* 1 = query error
|
||||||
* 2 = critical 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 {
|
try {
|
||||||
connection.prepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, SQL_STATEMENT_INSERT_ITEM_IN_REQUESTS);
|
connection.prepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, SQL_STATEMENT_INSERT_ITEM_IN_REQUESTS);
|
||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
@ -235,6 +234,7 @@ namespace Database {
|
|||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,7 +246,7 @@ namespace Database {
|
|||||||
* 1 = query error
|
* 1 = query error
|
||||||
* 2 = critical 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 {
|
try {
|
||||||
connection.prepare(PREPARED_STATEMENT_INSERT_NEW_FREELANCER, SQL_STATEMENT_INSERT_NEW_FREELANCER);
|
connection.prepare(PREPARED_STATEMENT_INSERT_NEW_FREELANCER, SQL_STATEMENT_INSERT_NEW_FREELANCER);
|
||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
@ -263,6 +263,7 @@ namespace Database {
|
|||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_INSERT_NEW_FREELANCER);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,6 +287,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ITEM_BY_ID, id);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ITEM_BY_ID, id);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_ITEM_BY_ID);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,6 +300,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER, freelancerID);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER, freelancerID);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -310,6 +313,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE, freelancerID);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE, freelancerID);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,6 +326,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL, freelancerID);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL, freelancerID);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,11 +334,14 @@ namespace Database {
|
|||||||
* Executes the prepared statement SELECT_FREELANCER_SALT
|
* Executes the prepared statement SELECT_FREELANCER_SALT
|
||||||
* Takes an open pqxx::connection and the id to select by
|
* 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);
|
connection.prepare(PREPARED_STATEMENT_SELECT_FREELANCER_SALT, SQL_STATEMENT_SELECT_FREELANCER_SALT);
|
||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_SALT, freelancerEmail);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_SALT, freelancerEmail);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_SALT);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,11 +350,15 @@ namespace Database {
|
|||||||
* Takes an open pqxx::connection and the emailAddress to check
|
* Takes an open pqxx::connection and the emailAddress to check
|
||||||
* Delivers count of emailaddress occurence 0 for none 1+ for more
|
* 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);
|
connection.prepare(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS, SQL_STATEMENT_SELECT_CHECK_EMAIL_EXISTS);
|
||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS, freelancerEmail);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS, freelancerEmail);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_CHECK_EMAIL_EXISTS);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,11 +367,12 @@ namespace Database {
|
|||||||
* Takes an open pqxx::connection and the emailAddress and hash to check
|
* Takes an open pqxx::connection and the emailAddress and hash to check
|
||||||
* Delivers 0 if email + hash are not valid 1 if they are
|
* 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);
|
connection.prepare(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID, SQL_STATEMENT_SELECT_CHECK_HASH_VALID);
|
||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID, emailAddress, hash);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID, emailAddress, hash);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_CHECK_HASH_VALID);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,6 +385,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE, templateID);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE, templateID);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_TEMPLATE);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,6 +398,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT, templateID);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT, templateID);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,6 +411,7 @@ namespace Database {
|
|||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES, freelancerID);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES, freelancerID);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -403,11 +419,14 @@ namespace Database {
|
|||||||
* Executes the prepared statement SELECT_ALIAS
|
* Executes the prepared statement SELECT_ALIAS
|
||||||
* Takes an open pqxx::connection and the alias name to select by
|
* 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);
|
connection.prepare(PREPARED_STATEMENT_SELECT_ALIAS, SQL_STATEMENT_SELECT_ALIAS);
|
||||||
pqxx::work work(connection);
|
pqxx::work work(connection);
|
||||||
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ALIAS, aliasName);
|
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_ALIAS, aliasName);
|
||||||
work.commit();
|
work.commit();
|
||||||
|
connection.unprepare(PREPARED_STATEMENT_SELECT_ALIAS);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
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
|
* 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
|
* 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;
|
std::vector<crow::json::wvalue> jsonVector;
|
||||||
for (int row = 0; row < result.size(); ++row) {
|
for (int row = 0; row < result.size(); ++row) {
|
||||||
crow::json::wvalue jsonVectorItem;
|
crow::json::wvalue jsonVectorItem;
|
||||||
|
@ -11,7 +11,7 @@ using namespace std;
|
|||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
Utilities::config configuration;
|
Utilities::config configuration;
|
||||||
if (argc > 0)
|
if (argc > 1)
|
||||||
configuration.configPath = argv[1];
|
configuration.configPath = argv[1];
|
||||||
|
|
||||||
if (configuration.readConfigFile()) {
|
if (configuration.readConfigFile()) {
|
||||||
@ -20,8 +20,6 @@ int main(int argc, char *argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
string databaseURI = configuration.databaseConnectionString;
|
|
||||||
|
|
||||||
// Create app with Middleware
|
// Create app with Middleware
|
||||||
crow::App<crow::CookieParser> app;
|
crow::App<crow::CookieParser> app;
|
||||||
|
|
||||||
|
@ -1,21 +1,14 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <list>
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <fstream>
|
#include <utility>
|
||||||
#include <locale>
|
|
||||||
#include <codecvt>
|
|
||||||
#include <regex>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <sstream>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <pqxx/pqxx>
|
#include <pqxx/pqxx>
|
||||||
#include "crow.h"
|
#include "crow.h"
|
||||||
#include "cpp/opportunisticsecuresmtpclient.hpp"
|
#include "cpp/opportunisticsecuresmtpclient.hpp"
|
||||||
#include "cpp/plaintextmessage.hpp"
|
|
||||||
#include "cpp/htmlmessage.hpp"
|
#include "cpp/htmlmessage.hpp"
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
@ -32,7 +25,7 @@ namespace Utilities {
|
|||||||
/*
|
/*
|
||||||
* Takes string to split it into a vector based on a given delimiter
|
* Takes string to split it into a vector based on a given delimiter
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> splitStringIntoVector(std::string stringToSplit, char delimiter) {
|
std::vector<std::string> splitStringIntoVector(const std::string& stringToSplit, char delimiter) {
|
||||||
std::vector<std::string> splitVector;
|
std::vector<std::string> splitVector;
|
||||||
std::istringstream stringStream(stringToSplit);
|
std::istringstream stringStream(stringToSplit);
|
||||||
while (!stringStream.eof()) {
|
while (!stringStream.eof()) {
|
||||||
@ -66,7 +59,7 @@ namespace Utilities {
|
|||||||
* validates existence of mandatory variables in config
|
* validates existence of mandatory variables in config
|
||||||
* returns 0 if successful else 1
|
* returns 0 if successful else 1
|
||||||
*/
|
*/
|
||||||
int checkMandatoryVariables() {
|
int checkMandatoryVariables() const {
|
||||||
if (emailAddress.compare("") == 0
|
if (emailAddress.compare("") == 0
|
||||||
|| emailPassword.compare("") == 0
|
|| emailPassword.compare("") == 0
|
||||||
|| emailServerAddress.compare("") == 0
|
|| emailServerAddress.compare("") == 0
|
||||||
@ -81,7 +74,7 @@ namespace Utilities {
|
|||||||
* returns 0 if successful else 1
|
* returns 0 if successful else 1
|
||||||
*/
|
*/
|
||||||
int readConfigFile(){
|
int readConfigFile(){
|
||||||
bool errorLevel = 0;
|
int errorLevel = 0;
|
||||||
std::ifstream configFile(configPath);
|
std::ifstream configFile(configPath);
|
||||||
std::cout << "Loading Configuration: " << configPath << std::endl;
|
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
|
* 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());
|
return responseBody.substr(valueName.length() + 1, responseBody.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* replaces a string with another string within a string
|
* 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);
|
int stringPosition = stringToProcess.find(from);
|
||||||
while (stringPosition != std::string::npos) {
|
while (stringPosition != std::string::npos) {
|
||||||
stringToProcess.replace(stringToProcess.find(from), std::string(from).size(), to);
|
stringToProcess.replace(stringToProcess.find(from), std::string(from).size(), to);
|
||||||
@ -182,8 +175,8 @@ namespace Utilities {
|
|||||||
*/
|
*/
|
||||||
void decodeString(std::string &stringToDecode) {
|
void decodeString(std::string &stringToDecode) {
|
||||||
replaceString(stringToDecode, "+", " ");
|
replaceString(stringToDecode, "+", " ");
|
||||||
for(auto it = HTML_URL_CODES.begin(); it != HTML_URL_CODES.end(); ++it) {
|
for(const auto & it : HTML_URL_CODES) {
|
||||||
replaceString(stringToDecode, it->first, it->second);
|
replaceString(stringToDecode, it.first, it.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,14 +184,14 @@ namespace Utilities {
|
|||||||
* Sends an HTML Email using CPP-SMTPClient-library
|
* Sends an HTML Email using CPP-SMTPClient-library
|
||||||
* return 0 at success, 1 at client fail, 2 at critical fail.
|
* 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);
|
OpportunisticSecureSMTPClient client(configuration.emailServerAddress, configuration.emailServerPort);
|
||||||
client.setCredentials(Credential(configuration.emailAddress, configuration.emailPassword));
|
client.setCredentials(Credential(configuration.emailAddress, configuration.emailPassword));
|
||||||
try {
|
try {
|
||||||
const MessageAddress from(configuration.emailAddress, configuration.emailAddressDisplay);
|
const MessageAddress from(configuration.emailAddress, configuration.emailAddressDisplay);
|
||||||
const auto to = { MessageAddress(destinationEmail) };
|
const auto to = { MessageAddress(destinationEmail) };
|
||||||
const auto subject = emailSubject;
|
const auto subject = std::move(emailSubject);
|
||||||
const auto body = htmlBody;
|
const auto body = std::move(htmlBody);
|
||||||
const std::vector<MessageAddress> cc = {};
|
const std::vector<MessageAddress> cc = {};
|
||||||
const std::vector<MessageAddress> bcc = {};
|
const std::vector<MessageAddress> bcc = {};
|
||||||
const std::vector<Attachment> attachments = {};
|
const std::vector<Attachment> attachments = {};
|
||||||
@ -222,7 +215,7 @@ namespace Utilities {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string createHashSha512(const std::string str){
|
std::string createHashSha512(const std::string& str){
|
||||||
unsigned char hash[SHA512_DIGEST_LENGTH];
|
unsigned char hash[SHA512_DIGEST_LENGTH];
|
||||||
|
|
||||||
SHA512_CTX sha512;
|
SHA512_CTX sha512;
|
||||||
@ -256,7 +249,7 @@ namespace Utilities {
|
|||||||
/*
|
/*
|
||||||
* Hashes a given password with a given salt
|
* 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);
|
return createHashSha512(pwsalt + password);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user