implement base template operations with the corresponding templates

fully implement the delete operation
todo:edit operation
This commit is contained in:
Tina_Azure
2023-05-15 19:32:21 +02:00
parent bbf526f99f
commit d9f3fd711c
6 changed files with 128 additions and 14 deletions

View File

@ -575,6 +575,17 @@ namespace Database {
return 0;
}
/*
* Executes the prepared statement DELETE_FREELANCER_TEMPLATE
* Deletes a templated based on the id and validated with the freelancer email
* Takes an open pqxx::connection the template id and the freelancers email
*/
void executePreparedStatement_DELETE_FREELANCER_TEMPLATE(pqxx::connection &connection, int templateID, const std::string& freelancerEmail) {
pqxx::work work(connection);
work.exec_prepared(PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE, templateID, freelancerEmail);
work.commit();
}
/*
* Prepares a statement based on ID
* Takes an open pqxx::connection, the statement id

View File

@ -193,6 +193,12 @@ namespace DatabaseStatementConstCollection {
const static std::string PREPARED_STATEMENT_INSERT_FREELANCER_TEMPLATE = "insertFreelancerTemplate";
const static std::string SQL_STATEMENT_INSERT_FREELANCER_TEMPLATE = "INSERT INTO templates(freelancerid, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver) VALUES((select freelancers.id from freelancers where emailaddress = $8), $1, $2, $3, $4, $5, $6, $7);";
/*
* Name and Statement for prepared statement to delete a template with ownership validation
*/
const static std::string PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE = "deleteFreelancerTemplate";
const static std::string SQL_STATEMENT_DELETE_FREELANCER_TEMPLATE = "delete from templates where id = $1 and freelancerid = (select freelancers.id from freelancers where emailaddress = $2);";
/*
* IDs of prepared statements
*/
@ -226,6 +232,8 @@ namespace DatabaseStatementConstCollection {
const static int ID_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS = 27;
const static int ID_UPDATE_EXPIRATION_LOGIN_LOCK_OUT = 28;
const static int ID_INSERT_FREELANCER_TEMPLATE = 29;
const static int ID_DELETE_FREELANCER_TEMPLATE = 30;
const static int ID_EDIT_FREELANCER_TEMPLATE = 31;
/*
* Easy access to prepared statements via prepared statement name
@ -260,7 +268,8 @@ namespace DatabaseStatementConstCollection {
{PREPARED_STATEMENT_INSERT_LOGIN_LOCK_OUT, SQL_STATEMENT_INSERT_LOGIN_LOCK_OUT},
{PREPARED_STATEMENT_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS, SQL_STATEMENT_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS},
{PREPARED_STATEMENT_UPDATE_EXPIRATION_LOGIN_LOCK_OUT, SQL_STATEMENT_UPDATE_EXPIRATION_LOGIN_LOCK_OUT},
{PREPARED_STATEMENT_INSERT_FREELANCER_TEMPLATE, SQL_STATEMENT_INSERT_FREELANCER_TEMPLATE}
{PREPARED_STATEMENT_INSERT_FREELANCER_TEMPLATE, SQL_STATEMENT_INSERT_FREELANCER_TEMPLATE},
{PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE, SQL_STATEMENT_DELETE_FREELANCER_TEMPLATE}
};
/*
* Easy access to prepared statement name via int
@ -295,7 +304,8 @@ namespace DatabaseStatementConstCollection {
{ID_INSERT_LOGIN_LOCK_OUT, PREPARED_STATEMENT_INSERT_LOGIN_LOCK_OUT},
{ID_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS, PREPARED_STATEMENT_SELECT_CHECK_LOGIN_LOCK_OUT_ATTEMPTS},
{ID_UPDATE_EXPIRATION_LOGIN_LOCK_OUT, PREPARED_STATEMENT_UPDATE_EXPIRATION_LOGIN_LOCK_OUT},
{ID_INSERT_FREELANCER_TEMPLATE, PREPARED_STATEMENT_INSERT_FREELANCER_TEMPLATE}
{ID_INSERT_FREELANCER_TEMPLATE, PREPARED_STATEMENT_INSERT_FREELANCER_TEMPLATE},
{ID_DELETE_FREELANCER_TEMPLATE, PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE}
};
/*

View File

@ -765,7 +765,7 @@ int main(int argc, char *argv[]) {
}
ctx[MUSTACHE_COOKIE_LOGGED_IN] = true;
}
auto page = crow::mustache::load(TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_FULFILMENT);
auto page = crow::mustache::load(TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_FULFILMENT_STAGE_CONFIRMATION);
return page.render(ctx);
});
@ -773,17 +773,77 @@ int main(int argc, char *argv[]) {
* Execute Template Operation
* todo:implement
*/
CROW_ROUTE(app, "/freelancer/templateManagement/fulfilment").methods("POST"_method)
([&, configuration](const crow::request& postRequest ) {
CROW_ROUTE(app, "/freelancer/templateManagement/fulfilment/<string>").methods("POST"_method)
([&, configuration](const crow::request& postRequest, string operation ) {
auto& cookieCtx = app.get_context<crow::CookieParser>(postRequest);
cout << postRequest.body << endl;
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;
bool operationEdit = false;
bool operationDelete = false;
bool error = false;
if (operation == MUSTACHE_FREELANCER_TEMPLATE_OPERATION_EDIT) {
operationEdit = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_EDIT] = true;
}
auto page = crow::mustache::load(TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT);
else if (operation == MUSTACHE_FREELANCER_TEMPLATE_OPERATION_DELETE) {
operationDelete = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_DELETE] = true;
}
else {
error = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_ERROR] = true;
}
if (!error) {
if (Utilities::checkCookieLoginState(configuration, cookieCtx)) {
ctx[MUSTACHE_COOKIE_LOGGED_IN] = true;
string freelancerEmail = cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL);
string postRequestBody = postRequest.body;
Utilities::decodeString(postRequestBody);
int templateid;
vector<string> splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&');
for (const string& item : splitPostRequestBody) {
vector<string> splitItem = Utilities::splitStringIntoVector(item, '=');
if (splitItem.at(0) == "templateid")
templateid = stoi(splitItem.at(1));
}
pqxx::connection databaseConnection(configuration.databaseConnectionString);
if (operationEdit) {
//todo:implement Edit
string name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver;
Database::prepareStatement(databaseConnection, ID_EDIT_FREELANCER_TEMPLATE);
for (const string& item : splitPostRequestBody) {
vector<string> splitItem = Utilities::splitStringIntoVector(item, '=');
if (splitItem.at(0) == "templatename")
name = splitItem.at(1);
if (splitItem.at(0) == "templatecontent")
content = splitItem.at(1);
if (splitItem.at(0) == "templatecontactdata")
contactdata = splitItem.at(1);
if (splitItem.at(0) == "templatecontactinformation")
contactinformation = splitItem.at(1);
if (splitItem.at(0) == "templatecurrencypreference")
currencypreference = splitItem.at(1);
if (splitItem.at(0) == "templatepriceupfront")
priceupfront = splitItem.at(1);
if (splitItem.at(0) == "templatepriceondeliver")
priceondeliver = splitItem.at(1);
}
if (!Utilities::checkIfStrIsNumber(priceupfront))
priceupfront = "0";
if (!Utilities::checkIfStrIsNumber(priceondeliver))
priceondeliver = "0";
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_COMPLETE] = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_EDIT] = true;
} else if (operationDelete) {
Database::prepareStatement(databaseConnection, ID_DELETE_FREELANCER_TEMPLATE);
Database::executePreparedStatement_DELETE_FREELANCER_TEMPLATE(databaseConnection, templateid, freelancerEmail);
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_COMPLETE] = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_DELETE] = true;
}
}
}
auto page = crow::mustache::load(TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_FULFILMENT_STAGE_EXECUTION);
return page.render(ctx);
});

