Optimize template edit/creation requestbody parsing
This commit is contained in:
85
src/main.cpp
85
src/main.cpp
@ -676,49 +676,27 @@ int main(int argc, char *argv[]) {
|
||||
crow::mustache::context ctx;
|
||||
if (Utilities::checkCookieLoginState(configuration, cookieCtx)) {
|
||||
ctx[MUSTACHE_COOKIE_LOGGED_IN] = true;
|
||||
string name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver;
|
||||
|
||||
Utilities::templateItem newTemplate;
|
||||
|
||||
string postRequestBody = postRequest.body;
|
||||
Utilities::decodeString(postRequestBody);
|
||||
vector<string> splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&');
|
||||
|
||||
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 (name.empty())
|
||||
name = "unnamed";
|
||||
if (!Utilities::checkIfStrIsNumber(priceupfront))
|
||||
priceupfront = "0";
|
||||
if (!Utilities::checkIfStrIsNumber(priceondeliver))
|
||||
priceondeliver = "0";
|
||||
|
||||
newTemplate.parseRequestBodyIntoItem(postRequestBody);
|
||||
newTemplate.outputItem();
|
||||
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
Database::prepareStatement(databaseConnection, ID_INSERT_FREELANCER_TEMPLATE);
|
||||
int errorLevel = Database::executePreparedStatement_INSERT_FREELANCER_TEMPLATE(
|
||||
databaseConnection, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL));
|
||||
databaseConnection, newTemplate.name, newTemplate.content, newTemplate.contactdata, newTemplate.contactinformation,
|
||||
newTemplate.currencypreference, newTemplate.priceupfront, newTemplate.priceondeliver, cookieCtx.get_cookie(COOKIE_FREELANCER_EMAIL));
|
||||
if (errorLevel == 0) {
|
||||
ctx["templatename"] = name;
|
||||
ctx["contactdata"] = contactdata;
|
||||
ctx["contactinformation"] = contactinformation;
|
||||
ctx["currencypreference"] = currencypreference;
|
||||
ctx["priceupfront"] = priceupfront;
|
||||
ctx["priceondeliver"] = priceondeliver;
|
||||
ctx["content"] = content;
|
||||
ctx["templatename"] = newTemplate.name;
|
||||
ctx["contactdata"] = newTemplate.contactdata;
|
||||
ctx["contactinformation"] = newTemplate.contactinformation;
|
||||
ctx["currencypreference"] = newTemplate.currencypreference;
|
||||
ctx["priceupfront"] = newTemplate.priceupfront;
|
||||
ctx["priceondeliver"] = newTemplate.priceondeliver;
|
||||
ctx["content"] = newTemplate.content;
|
||||
}
|
||||
else {
|
||||
ctx[MUSTACHE_FREELANCER_TEMPLATE_CREATION_ERROR] = true;
|
||||
@ -802,38 +780,21 @@ int main(int argc, char *argv[]) {
|
||||
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));
|
||||
if (splitItem[0] == "templateid") {
|
||||
templateid = stoi(splitItem[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
if (operationEdit) {
|
||||
string name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver;
|
||||
Utilities::templateItem toEditTemplate;
|
||||
Database::prepareStatement(databaseConnection, ID_UPDATE_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 (name.empty())
|
||||
name = "unnamed";
|
||||
if (!Utilities::checkIfStrIsNumber(priceupfront))
|
||||
priceupfront = "0";
|
||||
if (!Utilities::checkIfStrIsNumber(priceondeliver))
|
||||
priceondeliver = "0";
|
||||
toEditTemplate.parseRequestBodyIntoItem(postRequestBody);
|
||||
toEditTemplate.outputItem();
|
||||
|
||||
bool updateSuccess = Database::executePreparedStatement_UPDATE_EDIT_FREELANCER_TEMPLATE(databaseConnection, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, templateid, freelancerEmail);
|
||||
bool updateSuccess = Database::executePreparedStatement_UPDATE_EDIT_FREELANCER_TEMPLATE(
|
||||
databaseConnection, toEditTemplate.name, toEditTemplate.content, toEditTemplate.contactdata, toEditTemplate.contactinformation,
|
||||
toEditTemplate.currencypreference, toEditTemplate.priceupfront, toEditTemplate.priceondeliver, templateid, freelancerEmail);
|
||||
|
||||
if (updateSuccess) {
|
||||
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_COMPLETE] = true;
|
||||
|
Reference in New Issue
Block a user