Modification of requestsItem

SELECT_TEMPLATE_FLAT to get base template data for request creation
This commit is contained in:
Tina_Azure
2023-04-07 22:32:01 +02:00
parent 88167c4f95
commit 0272e0b719

View File

@ -11,13 +11,12 @@
*/
namespace Database {
//Valid names for the JSON
const static std::string JSON_ITEM_NAMES[] = {"id", "customerEmailAddress", "freelancer", "templateName", "currencyPreference", "priceUpFront", "priceOnDeliver", "requestDescription", "accepted", "upFrontInvoiceID", "onDeliverInvoiceID", "upFrontPaid", "onDeliverPaid"};
const static std::string JSON_ITEM_NAMES[] ={"id", "customerName", "customerEmailAddress", "customerContactDetails", "freelancerID", "templateID", "currencyPreference", "priceUpFront", "priceOnDeliver", "requestDescription", "accepted", "upFrontInvoiceID", "onDeliverInvoiceID", "upFrontPaid", "onDeliverPaid", "completed"};
/*
* Name and Statement for prepared statement to insert an item into Requests
* Takes the name of the freelancer and the template and resolves them to create the request
* todo:validate
* todo:createAlternative
* todo:redo
*/
const static std::string PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS = "insertItemInRequests";
const static std::string SQL_STATEMENT_INSERT_ITEM_IN_REQUESTS = "INSERT INTO requests(id, customerEmailAddress, freelancerid , templateid , currencyPreference, priceUpFront, priceOnDeliver, requestDescription, accepted, upFrontInvoiceID, onDeliverInvoiceID, upFrontPaid, onDeliverPaid) VALUES(DEFAULT, $1, (select id from freelancers where name = $2), (select id from templates where name = $3), $4, $5, $6, $7, $8, $9, $10, $11, $12 );";
@ -37,11 +36,17 @@ namespace Database {
const static std::string SQL_STATEMENT_SELECT_FREELANCER = "select id, name, generalinformation, basicinformation from freelancers WHERE id=$1;";
/*
* Name and Statement for prepared statement to select a freelancer based on a given id
* Name and Statement for prepared statement to select a freelancer based on a given id for presentation to the customer
*/
const static std::string PREPARED_STATEMENT_SELECT_TEMPLATE = "selectTemplate";
const static std::string SQL_STATEMENT_SELECT_TEMPLATE = "select templates.id as templateid, templates.name as templatename, freelancers.id as freelancerid, freelancers.name as freelancername, contactdata, contactinformation, currencypreference, (coalesce(priceupfront ,0) + coalesce(priceondeliver ,0)) as pricetotal, priceupfront, priceondeliver, content from templates inner join freelancers on freelancers.id=templates.freelancerid where templates.id = $1;";
/*
* Name and Statement for prepared statement to select a freelancer based on a given id for usage in a request
*/
const static std::string PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT = "selectTemplateFlat";
const static std::string SQL_STATEMENT_SELECT_TEMPLATE_FLAT = "select freelancerid, currencypreference, priceupfront, priceondeliver from templates where id = $1;";
/*
* Name and Statement for prepared statement to select a freelancers templates based on a given freelancer id
*/
@ -64,39 +69,62 @@ namespace Database {
* Struct Representing an item in Requests
*/
struct requestsItem {
int id;
std::string customerEmailAddress;
std::string freelancer;
std::string freelancerid;
std::string templateName;
std::string templateNameid;
std::string currencyPreference;
std::string priceUpFront;
std::string priceOnDeliver;
std::string requestDescription;
bool accepted;
std::string upFrontInvoiceID;
std::string onDeliverInvoiceID;
bool upFrontPaid;
bool onDeliverPaid;
int id = 0;
std::string customerName = "";
std::string customerEmailAddress = "";
std::string customerContactDetails = "";
int freelancerID = 0;
int templateID = 0;
std::string currencyPreference = "";
std::string priceUpFront = "";
std::string priceOnDeliver = "";
std::string requestDescription = "";
bool accepted = false;
std::string upFrontInvoiceID = "";
std::string onDeliverInvoiceID = "";
bool upFrontPaid = false;
bool onDeliverPaid = false;
bool completed = false;
void outputItem(){
std::cout << "id = " << id << std::endl;
std::cout << "customerName = " << customerName << std::endl;
std::cout << "customerEmailAddress = " << customerEmailAddress << std::endl;
std::cout << "customerContactDetails = " << customerContactDetails << std::endl;
std::cout << "freelancerID = " << freelancerID << std::endl;
std::cout << "templateID = " << templateID << std::endl;
std::cout << "currencyPreference = " << currencyPreference << std::endl;
std::cout << "priceUpFront = " << priceUpFront << std::endl;
std::cout << "priceOnDeliver = " << priceOnDeliver << std::endl;
std::cout << "requestDescription = " << requestDescription << std::endl;
std::cout << "accepted = " << accepted << std::endl;
std::cout << "InvoiceID = " << upFrontInvoiceID << std::endl;
std::cout << "onDeliverInvoiceID = " << onDeliverInvoiceID << std::endl;
std::cout << "upFrontPaid = " << upFrontPaid << std::endl;
std::cout << "onDeliverPaid = " << onDeliverPaid << std::endl;
std::cout << "completed = " << completed << std::endl;
}
/*
* Parses a JSON and fills the Item with the delivered values
*/
void insertJsonIntoItem(crow::json::rvalue itemJson){
if (itemJson.has(JSON_ITEM_NAMES[0])) id = itemJson[JSON_ITEM_NAMES[0]].i();
if (itemJson.has(JSON_ITEM_NAMES[1])) customerEmailAddress = itemJson[JSON_ITEM_NAMES[1]].s();
if (itemJson.has(JSON_ITEM_NAMES[2])) freelancer = itemJson[JSON_ITEM_NAMES[2]].s();
if (itemJson.has(JSON_ITEM_NAMES[3])) templateName = itemJson[JSON_ITEM_NAMES[3]].s();
if (itemJson.has(JSON_ITEM_NAMES[4])) currencyPreference = itemJson[JSON_ITEM_NAMES[4]].s();
if (itemJson.has(JSON_ITEM_NAMES[5])) priceUpFront = itemJson[JSON_ITEM_NAMES[5]].s();
if (itemJson.has(JSON_ITEM_NAMES[6])) priceOnDeliver = itemJson[JSON_ITEM_NAMES[6]].s();
if (itemJson.has(JSON_ITEM_NAMES[7])) requestDescription = itemJson[JSON_ITEM_NAMES[7]].s();
if (itemJson.has(JSON_ITEM_NAMES[8])) accepted = itemJson[JSON_ITEM_NAMES[8]].b();
if (itemJson.has(JSON_ITEM_NAMES[9])) upFrontInvoiceID = itemJson[JSON_ITEM_NAMES[9]].s();
if (itemJson.has(JSON_ITEM_NAMES[10])) onDeliverInvoiceID = itemJson[JSON_ITEM_NAMES[10]].s();
if (itemJson.has(JSON_ITEM_NAMES[11])) upFrontPaid = itemJson[JSON_ITEM_NAMES[11]].b();
if (itemJson.has(JSON_ITEM_NAMES[12])) onDeliverPaid = itemJson[JSON_ITEM_NAMES[12]].b();
if (itemJson.has(JSON_ITEM_NAMES[1])) customerName = itemJson[JSON_ITEM_NAMES[1]].s();
if (itemJson.has(JSON_ITEM_NAMES[2])) customerEmailAddress = itemJson[JSON_ITEM_NAMES[2]].s();
if (itemJson.has(JSON_ITEM_NAMES[3])) customerContactDetails = itemJson[JSON_ITEM_NAMES[3]].s();
if (itemJson.has(JSON_ITEM_NAMES[4])) freelancerID = itemJson[JSON_ITEM_NAMES[4]].i();
if (itemJson.has(JSON_ITEM_NAMES[5])) templateID = itemJson[JSON_ITEM_NAMES[5]].i();
if (itemJson.has(JSON_ITEM_NAMES[6])) currencyPreference = itemJson[JSON_ITEM_NAMES[6]].s();
if (itemJson.has(JSON_ITEM_NAMES[7])) priceUpFront = itemJson[JSON_ITEM_NAMES[7]].s();
if (itemJson.has(JSON_ITEM_NAMES[8])) priceOnDeliver = itemJson[JSON_ITEM_NAMES[8]].s();
if (itemJson.has(JSON_ITEM_NAMES[9])) requestDescription = itemJson[JSON_ITEM_NAMES[9]].s();
if (itemJson.has(JSON_ITEM_NAMES[10])) accepted = itemJson[JSON_ITEM_NAMES[10]].b();
if (itemJson.has(JSON_ITEM_NAMES[11])) upFrontInvoiceID = itemJson[JSON_ITEM_NAMES[11]].s();
if (itemJson.has(JSON_ITEM_NAMES[12])) onDeliverInvoiceID = itemJson[JSON_ITEM_NAMES[12]].s();
if (itemJson.has(JSON_ITEM_NAMES[13])) upFrontPaid = itemJson[JSON_ITEM_NAMES[13]].b();
if (itemJson.has(JSON_ITEM_NAMES[14])) onDeliverPaid = itemJson[JSON_ITEM_NAMES[14]].b();
if (itemJson.has(JSON_ITEM_NAMES[15])) completed = itemJson[JSON_ITEM_NAMES[15]].b();
}
};
@ -114,12 +142,13 @@ namespace Database {
/*
* Executes the prepared statement INSERT_ITEM_IN_REQUESTS
* Takes an open pqxx::connection and the requestsItem to insert
* todo:redo
*/
pqxx::result executePreparedStatement_INSERT_ITEM_IN_REQUESTS(pqxx::connection &connection, requestsItem item) {
connection.prepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, SQL_STATEMENT_INSERT_ITEM_IN_REQUESTS);
pqxx::work work(connection);
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS,
item.customerEmailAddress, item.freelancer, item.templateName,
item.customerEmailAddress, item.freelancerID, item.templateID,
item.currencyPreference, item.priceUpFront, item.priceOnDeliver,
item.requestDescription, item.accepted, item.upFrontInvoiceID,
item.onDeliverInvoiceID, item.upFrontPaid, item.onDeliverPaid);
@ -174,6 +203,18 @@ namespace Database {
return result;
}
/*
* Executes the prepared statement SELECT_TEMPLATE_FLAT
* Takes an open pqxx::connection and the id to select by
*/
pqxx::result executePreparedStatement_SELECT_TEMPLATE_FLAT(pqxx::connection &connection, int templateID) {
connection.prepare(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT, SQL_STATEMENT_SELECT_TEMPLATE_FLAT);
pqxx::work work(connection);
pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_TEMPLATE_FLAT, templateID);
work.commit();
return result;
}
/*
* Executes the prepared statement SELECT_FREELANCER_TEMPLATES
* Takes an open pqxx::connection and the id to select by
@ -198,8 +239,6 @@ namespace Database {
return result;
}
/*
* parses the result and returns a JSON
* Takes a result and optionally the desired row