rewrote to use C++ instead of Java
This commit is contained in:
110
src/main.cpp
Normal file
110
src/main.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
#include "crow.h"
|
||||
#include <pqxx/pqxx>
|
||||
#include <string>
|
||||
#include <fmt/core.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
crow::SimpleApp app;
|
||||
//define your endpoint at the root directory
|
||||
CROW_ROUTE(app, "/")([](){
|
||||
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")
|
||||
([](){
|
||||
pqxx::connection db("postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm");
|
||||
pqxx::work work(db);
|
||||
pqxx::result result = work.exec("SELECT * from requests;");
|
||||
work.commit();
|
||||
|
||||
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 );
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
/*
|
||||
* 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 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);
|
||||
});
|
||||
|
||||
|
||||
//set the port, set the app to run on multiple threads, and run the app
|
||||
app.port(18080).multithreaded().run();
|
||||
}
|
Reference in New Issue
Block a user