implementation of the edit function for the template managment

+minor typo fixes
This commit is contained in:
Tina_Azure 2023-05-17 18:39:32 +02:00
parent d9f3fd711c
commit 1620967c45
3 changed files with 37 additions and 9 deletions

View File

@ -308,7 +308,7 @@ namespace Database {
/*
* Executes the prepared statement SELECT_CHECK_EMAIL_EXISTS
* Takes an open pqxx::connection and the emailAddress to check
* Delivers count of emailaddress occurence 0 for none 1+ for more
* Delivers count of emailaddress occurrence 0 for none 1+ for more
*/
pqxx::result executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(pqxx::connection &connection, const std::string& freelancerEmail) {
pqxx::work work(connection);
@ -320,7 +320,7 @@ namespace Database {
/*
* Executes the prepared statement SELECT_CHECK_FREELANCER_LOGIN_STATE
* Takes an open pqxx::connection the loginKey and the id to check
* Delivers count of loginValidationKey occurence 0 for none 1+ for more
* Delivers count of loginValidationKey occurrence 0 for none 1+ for more
*/
pqxx::result executePreparedStatement_SELECT_CHECK_FREELANCER_LOGIN_STATE(pqxx::connection &connection, const std::string& freelancerEmail, const std::string& loginKey) {
pqxx::work work(connection);
@ -575,6 +575,18 @@ namespace Database {
return 0;
}
/*
* Executes the prepared statement UPDATE_EDIT_FREELANCER_TEMPLATE
* Takes an open pqxx::connection name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, templateid and the freelancers email
* returns true if update occured
*/
bool executePreparedStatement_UPDATE_EDIT_FREELANCER_TEMPLATE(pqxx::connection &connection, const std::string& name, const std::string& content, const std::string& contactdata, const std::string& contactinformation, const std::string& currencypreference, const std::string& priceupfront, const std::string& priceondeliver, int templateid, const std::string& emailAddress) {
pqxx::work work(connection);
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, templateid, emailAddress);
work.commit();
return result.affected_rows() != 0;
}
/*
* Executes the prepared statement DELETE_FREELANCER_TEMPLATE
* Deletes a templated based on the id and validated with the freelancer email

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 update the template of a freelancer
*/
const static std::string PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE = "updateEeditFreelancerTemplate";
const static std::string SQL_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE = "UPDATE templates SET (name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver) = ($1, $2, $3, $4, $5, $6, $7) WHERE id = $8 and freelancerid = (select freelancers.id from freelancers where emailaddress = $9);";
/*
* Name and Statement for prepared statement to delete a template with ownership validation
*/
@ -233,7 +239,7 @@ namespace DatabaseStatementConstCollection {
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;
const static int ID_UPDATE_EDIT_FREELANCER_TEMPLATE = 31;
/*
* Easy access to prepared statements via prepared statement name
@ -269,7 +275,8 @@ namespace DatabaseStatementConstCollection {
{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_DELETE_FREELANCER_TEMPLATE, SQL_STATEMENT_DELETE_FREELANCER_TEMPLATE}
{PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE, SQL_STATEMENT_DELETE_FREELANCER_TEMPLATE},
{PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE, SQL_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE}
};
/*
* Easy access to prepared statement name via int
@ -305,7 +312,8 @@ namespace DatabaseStatementConstCollection {
{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_DELETE_FREELANCER_TEMPLATE, PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE}
{ID_DELETE_FREELANCER_TEMPLATE, PREPARED_STATEMENT_DELETE_FREELANCER_TEMPLATE},
{ID_UPDATE_EDIT_FREELANCER_TEMPLATE, PREPARED_STATEMENT_UPDATE_EDIT_FREELANCER_TEMPLATE}
};
/*

View File

@ -808,9 +808,8 @@ int main(int argc, char *argv[]) {
}
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);
Database::prepareStatement(databaseConnection, ID_UPDATE_EDIT_FREELANCER_TEMPLATE);
for (const string& item : splitPostRequestBody) {
vector<string> splitItem = Utilities::splitStringIntoVector(item, '=');
if (splitItem.at(0) == "templatename")
@ -828,13 +827,22 @@ int main(int argc, char *argv[]) {
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";
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_COMPLETE] = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_EDIT] = true;
bool updateSuccess = Database::executePreparedStatement_UPDATE_EDIT_FREELANCER_TEMPLATE(databaseConnection, name, content, contactdata, contactinformation, currencypreference, priceupfront, priceondeliver, templateid, freelancerEmail);
if (updateSuccess) {
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_COMPLETE] = true;
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_EDIT] = true;
}
else {
ctx[MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_ERROR] = true;
}
} else if (operationDelete) {
Database::prepareStatement(databaseConnection, ID_DELETE_FREELANCER_TEMPLATE);
Database::executePreparedStatement_DELETE_FREELANCER_TEMPLATE(databaseConnection, templateid, freelancerEmail);