added shadowrun database
This commit is contained in:
106
src/shadowrun/ShadowrunApi.cpp
Normal file
106
src/shadowrun/ShadowrunApi.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
|
||||
|
||||
#include "ShadowrunApi.hpp"
|
||||
#include "ShadowrunCharacterForm.hpp"
|
||||
#include "database.hpp"
|
||||
|
||||
namespace shadowrun
|
||||
{
|
||||
|
||||
bool initDb() {
|
||||
auto db = Database();
|
||||
|
||||
if (!db.open()){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a tables
|
||||
const char* create_sql_chars = "CREATE TABLE IF NOT EXISTS shadowrun_characters ("
|
||||
"id INTEGER PRIMARY KEY,"
|
||||
"name TEXT,"
|
||||
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP);";
|
||||
|
||||
if (!db.exec(create_sql_chars)){
|
||||
CROW_LOG_ERROR << "Failed to create shadowrun_characters table";
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* create_sql_data = "CREATE TABLE IF NOT EXISTS shadowrun_data ("
|
||||
"id INTEGER PRIMARY KEY,"
|
||||
"character_id INTEGER NOT NULL,"
|
||||
"name TEXT NOT NULL,"
|
||||
"value TEXT,"
|
||||
"created_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"FOREIGN KEY (character_id) REFERENCES characters(id) ON DELETE CASCADE);";
|
||||
|
||||
if (!db.exec(create_sql_data)){
|
||||
CROW_LOG_ERROR << "Failed to create shadowrun_data table";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void initApi(crow::SimpleApp& app)
|
||||
{
|
||||
CROW_ROUTE(app, "/api/shadowrun/submit-character").methods("POST"_method)(
|
||||
[](const crow::request& req) {
|
||||
auto params = crow::query_string(req.body);
|
||||
|
||||
std::string name = params.get("name") ? params.get("name") : "";
|
||||
std::string metatype = params.get("metatype") ? params.get("metatype") : "";
|
||||
std::string age = params.get("age") ? params.get("age") : "";
|
||||
// ... extract more fields as needed
|
||||
|
||||
// Optionally save to a DB or do logic here
|
||||
|
||||
// Return response HTML
|
||||
std::ostringstream out;
|
||||
out << "<div class='alert alert-success'>"
|
||||
<< "Character " << name << " submitted successfully!"
|
||||
<< "</div>";
|
||||
|
||||
return crow::response{out.str()};
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/character-form")
|
||||
([](const crow::request& req) {
|
||||
auto query = crow::query_string(req.url_params);
|
||||
std::string name = query.get("name") ? query.get("name") : "";
|
||||
|
||||
// TODO: Load data from file or DB using `name`
|
||||
std::string metatype = "Troll";
|
||||
int age = 28;
|
||||
|
||||
return crow::response{ShadowrunCharacterForm().htmx()};
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/character-list")
|
||||
([] {
|
||||
std::ostringstream html;
|
||||
|
||||
// Simulated character database
|
||||
std::vector<std::string> characters = { "Trogdor", "Alice", "Zigzag" };
|
||||
|
||||
html << "<form hx-get='/api/shadowrun/character-form' hx-target='#form-container' hx-params='*'>"
|
||||
<< "<label>Character Name: "
|
||||
<< "<select name='name'>";
|
||||
|
||||
for (const auto& name : characters) {
|
||||
html << "<option value='" << name << "'>" << name << "</option>";
|
||||
}
|
||||
|
||||
html << "</select></label>"
|
||||
<< "<button type='submit'>Load Character</button>"
|
||||
<< "</form>";
|
||||
|
||||
return crow::response{html.str()};
|
||||
});
|
||||
|
||||
if(initDb()){
|
||||
CROW_LOG_ERROR << "Failed to Init shadowrun database";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user