From aea7bebbb51d15ab0ef73b71b0cb76ff5a480d71 Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Mon, 20 Mar 2023 18:07:51 +0100 Subject: [PATCH] Rewriting of test routes --- setupdb.sh | 0 spec/buildchart.sh | 0 src/main.cpp | 159 ++++++++++++++++----------------------------- 3 files changed, 55 insertions(+), 104 deletions(-) mode change 100755 => 100644 setupdb.sh mode change 100755 => 100644 spec/buildchart.sh diff --git a/setupdb.sh b/setupdb.sh old mode 100755 new mode 100644 diff --git a/spec/buildchart.sh b/spec/buildchart.sh old mode 100755 new mode 100644 diff --git a/src/main.cpp b/src/main.cpp index d2c3567..7a8a171 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,16 +2,16 @@ #include #include #include +#include "database.cpp" using namespace std; -int main() -{ - crow::SimpleApp app; +int main() { + crow::SimpleApp app; //define your endpoint at the root directory - CROW_ROUTE(app, "/")([](){ - return "Hello world"; - }); + CROW_ROUTE(app, "/")([]() { + return "Hello world"; + }); // CROW_ROUTE(app, "/test.json") // ([](){ @@ -22,119 +22,70 @@ int main() // return crow::json::wvalue x({{"message", rows[0][0].as();}}); // }); + string databaseURI = "postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm"; + + CROW_ROUTE(app, "/getEntry/") - ([](int entry){ - pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm"); - pqxx::work work(db); - pqxx::result result = work.exec("SELECT * from requests where id=" + to_string(entry)); - work.commit(); + ([databaseURI](int entry) { + pqxx::connection databaseConnection(databaseURI); + pqxx::result result = Database::executePreparedStatement_SELECT_ITEM_BY_ID(databaseConnection, entry); - auto page = crow::mustache::load("test.html"); - - //There's very likely a nice way of doing this with a mix of lamdas and auto, but I don't care right now. - int id = result[0][0].as(); - string customerEmailAddress = result[0][1].as(); - string freelancer = result[0][2].as(); - string templateName = result[0][3].as(); - string currencyPreference = result[0][4].as(); - double priceUpFront = result[0][5].as(); - double priceOnDeliver = result[0][6].as(); - string requestDescription = result[0][7].as(); - bool accepted = result[0][8].as(); - string upFrontInvoiceID = result[0][9].as(); - string onDeliverInvoiceID = result[0][10].as(); - bool upFrontPaid = result[0][11].as(); - bool onDeliverPaid = result[0][12].as(); - crow::mustache::context ctx; - ctx["id"] = id ; - ctx["customerEmailAddress"] = customerEmailAddress; - ctx["freelancer"] = freelancer ; - ctx["templateName"] = templateName ; - ctx["currencyPreference"] = currencyPreference ; - ctx["priceUpFront"] = priceUpFront ; - ctx["priceOnDeliver"] = priceOnDeliver ; - ctx["requestDescription"] = requestDescription ; - ctx["upFrontInvoiceID"] = upFrontInvoiceID ; - ctx["onDeliverInvoiceID"] = onDeliverInvoiceID ; - ctx["upFrontPaid"] = to_string(upFrontPaid); - ctx["onDeliverPaid"] = to_string(onDeliverPaid); - ctx["accepted"] = to_string(accepted) ; - return page.render(ctx); + auto page = crow::mustache::load("test.html"); - }); + + crow::json::wvalue resultJson = Database::convertResultRowToJSON(result); + crow::mustache::context ctx(resultJson); + + return page.render(ctx); + + + }); CROW_ROUTE(app, "/testRequest") - ([](){ + ([databaseURI]() { + Database::requestsItem item; - string customerEmailAddress = "test@testmail.com"; - string freelancer = "michael"; - string templateName = "coding"; - string currencyPreference = "XMR"; - string priceUpFront = "0.36"; - string priceOnDeliver = "0.36"; - string requestDescription = "This is a test"; - string accepted = "0"; - string upFrontInvoiceID = "12424242424"; - string onDeliverInvoiceID = "329532532532"; - string upFrontPaid = "0"; - string onDeliverPaid = "0"; - string sql = fmt::format("INSERT INTO requests(id, customerEmailAddress, freelancer, templateName, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, accepted, upFrontInvoiceID, onDeliverInvoiceID, upFrontPaid, onDeliverPaid) \ - VALUES(DEFAULT, '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}' );", customerEmailAddress, freelancer, templateName, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, accepted, upFrontInvoiceID, onDeliverInvoiceID, upFrontPaid, onDeliverPaid ); + item.customerEmailAddress = "test@testmail.com"; + item.freelancer = "michael"; + item.templateName = "coding"; + item.currencyPreference = "XMR"; + item.priceUpFront = "0.36"; + item.priceOnDeliver = "0.36"; + item.requestDescription = "This is a test"; + item.accepted = stoi("0"); + item.upFrontInvoiceID = "12424242424"; + item.onDeliverInvoiceID = "329532532532"; + item.upFrontPaid = stoi("0"); + item.onDeliverPaid = stoi("0"); + pqxx::connection databaseConnection(databaseURI); + Database::executePreparedStatement_INSERT_ITEM_IN_REQUESTS(databaseConnection, item); - - pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm"); - pqxx::work work(db); - work.exec( sql ); - work.commit(); - - return crow::response(200); - }); + return crow::response(200); + }); CROW_ROUTE(app, "/newRequest") - .methods("POST"_method) - ([](const crow::request& req){ - auto jsonBody = crow::json::load(req.body); - if (!jsonBody) - return crow::response(crow::status::BAD_REQUEST); + .methods("POST"_method) + ([databaseURI](const crow::request &req) { + auto jsonBody = crow::json::load(req.body); + if (!jsonBody) + return crow::response(crow::status::BAD_REQUEST); - /* - * Example CURL to insert into DB: - * curl -d '{"customerEmailAddress":"test@testmail.com","freelancer":"michael","templateName":"coding","currencyPreference":"XMR","priceUpFront":0.36,"priceOnDeliver":0.36,"requestDescription":"This is a test","accepted":false,"upFrontInvoiceID":"12424242424","onDeliverInvoiceID":"329532532532","upFrontPaid":false,"onDeliverPaid":false}' http://0.0.0.0:18080/newRequest - */ + /* + * Example CURL to insert into DB: + * Price must be delivered as a string to avoid unnecessary conversion to double which would lead to the addition to a database of the value 0.359999999999999999999999999 instead of 0.36. + * curl -H "Content-Type: application/json" -H "Accept: application/json" -d '{"customerEmailAddress":"2@testmail.com","freelancer":"nick","templateName":"coding","currencyPreference":"XMR","priceUpFront":"0.36","priceOnDeliver":"0.36","requestDescription":"This is a test","accepted":false,"upFrontInvoiceID":"12424242424","onDeliverInvoiceID":"329532532532","upFrontPaid":false,"onDeliverPaid":false}' http://0.0.0.0:18080/newRequest + */ + Database::requestsItem item; + item.insertJsonIntoItem(jsonBody); - string customerEmailAddress = jsonBody["customerEmailAddress"].s(); - string freelancer = jsonBody["freelancer"].s(); - string templateName = jsonBody["templateName"].s(); - string currencyPreference = jsonBody["currencyPreference"].s(); - double priceUpFront = jsonBody["priceUpFront"].d(); - double priceOnDeliver = jsonBody["priceOnDeliver"].d(); - string requestDescription = jsonBody["requestDescription"].s(); - bool accepted = jsonBody["accepted"].b(); - string upFrontInvoiceID = jsonBody["upFrontInvoiceID"].s(); - string onDeliverInvoiceID = jsonBody["onDeliverInvoiceID"].s(); - bool upFrontPaid = jsonBody["upFrontPaid"].b(); - bool onDeliverPaid = jsonBody["onDeliverPaid"].b(); + pqxx::connection databaseConnection(databaseURI); + Database::executePreparedStatement_INSERT_ITEM_IN_REQUESTS(databaseConnection, item); - string sql = fmt::format("INSERT INTO requests(id, customerEmailAddress, freelancer, templateName, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, accepted, upFrontInvoiceID, onDeliverInvoiceID, upFrontPaid, onDeliverPaid) \ - VALUES(DEFAULT, '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}');", customerEmailAddress, freelancer, templateName, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, accepted ,upFrontInvoiceID , onDeliverInvoiceID, upFrontPaid, onDeliverPaid ); - - /* - In C++20 this would be valid: - sql = std::format("INSERT INTO requests \ - VALUES(DEFAULT, \"{}\", \"{}\", \"{}\", \"{}\", \"{}\", \"{}\" ,\"{}\" ,\"{}\" ,\"{}\" ,\"{}\", \"{}\", \"{}\");", - customerEmailAddress, freelancer, templateName, currencyPreference, priceUpFront, priceOnDeliver, requestDescription, accepted, upFrontInvoiceID, onDeliverInvoiceID, upFrontPaid, onDeliverPaid); - */ - - pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm"); - pqxx::work work(db); - work.exec( sql ); - work.commit(); - - return crow::response(200); - }); + return crow::response(200); + }); //set the port, set the app to run on multiple threads, and run the app