113 lines
4.3 KiB
C++
113 lines
4.3 KiB
C++
#include "crow.h"
|
|
#include <pqxx/pqxx>
|
|
#include <string>
|
|
#include <fmt/core.h>
|
|
#include "database.cpp"
|
|
|
|
using namespace std;
|
|
|
|
int main() {
|
|
crow::SimpleApp app;
|
|
|
|
// 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>();}});
|
|
// });
|
|
|
|
string databaseURI = "postgresql://cavecommadmin:cavecomm@localhost:5432/cavecomm";
|
|
|
|
|
|
/*
|
|
* Freelancer Profile listing for customers
|
|
*/
|
|
CROW_ROUTE(app, "/")
|
|
([databaseURI]() {
|
|
pqxx::connection databaseConnection(databaseURI);
|
|
pqxx::result result = Database::executeStatement_SELECT_FREELANCERS_WITHCOMMISSIONSSTATE(databaseConnection);
|
|
|
|
auto page = crow::mustache::load("customerIndex_FreelancerListing.html");
|
|
crow::json::wvalue resultJson = Database::convertResultToJSON(result, "freelancerProfiles");
|
|
|
|
crow::mustache::context ctx(resultJson);
|
|
return page.render(ctx);
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CROW_ROUTE(app, "/getEntry/<int>")
|
|
([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");
|
|
|
|
|
|
|
|
crow::json::wvalue resultJson = Database::convertResultRowToJSON(result);
|
|
crow::mustache::context ctx(resultJson);
|
|
|
|
return page.render(ctx);
|
|
|
|
|
|
});
|
|
|
|
CROW_ROUTE(app, "/testRequest")
|
|
([databaseURI]() {
|
|
Database::requestsItem item;
|
|
|
|
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);
|
|
|
|
return crow::response(200);
|
|
});
|
|
|
|
CROW_ROUTE(app, "/newRequest")
|
|
.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:
|
|
* 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);
|
|
|
|
pqxx::connection databaseConnection(databaseURI);
|
|
Database::executePreparedStatement_INSERT_ITEM_IN_REQUESTS(databaseConnection, item);
|
|
|
|
return crow::response(200);
|
|
});
|
|
|
|
|
|
//set the port, set the app to run on multiple threads, and run the app
|
|
app.port(18080).multithreaded().run();
|
|
}
|