From 1a1908a814aa99c75a23a2266be5060942e1d59d Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Wed, 5 Apr 2023 02:24:42 +0200 Subject: [PATCH] Redirection template Handle @alias via the redirection template --- src/database.cpp | 18 ++++++++++++++ src/main.cpp | 29 +++++++++++++++++++++- templates/alias_Redirect.html | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 templates/alias_Redirect.html diff --git a/src/database.cpp b/src/database.cpp index 783e4fb..2fc79dc 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -48,6 +48,12 @@ namespace Database { const static std::string PREPARED_STATEMENT_SELECT_FREELANCER_TEMPLATES = "selectFreelancerTemplates"; const static std::string SQL_STATEMENT_SELECT_FREELANCER_TEMPLATES = "select id, name, content from templates where freelancerid = $1 order by id;"; + /* + * Name and Statement for prepared statement to select an alias and its route via the alias name + */ + const static std::string PREPARED_STATEMENT_SELECT_ALIAS = "selectAlias"; + const static std::string SQL_STATEMENT_SELECT_ALIAS = "select aliasname, route, routeparameter, routevalue from aliasroutes where aliasname = $1;"; + /* * Selects freelancers, their basicInfo and if the commissions are closed/open ordered by name. * Delivers if the commission limit has been reached and if the commissions are closed based on the accepted/completed state of the freelancers requests @@ -180,6 +186,18 @@ namespace Database { 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, 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(); + return result; + } + /* diff --git a/src/main.cpp b/src/main.cpp index 968b4cb..47c393c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,7 @@ int main() { /* * Freelancer Profile listing for customers */ - CROW_ROUTE(app, "/").methods("POST"_method) + CROW_ROUTE(app, "/").methods("POST"_method, "GET"_method) ([databaseURI]() { pqxx::connection databaseConnection(databaseURI); pqxx::result result = Database::executeStatement_SELECT_FREELANCERS_WITHCOMMISSIONSSTATE(databaseConnection); @@ -37,6 +37,33 @@ int main() { return page.render(ctx); }); + /* + * Freelancer Profile listing for customers + */ + CROW_ROUTE(app, "/@") + ([databaseURI](string alias) { + pqxx::connection databaseConnection(databaseURI); + pqxx::result resultAlias = Database::executePreparedStatement_SELECT_ALIAS(databaseConnection, alias); + + crow::json::wvalue resultJsonAlias; + + string redirectionHTML = "alias_Redirect.html"; + + if (resultAlias.size() > 0) { + resultJsonAlias = Database::convertResultRowToJSON(resultAlias); + } + else + { + //If the alias does not exist redirect back to the index. + resultJsonAlias["route"] = "/"; + } + + auto page = crow::mustache::load(redirectionHTML); + crow::mustache::context ctx(resultJsonAlias); + + return page.render(ctx); + }); + /* * Freelancer Profile Page for customers */ diff --git a/templates/alias_Redirect.html b/templates/alias_Redirect.html new file mode 100644 index 0000000..e30a87c --- /dev/null +++ b/templates/alias_Redirect.html @@ -0,0 +1,45 @@ + + + + + + +

Redirecting:

+ +
+ +
+
+ +
+ + + + + + \ No newline at end of file