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.
practice-tracker/server.cpp

124 lines
3.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;
}
void connect(){
con = driver->connect(getenv("DBURL"),getenv("DBUSER"),getenv("DBPWD"));
con->setSchema("practice");
stmt = con->createStatement();
}
int main()
{
crow::App<crow::CORSHandler> app;
driver = get_driver_instance();
CROW_ROUTE(app, "/categories")([](){
connect();
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 instruments.name ASC, category 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")([](){
connect();
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")([](){
connect();
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;
});
CROW_ROUTE(app, "/show-category/<int>")([](int category_id){
connect();
crow::json::wvalue entries;
ResultSet *res = stmt->executeQuery("call show_category("+to_string(category_id)+");");
int i = 0;
res->afterLast();
while (res->previous()) {
int id = res->getInt("id");
string description = res->getString("description");
int seconds = res->getInt("seconds");
string comments = res->getString("comments");
string created_at = res->getString("created_at");
int category_id = res->getInt("category_id");
string category = res->getString("category");
string instrument = res->getString("instrument");
entries[i] = {
{"id", id},
{"description", description},
{"seconds", seconds},
{"comments", comments},
{"created_at", created_at},
{"category_id", category_id},
{"category", category},
{"instrument", instrument}
};
i++;
}
delete res;
return entries;
});
app.port(18080).run();
atexit(exiting);
}