From 854d556c00e82395547ea65b26b1330902c2b817 Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Mon, 10 Apr 2023 19:32:37 +0200 Subject: [PATCH] Prepared Statments: Commission State Freelancer Email Insert Item in Requests Item struct: Dump data as a JSON String Statment execution function: redone inserted values based on modified requests item error handling for insertion basic functions for the selection of email and commission state --- src/database.cpp | 104 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 14 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index efb8d20..6662f76 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -1,8 +1,10 @@ #include +#include #include #include #include #include +#include #include "crow.h" @@ -15,11 +17,9 @@ namespace Database { /* * 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: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 );"; + 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, completed, customerContactDetails, customerName) VALUES(DEFAULT, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15);"; /* * Name and Statement for prepared statement to select an item based on a given ID @@ -29,6 +29,20 @@ namespace Database { const static std::string PREPARED_STATEMENT_SELECT_ITEM_BY_ID = "selectItemByID"; const static std::string SQL_STATEMENT_SELECT_ITEM_BY_ID = "SELECT requests.id , customerEmailAddress, freelancers.name, templates.name, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, (CASE WHEN requests.accepted THEN 1 ELSE 0 END) AS accepted, upFrontInvoiceID, onDeliverInvoiceID, (CASE WHEN requests.upFrontPaid THEN 1 ELSE 0 END) AS upFrontPaid, (CASE WHEN requests.onDeliverPaid THEN 1 ELSE 0 END) AS onDeliverPaid FROM requests INNER JOIN freelancers ON freelancers.id=requests.freelancerID INNER JOIN templates ON templates.id=requests.templateID WHERE requests.id=$1;"; + /* + * Name and Statement for prepared statement to select a freelancers commission state based on a given id + * 0 == commissions are open + * 1 == commissions are closed + */ + const static std::string PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE = "selectFreelancerCommissionState"; + const static std::string SQL_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE = "select (case when (commissionlimit <= (select count(*) as requestCount from requests where requests.accepted = true and requests.freelancerid = id group by freelancerid)) then 1 else 0 end) as commissionsclosed from freelancers where freelancers.id = $1;"; + + /* + * Name and Statement for prepared statement to select a freelancers emailaddress based on a given id + */ + const static std::string PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL = "selectFreelancerEmailAddress"; + const static std::string SQL_STATEMENT_SELECT_FREELANCER_EMAIL = "select emailaddress from freelancers where id = $1;"; + /* * Name and Statement for prepared statement to select a freelancer based on a given id */ @@ -86,6 +100,28 @@ namespace Database { bool onDeliverPaid = false; bool completed = false; + std::string toJSONString(){ + std::string outputString = "{\"requestItem:\":{"; + outputString += "\"id\": " + std::to_string(id) + ","; + outputString += "\"customerName\": \"" + customerName + "\","; + outputString += "\"customerEmailAddress\": \"" + customerEmailAddress + "\","; + outputString += "\"customerContactDetails\": \"" + customerContactDetails + "\","; + outputString += "\"freelancerID\": " + std::to_string(freelancerID) + ","; + outputString += "\"templateID\": " + std::to_string(templateID) + ","; + outputString += "\"currencyPreference\": \"" + currencyPreference + "\","; + outputString += "\"priceUpFront\": \"" + priceUpFront + "\","; + outputString += "\"priceOnDeliver\": \"" + priceOnDeliver + "\","; + outputString += "\"requestDescription\": \"" + requestDescription + "\","; + outputString += "\"accepted\": " + std::to_string(accepted) + ","; + outputString += "\"invoiceID\": \"" + upFrontInvoiceID + "\","; + outputString += "\"onDeliverInvoiceID\": \"" + onDeliverInvoiceID + "\","; + outputString += "\"upFrontPaid\": " + std::to_string(upFrontPaid) + ","; + outputString += "\"onDeliverPaid\": " + std::to_string(onDeliverPaid) + ","; + outputString += "\"completed\": " + std::to_string(completed); + outputString += "}}"; + return outputString; + } + void outputItem(){ std::cout << "id = " << id << std::endl; std::cout << "customerName = " << customerName << std::endl; @@ -142,18 +178,34 @@ namespace Database { /* * Executes the prepared statement INSERT_ITEM_IN_REQUESTS * Takes an open pqxx::connection and the requestsItem to insert - * todo:redo + * returns errorLevel + * 0 = no error + * 1 = query error + * 2 = critical error */ - 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.freelancerID, item.templateID, - item.currencyPreference, item.priceUpFront, item.priceOnDeliver, - item.requestDescription, item.accepted, item.upFrontInvoiceID, - item.onDeliverInvoiceID, item.upFrontPaid, item.onDeliverPaid); - work.commit(); - return result; + int executePreparedStatement_INSERT_ITEM_IN_REQUESTS(pqxx::connection &connection, requestsItem item) { + try { + connection.prepare(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, SQL_STATEMENT_INSERT_ITEM_IN_REQUESTS); + pqxx::work work(connection); + work.exec_prepared(PREPARED_STATEMENT_INSERT_ITEM_IN_REQUESTS, + item.customerEmailAddress, item.freelancerID, item.templateID, + item.currencyPreference, item.priceUpFront, item.priceOnDeliver, + item.requestDescription, item.accepted, item.upFrontInvoiceID, + item.onDeliverInvoiceID, item.upFrontPaid, item.onDeliverPaid, + item.completed, item.customerContactDetails, item.customerName); + work.commit(); + } + catch (pqxx::sql_error const &e) { + std::cerr + << "Database error: " << e.what() << std::endl + << "Query was: " << e.query() << std::endl; + return 1; + } + catch (std::exception const &e) { + std::cerr << e.what() << std::endl; + return 2; + } + return 0; } /* @@ -191,6 +243,30 @@ namespace Database { return result; } + /* + * Executes the prepared statement SELECT_FREELANCER_COMMISSION_STATE + * Takes an open pqxx::connection and the id to select by + */ + pqxx::result executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(pqxx::connection &connection, int freelancerID) { + connection.prepare(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE, SQL_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE); + pqxx::work work(connection); + pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_COMMISSION_STATE, freelancerID); + work.commit(); + return result; + } + + /* + * Executes the prepared statement SELECT_FREELANCER_EMAIL + * Takes an open pqxx::connection and the id to select by + */ + pqxx::result executePreparedStatement_SELECT_FREELANCER_EMAIL(pqxx::connection &connection, int freelancerID) { + connection.prepare(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL, SQL_STATEMENT_SELECT_FREELANCER_EMAIL); + pqxx::work work(connection); + pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_SELECT_FREELANCER_EMAIL, freelancerID); + work.commit(); + return result; + } + /* * Executes the prepared statement SELECT_TEMPLATE * Takes an open pqxx::connection and the id to select by