Rewriting of test routes

This commit is contained in:
Tina_Azure
2023-03-20 18:07:51 +01:00
parent 74a707f938
commit aea7bebbb5
3 changed files with 55 additions and 104 deletions

0
setupdb.sh Executable file → Normal file
View File

0
spec/buildchart.sh Executable file → Normal file
View File

View File

@ -2,16 +2,16 @@
#include <pqxx/pqxx>
#include <string>
#include <fmt/core.h>
#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<int>();}});
// });
string databaseURI = "postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm";
CROW_ROUTE(app, "/getEntry/<int>")
([](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<int>();
string customerEmailAddress = result[0][1].as<string>();
string freelancer = result[0][2].as<string>();
string templateName = result[0][3].as<string>();
string currencyPreference = result[0][4].as<string>();
double priceUpFront = result[0][5].as<double>();
double priceOnDeliver = result[0][6].as<double>();
string requestDescription = result[0][7].as<string>();
bool accepted = result[0][8].as<bool>();
string upFrontInvoiceID = result[0][9].as<string>();
string onDeliverInvoiceID = result[0][10].as<string>();
bool upFrontPaid = result[0][11].as<bool>();
bool onDeliverPaid = result[0][12].as<bool>();
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