completed HTML assembler templates

This commit is contained in:
2023-03-13 03:39:46 -05:00
parent 8d9d3df7eb
commit fc70b91b9d
30 changed files with 176 additions and 1307 deletions

5
src/README.md Normal file
View File

@ -0,0 +1,5 @@
Depends on:
https://packages.debian.org/bullseye/libpqxx-dev
https://packages.debian.org/bullseye/libfmt-dev
https://crowcpp.org

View File

@ -13,96 +13,128 @@ int main()
return "Hello world";
});
// CROW_ROUTE(app, "/test.json")
// ([](){
// pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm");
// pqxx::work work(db);
// pqxx::result result = work.exec("SELECT * from public.requests;");
// work.commit();
// return crow::json::wvalue x({{"message", rows[0][0].as<int>();}});
// });
// CROW_ROUTE(app, "/test.json")
// ([](){
// pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm");
// pqxx::work work(db);
// pqxx::result result = work.exec("SELECT * from public.requests;");
// work.commit();
// return crow::json::wvalue x({{"message", rows[0][0].as<int>();}});
// });
CROW_ROUTE(app, "/test")
([](){
pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm");
pqxx::work work(db);
pqxx::result result = work.exec("SELECT * from requests;");
work.commit();
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();
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);
});
return result[0][0].as<string>();
});
CROW_ROUTE(app, "/testRequest")
([](){
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 );
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 );
pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm");
pqxx::work work(db);
work.exec( sql );
work.commit();
return crow::response(200);
pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm");
pqxx::work work(db);
work.exec( sql );
work.commit();
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);
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:
* 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
*/
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();
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();
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 );
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);
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();
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