implement freelancer alias creation
This commit is contained in:
85
src/main.cpp
85
src/main.cpp
@ -895,6 +895,91 @@ int main(int argc, char *argv[]) {
|
||||
return page.render(ctx);
|
||||
});
|
||||
|
||||
/*
|
||||
* Page for freelancer to create alias
|
||||
*/
|
||||
CROW_ROUTE(app, "/freelancer/aliasManagement/new")
|
||||
([&, configuration](const crow::request& getRequest ) {
|
||||
auto& cookieCtx = app.get_context<crow::CookieParser>(getRequest);
|
||||
crow::mustache::context ctx;
|
||||
if (Utilities::checkCookieLoginState(configuration, cookieCtx)) {
|
||||
ctx = Utilities::getFreelancerTemplates(configuration, 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_NEW);
|
||||
return page.render(ctx);
|
||||
});
|
||||
|
||||
/*
|
||||
* Execute alias creation
|
||||
*/
|
||||
CROW_ROUTE(app, "/freelancer/aliasManagement/new/fulfilment").methods("POST"_method)
|
||||
([&, configuration](const crow::request& postRequest) {
|
||||
auto& cookieCtx = app.get_context<crow::CookieParser>(postRequest);
|
||||
crow::mustache::context ctx;
|
||||
if (Utilities::checkCookieLoginState(configuration, cookieCtx)) {
|
||||
bool error = false;
|
||||
ctx[MUSTACHE_COOKIE_LOGGED_IN] = true;
|
||||
string postRequestBody = postRequest.body;
|
||||
Utilities::decodeString(postRequestBody);
|
||||
vector<string> splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&');
|
||||
string aliasname, templateNameID, templateName;
|
||||
for (const string& item : splitPostRequestBody) {
|
||||
vector<string> splitItem = Utilities::splitStringIntoVector(item, '=');
|
||||
if (splitItem.at(0) == "freelanceralias")
|
||||
aliasname = splitItem.at(1);
|
||||
if (splitItem.at(0) == "aliasroute")
|
||||
templateNameID = splitItem.at(1);
|
||||
}
|
||||
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatements(databaseConnection, {
|
||||
ID_SELECT_CHECK_FREELANCER_ALIAS,
|
||||
ID_INSERT_FREELANCER_ALIAS,
|
||||
ID_SELECT_FREELANCER_ID,
|
||||
ID_SELECT_FREELANCER_NAME
|
||||
});
|
||||
|
||||
bool aliasAlreadyUsed = false;
|
||||
if (aliasname.empty()) {
|
||||
error = true;
|
||||
ctx[MUSTACHE_FREELANCER_ALIAS_CREATION_ERROR] = true;
|
||||
ctx[MUSTACHE_FREELANCER_ALIAS_CREATION_ERROR_UNNAMED] = true;
|
||||
} else {
|
||||
ctx["aliasname"] = aliasname;
|
||||
aliasAlreadyUsed = Database::executePreparedStatement_SELECT_CHECK_FREELANCER_ALIAS(databaseConnection, aliasname);
|
||||
}
|
||||
if (aliasAlreadyUsed) {
|
||||
error = true;
|
||||
ctx[MUSTACHE_FREELANCER_ALIAS_CREATION_ERROR] = true;
|
||||
ctx[MUSTACHE_FREELANCER_ALIAS_CREATION_ERROR_DUPLICATE] = true;
|
||||
}
|
||||
if (!error) {
|
||||
string route, routeparameter, routevalue;
|
||||
pqxx::result freelancerNameResult = Database::executePreparedStatement_SELECT_FREELANCER_NAME(databaseConnection, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL));
|
||||
if (templateNameID.empty()) {
|
||||
routeparameter = "freelancerID";
|
||||
pqxx::result freelancerIDResult = Database::executePreparedStatement_SELECT_FREELANCER_ID(databaseConnection, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL));
|
||||
routevalue = freelancerIDResult.at(0).at(0).c_str();
|
||||
route = Utilities::generateAliasRouteFreelancer(freelancerNameResult.at(0).at(0).c_str());
|
||||
|
||||
} else {
|
||||
routeparameter = "templateID";
|
||||
vector<string> splitValues = Utilities::splitStringIntoVector(templateNameID, '|');
|
||||
routevalue = splitValues.at(1);
|
||||
route = Utilities::generateAliasRouteFreelancerTemplate(freelancerNameResult.at(0).at(0).c_str(), splitValues.at(0));
|
||||
}
|
||||
int errorLevel = Database::executePreparedStatement_INSERT_FREELANCER_ALIAS(databaseConnection, aliasname, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL), route, routeparameter, routevalue);
|
||||
ctx["aliasname"] = aliasname;
|
||||
if (errorLevel != 0)
|
||||
ctx[MUSTACHE_FREELANCER_ALIAS_CREATION_ERROR] = true;
|
||||
}
|
||||
}
|
||||
auto page = crow::mustache::load(TEMPLATE_FREELANCER_ALIAS_MANAGEMENT_NEW_FULFILMENT);
|
||||
return page.render(ctx);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user