diff --git a/src/database.cpp b/src/database.cpp index 3866e99..6de5330 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -462,6 +462,18 @@ namespace Database { 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 UPDATE_LOGIN_VALIDATION_KEY * Takes an open pqxx::connection, the freelancerID and the loginKey to update the entry diff --git a/src/databaseStatementConstCollection.cpp b/src/databaseStatementConstCollection.cpp index 778e696..e7c9e7e 100644 --- a/src/databaseStatementConstCollection.cpp +++ b/src/databaseStatementConstCollection.cpp @@ -106,6 +106,11 @@ namespace DatabaseStatementConstCollection { const static std::string PREPARED_STATEMENT_SELECT_FREELANCER_ALIAS = "selectFreelancerAlias"; const static std::string SQL_STATEMENT_SELECT_FREELANCER_ALIAS = "select aliasname, route from aliasroutes where freelancerid = (select freelancers.id from freelancers where emailaddress = $1);"; + /* + * Name and Statement for prepared statement to select an alias and its route via the alias name + */ + const static std::string PREPARED_STATEMENT_DELETE_FREELANCER_ALIAS = "deleteFreelancerAlias"; + const static std::string SQL_STATEMENT_DELETE_FREELANCER_ALIAS = "delete from aliasroutes where aliasname = $1 and freelancerid = (select freelancers.id from freelancers where emailaddress = $2);"; /* * Name and Statement for prepared statement to update the loginvalidationkey of a freelancer via the freelancerID */ @@ -247,6 +252,7 @@ namespace DatabaseStatementConstCollection { const static int ID_DELETE_FREELANCER_TEMPLATE = 30; const static int ID_UPDATE_EDIT_FREELANCER_TEMPLATE = 31; const static int ID_SELECT_FREELANCER_ALIAS = 32; + const static int ID_DELETE_FREELANCER_ALIAS = 33; /* * Easy access to prepared statements via prepared statement name @@ -285,6 +291,7 @@ namespace DatabaseStatementConstCollection { {PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE, SQL_STATEMENT_DELETE_FREELANCER_TEMPLATE}, {PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE, SQL_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE}, {PREPARED_STATEMENT_SELECT_FREELANCER_ALIAS, SQL_STATEMENT_SELECT_FREELANCER_ALIAS}, + {PREPARED_STATEMENT_DELETE_FREELANCER_ALIAS, SQL_STATEMENT_DELETE_FREELANCER_ALIAS}, }; /* * Easy access to prepared statement name via int @@ -323,6 +330,7 @@ namespace DatabaseStatementConstCollection { {ID_DELETE_FREELANCER_TEMPLATE, PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE}, {ID_UPDATE_EDIT_FREELANCER_TEMPLATE, PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE}, {ID_SELECT_FREELANCER_ALIAS, PREPARED_STATEMENT_SELECT_FREELANCER_ALIAS}, + {ID_DELETE_FREELANCER_ALIAS, PREPARED_STATEMENT_DELETE_FREELANCER_ALIAS}, }; /* diff --git a/src/main.cpp b/src/main.cpp index 27af83d..b542b39 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -870,6 +870,31 @@ int main(int argc, char *argv[]) { return page.render(ctx); }); + /* + * Execute Alias deletion + */ + CROW_ROUTE(app, "/freelancer/aliasManagement/delete").methods("POST"_method) + ([&, configuration](const crow::request& postRequest) { + auto& cookieCtx = app.get_context(postRequest); + crow::mustache::context ctx; + if (Utilities::checkCookieLoginState(configuration, cookieCtx)) { + string postRequestBody = postRequest.body; + Utilities::decodeString(postRequestBody); + string aliasName; + vector splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&'); + for (const string& item : splitPostRequestBody) { + vector splitItem = Utilities::splitStringIntoVector(item, '='); + if (splitItem.at(0) == "alias") + aliasName = splitItem.at(1); + } + Utilities::deleteFreelancerAlias(configuration, aliasName, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL)); + ctx["freelanceremail"] = cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL); + ctx[MUSTACHE_COOKIE_LOGGED_IN] = true; + } + auto page = crow::mustache::load(TEMPLATE_FREELANCER_ALIAS_MANAGEMENT_DELETE); + return page.render(ctx); + }); + diff --git a/src/templateConstCollection.cpp b/src/templateConstCollection.cpp index 347cc46..a89f9df 100644 --- a/src/templateConstCollection.cpp +++ b/src/templateConstCollection.cpp @@ -30,6 +30,7 @@ namespace TemplateConstCollection { const static std::string TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_CREATE_NEW = "freelancer_Template_Management_Create_New.html"; const static std::string TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_CREATE_NEW_FULFILMENT = "freelancer_Template_Management_Create_New_Fulfilment.html"; const static std::string TEMPLATE_FREELANCER_ALIAS_MANAGEMENT = "freelancer_Alias_Management.html"; + const static std::string TEMPLATE_FREELANCER_ALIAS_MANAGEMENT_DELETE = "freelancer_Alias_Management_Delete.html"; //Mustache Error/Success variable names const static std::string MUSTACHE_ERROR_COMMISSIONS_CLOSED = "ERROR_COMMISSIONS_CLOSED"; diff --git a/src/utilities.cpp b/src/utilities.cpp index f82ec11..3645c54 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -536,6 +536,15 @@ namespace Utilities { return resultJsonFreelancerAlias; } + /* + * Deletes a freelancers alias + * takes config the alias name and the freelancers email + */ + void deleteFreelancerAlias(const Utilities::config& configuration, const std::string& aliasName, const std::string& emailAddress) { + pqxx::connection databaseConnection(configuration.databaseConnectionString); + Database::prepareStatement(databaseConnection, ID_DELETE_FREELANCER_ALIAS); + Database::executePreparedStatement_DELETE_FREELANCER_ALIAS(databaseConnection, aliasName, emailAddress); + } /* * Checks if a given string is a valid number */ diff --git a/templates/freelancer_Alias_Management_Delete.html b/templates/freelancer_Alias_Management_Delete.html new file mode 100644 index 0000000..011dbe7 --- /dev/null +++ b/templates/freelancer_Alias_Management_Delete.html @@ -0,0 +1,28 @@ + + + + {{> templateIncludes/style.css.html}} + + + {{^COOKIE_LOGGED_IN}} + Please Log in. + {{/COOKIE_LOGGED_IN}} + {{#COOKIE_LOGGED_IN}} + Operation concluded: +
+
+ +
+
+ +
+ {{/COOKIE_LOGGED_IN}} + {{> templateIncludes/freelancerLoginSignupProfileLogoutInterface.html.html}} +
+ {{> templateIncludes/returnToIndexButton.html.html}} + + + + + + \ No newline at end of file