View File

@ -25,7 +25,8 @@ namespace TemplateConstCollection {
const static std::string TEMPLATE_FREELANCER_REDIRECT_PROFILE = "freelancer_Redirect_Profile.html";
const static std::string TEMPLATE_FREELANCER_PROFILE = "freelancer_Profile.html";
const static std::string TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT = "freelancer_Template_Management.html";
const static std::string TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_FULFILMENT = "freelancer_Template_Management_Fulfilment.html";
const static std::string TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_FULFILMENT_STAGE_CONFIRMATION = "freelancer_Template_Management_Fulfilment_Stage_Confirmation.html";
const static std::string TEMPLATE_FREELANCER_TEMPLATE_MANAGEMENT_FULFILMENT_STAGE_EXECUTION = "freelancer_Template_Management_Fulfilment_Stage_Execution.html";
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";
@ -56,7 +57,9 @@ namespace TemplateConstCollection {
const static std::string MUSTACHE_RESET_SUCCESS = "RESET_SUCCESS";
const static std::string MUSTACHE_LOGIN_SUCCESS = "LOGIN_SUCCESS";
const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_ERROR_NO_TEMPLATE = "TEMPLATE_OPERATION_ERROR_NO_TEMPLATE";
const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_COMPLETE = "TEMPLATE_OPERATION_COMPLETE";
const static std::string MUSTACHE_FREELANCER_TEMPLATE_CREATION_ERROR = "TEMPLATE_CREATION_ERROR";
const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_ERROR = "OPERATION_ERROR";
//Mustache Cookie variable names
const static std::string MUSTACHE_COOKIE_LOGGED_IN = "COOKIE_LOGGED_IN";