Minor refactoring + move hardcoded template names and handling based mustache variables into a const collection for ease of modification/access

This commit is contained in:
Tina_Azure
2023-05-02 17:44:23 +02:00
parent a800fb07d4
commit 4720ffdad5
5 changed files with 112 additions and 62 deletions

View File

@ -15,7 +15,6 @@
using namespace DatabaseStatementConstCollection;
/*
* Database Manager
*/

View File

@ -10,8 +10,10 @@
#include "database.cpp"
#include "utilities.cpp"
#include "regularTaskExecution.cpp"
#include "templateConstCollection.cpp"
using namespace std;
using namespace TemplateConstCollection;
int main(int argc, char *argv[]) {
Utilities::config configuration;
@ -35,7 +37,7 @@ int main(int argc, char *argv[]) {
*/
CROW_ROUTE(app, "/").methods("POST"_method, "GET"_method)
([configuration]() {
auto page = crow::mustache::load("customerIndex_FreelancerListing.html");
auto page = crow::mustache::load(TEMPLATE_CUSTOMER_INDEX_FREELANCER_LISTING);
crow::mustache::context ctx(Utilities::getFreelancerListing(configuration));
return page.render(ctx);
});
@ -45,9 +47,8 @@ int main(int argc, char *argv[]) {
*/
CROW_ROUTE(app, "/@<string>")
([configuration](string alias) {
string redirectionHTML = "alias_Redirect.html";
crow::mustache::context ctx(getAlias(configuration, alias));
auto page = crow::mustache::load(redirectionHTML);
auto page = crow::mustache::load(TEMPLATE_ALIAS_REDIRECT);
return page.render(ctx);
});
@ -66,11 +67,11 @@ int main(int argc, char *argv[]) {
pqxx::result resultFreelancer = Database::executePreparedStatement_SELECT_FREELANCER(databaseConnection, freelancerID);
//error handling if invalid ID was delivered
string freelancerHTML = "customer_FreelancerListing_NOTFOUND.html";
string freelancerHTML = TEMPLATE_CUSTOMER_FREELANCER_LISTING_NOT_FOUND;
crow::json::wvalue resultJsonFreelancerTemplate;
if (!resultFreelancer.empty()){
freelancerHTML = "customer_FreelancerListing.html";
freelancerHTML = TEMPLATE_CUSTOMER_FREELANCER_LISTING;
pqxx::result resultFreelancerTemplates = Database::executePreparedStatement_SELECT_FREELANCER_TEMPLATES(databaseConnection, freelancerID);
resultJsonFreelancerTemplate = Database::convertResultToJSON(resultFreelancerTemplates, "templates");
}
@ -110,8 +111,7 @@ int main(int argc, char *argv[]) {
crow::json::wvalue resultJsonTemplate = Database::convertResultRowToJSON(resultTemplate);
string templateHTML = "customer_Freelancer_Template.html";
auto page = crow::mustache::load(templateHTML);
auto page = crow::mustache::load(TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE);
crow::mustache::context ctx(resultJsonTemplate);
@ -123,7 +123,7 @@ int main(int argc, char *argv[]) {
if (resultCommissionState.empty() || stoi(resultCommissionState.at(0).at(0).c_str()) == 1)
commissionState = true;
}
ctx["ERROR_COMMISSIONS_CLOSED"] = commissionState;
ctx[MUSTACHE_ERROR_COMMISSIONS_CLOSED] = commissionState;
return page.render(ctx);
});
@ -141,8 +141,7 @@ int main(int argc, char *argv[]) {
crow::json::wvalue resultJsonTemplate = Database::convertResultRowToJSON(resultTemplate);
string templateHTML = "customer_Freelancer_Template_Request.html";
auto page = crow::mustache::load(templateHTML);
auto page = crow::mustache::load(TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE_REQUEST);
crow::mustache::context ctx(resultJsonTemplate);
return page.render(ctx);
@ -154,7 +153,7 @@ int main(int argc, char *argv[]) {
CROW_ROUTE(app, "/customer/<string>/template/<string>/request/fulfilment").methods("POST"_method)
([configuration](const crow::request& postRequest, string freelancerName, string templateName ) {
pqxx::connection databaseConnection(configuration.databaseConnectionString);
string templateHTML = "customer_Freelancer_Template_Request_Fulfilment_ERROR.html";
string templateHTML = TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE_REQUEST_FULFILMENT_ERROR;
Database::requestsItem newRequest;
crow::mustache::context ctx;
@ -208,26 +207,26 @@ int main(int argc, char *argv[]) {
//the commissionstate
if (resultCommissionState.empty() || stoi(resultCommissionState.at(0).at(0).c_str()) == 1) {
ctx["ERROR_COMMISSIONS_CLOSED"] = true;
ctx[MUSTACHE_ERROR_COMMISSIONS_CLOSED] = true;
}
else {
int resultInsertOperation = Database::executePreparedStatement_INSERT_ITEM_IN_REQUESTS(databaseConnection, newRequest);
if (resultInsertOperation == 0) {
templateHTML = "customer_Freelancer_Template_Request_Fulfilment.html";
templateHTML = TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE_REQUEST_FULFILMENT;
pqxx::result resultEmailAddress = Database::executePreparedStatement_SELECT_FREELANCER_EMAIL(databaseConnection, newRequest.freelancerID);
if (!resultEmailAddress.empty())
Utilities::sendEmail(configuration, resultEmailAddress.at(0).at(0).c_str(), "NEW REQUEST", newRequest.toJSONString());
}
else {
ctx["ERROR_UNABLE_TO_CREATE_REQUEST"] = true;
ctx[MUSTACHE_ERROR_UNABLE_TO_CREATE_REQUEST] = true;
}
}
}
else {
if (requestFillCompletion)
ctx["ERROR_TEMPLATE_NOT_FOUND"] = true;
ctx[MUSTACHE_ERROR_TEMPLATE_NOT_FOUND] = true;
else
ctx["REQUEST_NOT_FILLED"] = true;
ctx[MUSTACHE_REQUEST_NOT_FILLED] = true;
}
auto page = crow::mustache::load(templateHTML);
@ -278,9 +277,9 @@ int main(int argc, char *argv[]) {
CROW_ROUTE(app, "/freelancer/login")
([&,configuration](const crow::request& getRequest ) {
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
string templateHTML = "freelancer_Login.html";
string templateHTML = TEMPLATE_FREELANCER_LOGIN;
if (Utilities::checkCookieLoginState(configuration, ctx))
templateHTML = "freelancer_Redirect_Profile.html";
templateHTML = TEMPLATE_FREELANCER_REDIRECT_PROFILE;
auto page = crow::mustache::load(templateHTML);
return page.render();
@ -291,7 +290,7 @@ int main(int argc, char *argv[]) {
*/
CROW_ROUTE(app, "/freelancer/login/passwordreset")
([]() {
auto page = crow::mustache::load("freelancer_Login_PasswordReset.html");
auto page = crow::mustache::load(TEMPLATE_FREELANCER_LOGIN_PASSWORD_RESET);
return page.render();
});
@ -336,7 +335,7 @@ int main(int argc, char *argv[]) {
}
ctx["resetemail"] = email;
}
auto page = crow::mustache::load("freelancer_Login_PasswordReset.html");
auto page = crow::mustache::load(TEMPLATE_FREELANCER_LOGIN_PASSWORD_RESET_FULFILMENT);
return page.render(ctx);
});
@ -354,9 +353,9 @@ int main(int argc, char *argv[]) {
ctx["passwordresetkey"] = passwordResetKey;
}
else {
ctx["PASSWORD_RESET_DOES_NOT_EXIST"] = true;
ctx[MUSTACHE_PASSWORD_RESET_DOES_NOT_EXIST] = true;
}
auto page = crow::mustache::load("passwordReset.html");
auto page = crow::mustache::load(TEMPLATE_PASSWORD_RESET);
return page.render(ctx);
});
@ -396,30 +395,30 @@ int main(int argc, char *argv[]) {
int errorLevel = Database::executePreparedStatement_UPDATE_FREELANCER_PASSWORD_HASH(databaseConnection, pwhash, pwsalt, email);
if (errorLevel == 0) {
ctx["RESET_SUCCESS"] = true;
ctx[MUSTACHE_RESET_SUCCESS] = true;
ctx["freelanceremail"] = email;
}
else {
ctx["RESET_ERROR"] = true;
ctx[MUSTACHE_RESET_ERROR] = true;
if (errorLevel == 1)
ctx["RESET_ERROR_QUERY"] = true;
ctx[MUSTACHE_RESET_ERROR_QUERY] = true;
if (errorLevel == 2)
ctx["RESET_ERROR_CRITICAL"] = true;
ctx[MUSTACHE_RESET_ERROR_CRITICAL] = true;
}
}
else {
ctx["PASSWORD_RESET_EXPIRED"] = true;
ctx[MUSTACHE_PASSWORD_RESET_EXPIRED] = true;
}
}
else {
if (password.empty())
ctx["PASSWORD_EMPTY"] = true;
ctx[MUSTACHE_PASSWORD_EMPTY] = true;
if (freelancerEmail.empty())
ctx["PASSWORD_RESET_DOES_NOT_EXIST"] = true;
ctx[MUSTACHE_PASSWORD_RESET_DOES_NOT_EXIST] = true;
if (password.compare(passwordConfirmation) != 0)
ctx["PASSWORD_RESET_PASS_CONFIRMATION"] = true;
ctx[MUSTACHE_PASSWORD_RESET_PASS_CONFIRMATION] = true;
}
auto page = crow::mustache::load("passwordReset_Fulfillment.html");
auto page = crow::mustache::load(TEMPLATE_PASSWORD_RESET_FULFILMENT);
return page.render(ctx);
});
@ -472,27 +471,24 @@ int main(int argc, char *argv[]) {
std::string freelancerEmailCookieValue = Utilities::generateSecureCookieFreelancerEmailValue(email, stayLoggedIn);
cookieCtx.set_cookie("loginKey", loginKeyCookieValue);
cookieCtx.set_cookie("freelancerEmail",freelancerEmailCookieValue);
ctx["LOGIN_SUCCESS"] = true;
ctx[MUSTACHE_LOGIN_SUCCESS] = true;
}
else {
ctx["LOGIN_ERROR"] = true;
ctx["LOGIN_ERROR_LOGIN_DATA_INVALID"] = true;
ctx[MUSTACHE_LOGIN_ERROR] = true;
ctx[MUSTACHE_LOGIN_ERROR_LOGIN_DATA_INVALID] = true;
}
}
else {
ctx["LOGIN_ERROR"] = true;
ctx["LOGIN_ERROR_LOGIN_DATA_INVALID"] = true;
ctx[MUSTACHE_LOGIN_ERROR] = true;
ctx[MUSTACHE_LOGIN_ERROR_LOGIN_DATA_INVALID] = true;
}
}
else {
ctx["LOGIN_ERROR"] = true;
ctx["LOGIN_ERROR_EMAIL_PASS_NOT_FILLED"] = true;
ctx[MUSTACHE_LOGIN_ERROR] = true;
ctx[MUSTACHE_LOGIN_ERROR_EMAIL_PASS_NOT_FILLED] = true;
}
string templateHTML = "freelancer_Login_Fulfilment.html";
auto page = crow::mustache::load(templateHTML);
auto page = crow::mustache::load(TEMPLATE_FREELANCER_LOGIN_FULFILMENT);
return page.render(ctx);
});
@ -504,10 +500,10 @@ int main(int argc, char *argv[]) {
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
string loginKey = ctx.get_cookie("loginKey");
string freelancerEmail = ctx.get_cookie("freelancerEmail");
string templateHTML = "freelancer_Signup.html";
string templateHTML = TEMPLATE_FREELANCER_SIGN_UP;
if (!freelancerEmail.empty() && !loginKey.empty()) {
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail))
templateHTML = "freelancer_Redirect_Profile.html";
templateHTML = TEMPLATE_FREELANCER_REDIRECT_PROFILE;
}
auto page = crow::mustache::load(templateHTML);
return page.render();
@ -561,35 +557,32 @@ int main(int argc, char *argv[]) {
int errorLevel = Database::executePreparedStatement_INSERT_NEW_FREELANCER(databaseConnection, email, name, pwsalt, pwhash);
if (errorLevel == 0) {
ctx["REGISTRATION_SUCCESS"] = true;
ctx[MUSTACHE_REGISTRATION_SUCCESS] = true;
ctx["freelancername"] = name;
ctx["freelanceremail"] = email;
}
else {
ctx["REGISTRATION_ERROR"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR] = true;
if (errorLevel == 1)
ctx["REGISTRATION_ERROR_QUERY"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR_QUERY] = true;
if (errorLevel == 2)
ctx["REGISTRATION_ERROR_CRITICAL"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR_CRITICAL] = true;
}
}
else {
ctx["REGISTRATION_ERROR"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR] = true;
ctx["freelanceremail"] = email;
ctx["REGISTRATION_ERROR_EMAIL_ALREADY_IN_USE"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR_EMAIL_ALREADY_IN_USE] = true;
}
}
else {
ctx["REGISTRATION_ERROR"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR] = true;
if (password.compare(passwordConfirmation) != 0)
ctx["REGISTRATION_ERROR_PASS_CONFIRMATION"] = true;
ctx["REGISTRATION_ERROR_EMAIL_NAME_PASS_NOT_FILLED"] = true;
ctx[MUSTACHE_REGISTRATION_ERROR_PASS_CONFIRMATION] = true;
ctx[MUSTACHE_REGISTRATION_ERROR_EMAIL_NAME_PASS_NOT_FILLED] = true;
}
string templateHTML = "freelancer_Signup_Fulfilment.html";
auto page = crow::mustache::load(templateHTML);
auto page = crow::mustache::load(TEMPLATE_FREELANCER_SIGN_UP_FULFILMENT);
return page.render(ctx);
});

