Optimize template edit/creation requestbody parsing
This commit is contained in:
parent
549c98f673
commit
9e5f0a68ab
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;
|
||||
|
|
|
@ -59,6 +59,19 @@ namespace Utilities {
|
|||
return stringToTrim;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks if a given string is a valid number
|
||||
*/
|
||||
bool checkIfStrIsNumber(const std::string& numberString) {
|
||||
try {
|
||||
std::stod(numberString);
|
||||
}
|
||||
catch (const std::invalid_argument& ia) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Struct representing the configuration file
|
||||
*/
|
||||
|
@ -227,6 +240,54 @@ namespace Utilities {
|
|||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Struct representing a freelancers template
|
||||
*/
|
||||
struct templateItem {
|
||||
std::string name;
|
||||
std::string content;
|
||||
std::string contactdata;
|
||||
std::string contactinformation;
|
||||
std::string currencypreference;
|
||||
std::string priceupfront;
|
||||
std::string priceondeliver;
|
||||
|
||||
/*
|
||||
* Parses a decoded request body string and fills the struct
|
||||
*/
|
||||
void parseRequestBodyIntoItem(const std::string& requestBody) {
|
||||
std::vector<std::string> splitPostRequestBody = Utilities::splitStringIntoVector(requestBody, '&');
|
||||
for (const std::string& item : splitPostRequestBody) {
|
||||
std::vector<std::string> splitItem = Utilities::splitStringIntoVector(item, '=');
|
||||
if (splitItem[0] == "templatename")
|
||||
name = splitItem[1];
|
||||
if (splitItem[0] == "templatecontent")
|
||||
content = splitItem[1];
|
||||
if (splitItem[0] == "templatecontactdata")
|
||||
contactdata = splitItem[1];
|
||||
if (splitItem[0] == "templatecontactinformation")
|
||||
contactinformation = splitItem[1];
|
||||
if (splitItem[0] == "templatecurrencypreference")
|
||||
currencypreference = splitItem[1];
|
||||
if (splitItem[0] == "templatepriceupfront")
|
||||
priceupfront = splitItem[1];
|
||||
if (splitItem[0] == "templatepriceondeliver")
|
||||
priceondeliver = splitItem[1];
|
||||
}
|
||||
if (name.empty())
|
||||
name = "unnamed";
|
||||
if (!checkIfStrIsNumber(priceupfront))
|
||||
priceupfront = "0";
|
||||
if (!checkIfStrIsNumber(priceondeliver))
|
||||
priceondeliver = "0";
|
||||
}
|
||||
|
||||
void outputItem() {
|
||||
std::cout << name << " " << content << " " << contactdata << " " << contactinformation << " " << currencypreference
|
||||
<< " " << priceupfront << "-" << priceondeliver << " " << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Takes String and cuts from the start-up to the length of valueName
|
||||
*/
|
||||
|
@ -567,18 +628,5 @@ namespace Utilities {
|
|||
std::string generateAliasRouteFreelancerTemplate(const std::string& freelancerName, const std::string& templateName) {
|
||||
return "/customer/" + freelancerName + "/template/" + templateName;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks if a given string is a valid number
|
||||
*/
|
||||
bool checkIfStrIsNumber(const std::string& numberString) {
|
||||
try {
|
||||
std::stod(numberString);
|
||||
}
|
||||
catch (const std::invalid_argument& ia) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue