You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.1 KiB
84 lines
2.1 KiB
#include "crow.h"
|
|
#include "crow/middlewares/cors.h"
|
|
|
|
#include <driver.h>
|
|
#include <statement.h>
|
|
|
|
using namespace sql;
|
|
using namespace std;
|
|
|
|
Driver *driver;
|
|
Connection *con;
|
|
Statement *stmt;
|
|
|
|
void exiting(){
|
|
delete con;
|
|
delete stmt;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
crow::App<crow::CORSHandler> app;
|
|
|
|
driver = get_driver_instance();
|
|
con = driver->connect(getenv("DBURL"),getenv("DBUSER"),getenv("DBPWD"));
|
|
con->setSchema("practice");
|
|
stmt = con->createStatement();
|
|
|
|
CROW_ROUTE(app, "/categories")([](){
|
|
crow::json::wvalue entries;
|
|
|
|
ResultSet *res = stmt->executeQuery("SELECT practice_categories.id, practice_categories.name AS category, instruments.name AS instrument FROM practice_categories JOIN instruments ON practice_categories.instrument_id = instruments.id ORDER BY practice_categories.id ASC");
|
|
|
|
int i = 0;
|
|
while (res->next()) {
|
|
int id = res->getInt("id");
|
|
string category = res->getString("category");
|
|
string instrument = res->getString("instrument");
|
|
entries[i] = {{"id", id}, {"category", category}, {"instrument", instrument}};
|
|
i++;
|
|
}
|
|
|
|
delete res;
|
|
return entries;
|
|
});
|
|
|
|
CROW_ROUTE(app, "/summary")([](){
|
|
crow::json::wvalue entries;
|
|
|
|
ResultSet *res = stmt->executeQuery("SELECT * FROM summary ORDER BY chunks_practiced ASC;");
|
|
|
|
int i = 0;
|
|
while (res->next()) {
|
|
int category_id = res->getInt("category_id");
|
|
string category = res->getString("category");
|
|
string instrument = res->getString("instrument");
|
|
double chunks_practiced = res->getDouble("chunks_practiced");
|
|
int weight = res->getInt("weight");
|
|
entries[i] = {{"category_id", category_id}, {"category", category}, {"instrument", instrument}, {"chunks_practiced", chunks_practiced}, {"weight", weight}};
|
|
i++;
|
|
}
|
|
|
|
delete res;
|
|
return entries;
|
|
});
|
|
|
|
CROW_ROUTE(app, "/mins-left-to-practice-today")([](){
|
|
crow::json::wvalue entries;
|
|
|
|
ResultSet *res = stmt->executeQuery("SELECT * FROM mins_left_to_practice_today;");
|
|
|
|
res->next();
|
|
double time_left = res->getDouble("time_left");
|
|
entries = {{"time_left", time_left}};
|
|
|
|
delete res;
|
|
return entries;
|
|
});
|
|
|
|
app.port(18080).run();
|
|
|
|
atexit(exiting);
|
|
}
|
|
|