View File

@ -0,0 +1,56 @@
#ifndef TEMPLATE_CONST_COLLECTION_CPP
#define TEMPLATE_CONST_COLLECTION_CPP
#include <string>
namespace TemplateConstCollection {
//Template Names
const static std::string TEMPLATE_ALIAS_REDIRECT = "alias_Redirect.html";
//Customer Templates
const static std::string TEMPLATE_CUSTOMER_INDEX_FREELANCER_LISTING = "customerIndex_FreelancerListing.html";
const static std::string TEMPLATE_CUSTOMER_FREELANCER_LISTING = "customer_FreelancerListing.html";
const static std::string TEMPLATE_CUSTOMER_FREELANCER_LISTING_NOT_FOUND = "customer_FreelancerListing_NOTFOUND.html";
const static std::string TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE = "customer_Freelancer_Template.html";
const static std::string TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE_REQUEST = "customer_Freelancer_Template_Request.html";
const static std::string TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE_REQUEST_FULFILMENT = "customer_Freelancer_Template_Request_Fulfilment.html";
const static std::string TEMPLATE_CUSTOMER_FREELANCER_TEMPLATE_REQUEST_FULFILMENT_ERROR = "customer_Freelancer_Template_Request_Fulfilment_ERROR.html";
//Freelancer Templates
const static std::string TEMPLATE_FREELANCER_SIGN_UP = "freelancer_Signup.html";
const static std::string TEMPLATE_FREELANCER_SIGN_UP_FULFILMENT = "freelancer_Signup_Fulfilment.html";
const static std::string TEMPLATE_FREELANCER_LOGIN = "freelancer_Login.html";
const static std::string TEMPLATE_FREELANCER_LOGIN_FULFILMENT = "freelancer_Login_Fulfilment.html";
const static std::string TEMPLATE_FREELANCER_LOGIN_PASSWORD_RESET = "freelancer_Login_PasswordReset.html";
const static std::string TEMPLATE_FREELANCER_LOGIN_PASSWORD_RESET_FULFILMENT = "freelancer_Login_PasswordReset_Fulfilment.html";
const static std::string TEMPLATE_PASSWORD_RESET = "passwordReset.html";
const static std::string TEMPLATE_PASSWORD_RESET_FULFILMENT = "passwordReset_Fulfilment.html";
const static std::string TEMPLATE_FREELANCER_REDIRECT_PROFILE = "freelancer_Redirect_Profile.html";
const static std::string TEMPLATE_FREELANCER_PROFILE = "freelancer_Profile.html";
//Mustache Error/Success variable names
const static std::string MUSTACHE_ERROR_COMMISSIONS_CLOSED = "ERROR_COMMISSIONS_CLOSED";
const static std::string MUSTACHE_ERROR_TEMPLATE_NOT_FOUND = "ERROR_TEMPLATE_NOT_FOUND";
const static std::string MUSTACHE_ERROR_UNABLE_TO_CREATE_REQUEST = "ERROR_UNABLE_TO_CREATE_REQUEST";
const static std::string MUSTACHE_REQUEST_NOT_FILLED = "REQUEST_NOT_FILLED";
const static std::string MUSTACHE_LOGIN_ERROR = "LOGIN_ERROR";
const static std::string MUSTACHE_LOGIN_ERROR_EMAIL_PASS_NOT_FILLED = "LOGIN_ERROR_EMAIL_PASS_NOT_FILLED";
const static std::string MUSTACHE_LOGIN_ERROR_LOGIN_DATA_INVALID = "LOGIN_ERROR_LOGIN_DATA_INVALID";
const static std::string MUSTACHE_PASSWORD_EMPTY = "PASSWORD_EMPTY";
const static std::string MUSTACHE_PASSWORD_RESET_DOES_NOT_EXIST = "PASSWORD_RESET_DOES_NOT_EXIST";
const static std::string MUSTACHE_PASSWORD_RESET_EXPIRED = "PASSWORD_RESET_EXPIRED";
const static std::string MUSTACHE_PASSWORD_RESET_PASS_CONFIRMATION = "PASSWORD_RESET_PASS_CONFIRMATION";
const static std::string MUSTACHE_REGISTRATION_ERROR = "REGISTRATION_ERROR";
const static std::string MUSTACHE_REGISTRATION_ERROR_CRITICAL = "REGISTRATION_ERROR_CRITICAL";
const static std::string MUSTACHE_REGISTRATION_ERROR_EMAIL_ALREADY_IN_USE = "REGISTRATION_ERROR_EMAIL_ALREADY_IN_USE";
const static std::string MUSTACHE_REGISTRATION_ERROR_EMAIL_NAME_PASS_NOT_FILLED = "REGISTRATION_ERROR_EMAIL_NAME_PASS_NOT_FILLED";
const static std::string MUSTACHE_REGISTRATION_ERROR_PASS_CONFIRMATION = "REGISTRATION_ERROR_PASS_CONFIRMATION";
const static std::string MUSTACHE_REGISTRATION_ERROR_QUERY = "REGISTRATION_ERROR_QUERY";
const static std::string MUSTACHE_RESET_ERROR = "RESET_ERROR";
const static std::string MUSTACHE_RESET_ERROR_CRITICAL = "RESET_ERROR_CRITICAL";
const static std::string MUSTACHE_RESET_ERROR_QUERY = "RESET_ERROR_QUERY";
const static std::string MUSTACHE_REGISTRATION_SUCCESS = "REGISTRATION_SUCCESS";
const static std::string MUSTACHE_RESET_SUCCESS = "RESET_SUCCESS";
const static std::string MUSTACHE_LOGIN_SUCCESS = "LOGIN_SUCCESS";
//Mustache Cookie variable names
const static std::string MUSTACHE_COOKIE_LOGGED_IN = "COOKIE_LOGGED_IN";
}
#endif

View File

@ -20,9 +20,11 @@
#include "cpp/htmlmessage.hpp"
#include "emailTemplateCollection.cpp"
#include "templateConstCollection.cpp"
using namespace jed_utils::cpp;
using namespace DatabaseStatementConstCollection;
using namespace TemplateConstCollection;
/*
* Utility Manager

View File

@ -1,13 +1,13 @@
<div>
{{^cookieloggedin}}
{{^COOKIE_LOGGED_IN}}
<form action="/freelancer/login" method="get">
<button type="submit" class="button">Freelancer Login</button>
</form>
<form action="/freelancer/signup" method="get">
<button type="submit" class="button">Freelancer Sign-Up</button>
</form>
{{/cookieloggedin}}
{{#cookieloggedin}}
{{/COOKIE_LOGGED_IN}}
{{#COOKIE_LOGGED_IN}}
<form action="/freelancer/profile" method="get">
<button type="submit" class="button">Profile</button>
</form>
@ -15,5 +15,5 @@
<form action="/freelancer/logout" method="get">
<button type="submit" class="button">Logout</button>
</form>
{{/cookieloggedin}}
{{/COOKIE_LOGGED_IN}}
</div>