Possible to load basic data from the database
This commit is contained in:
@@ -2,66 +2,71 @@
|
||||
|
||||
#include "ShadowrunApi.hpp"
|
||||
#include "ShadowrunCharacterForm.hpp"
|
||||
#include "database.hpp"
|
||||
#include "ShadowrunDb.hpp"
|
||||
#include <format>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
namespace shadowrun
|
||||
{
|
||||
|
||||
bool initDb() {
|
||||
auto db = Database();
|
||||
static std::unordered_map<std::string, std::string> parse_query_string(const std::string& query) {
|
||||
std::unordered_map<std::string, std::string> params;
|
||||
std::istringstream stream(query);
|
||||
std::string pair;
|
||||
|
||||
if (!db.open()){
|
||||
return false;
|
||||
while (std::getline(stream, pair, '&')) {
|
||||
auto pos = pair.find('=');
|
||||
if (pos != std::string::npos) {
|
||||
std::string key = pair.substr(0, pos);
|
||||
std::string value = pair.substr(pos + 1);
|
||||
params[key] = value; // You may want to URL-decode here
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
return params;
|
||||
}
|
||||
|
||||
static crow::response rsp(const std::string& msg){
|
||||
auto str = format("<div class='alert alert-success'>"
|
||||
"{} </div>", msg);
|
||||
return crow::response{str};
|
||||
}
|
||||
|
||||
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);
|
||||
auto params = parse_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
|
||||
auto name_data = params["Character-Info_Name"];
|
||||
if (name_data.empty()){
|
||||
CROW_LOG_WARNING << "Character without name submited, will not be saved";
|
||||
return rsp("Failed : Character without name submited, will not be saved");
|
||||
}
|
||||
|
||||
// Optionally save to a DB or do logic here
|
||||
auto key = getKeyOfCharacter(name_data);
|
||||
if (key < 0){
|
||||
CROW_LOG_ERROR << "Failed to create id of character : " << name_data;
|
||||
return rsp("Failed to create id of character");
|
||||
}
|
||||
|
||||
// Return response HTML
|
||||
std::ostringstream out;
|
||||
out << "<div class='alert alert-success'>"
|
||||
<< "Character " << name << " submitted successfully!"
|
||||
<< "</div>";
|
||||
vector<pair<const string&, const string&>> idValues;
|
||||
idValues.reserve(ShadowrunCharacterForm::m_formIds.size());
|
||||
|
||||
for (auto& id : ShadowrunCharacterForm::m_formIds) {
|
||||
auto data = params[id];
|
||||
if(!
|
||||
data.empty()){
|
||||
idValues.push_back(make_pair(id, data));
|
||||
}
|
||||
}
|
||||
|
||||
return crow::response{out.str()};
|
||||
if (!storeCharacterData(key, idValues)){
|
||||
CROW_LOG_ERROR << "Failed to store character data of " << name_data;
|
||||
return rsp("Failed to store character data");
|
||||
};
|
||||
return rsp(format("Character {} submitted successfully", name_data));
|
||||
});
|
||||
|
||||
CROW_ROUTE(app, "/api/shadowrun/character-form")
|
||||
@@ -69,9 +74,7 @@ void initApi(crow::SimpleApp& app)
|
||||
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;
|
||||
auto data = getCharacterData(getKeyOfCharacter(name));
|
||||
|
||||
return crow::response{ShadowrunCharacterForm().htmx()};
|
||||
});
|
||||
@@ -81,7 +84,7 @@ void initApi(crow::SimpleApp& app)
|
||||
std::ostringstream html;
|
||||
|
||||
// Simulated character database
|
||||
std::vector<std::string> characters = { "Trogdor", "Alice", "Zigzag" };
|
||||
auto characters = getCharacters();
|
||||
|
||||
html << "<form hx-get='/api/shadowrun/character-form' hx-target='#form-container' hx-params='*'>"
|
||||
<< "<label>Character Name: "
|
||||
@@ -98,7 +101,7 @@ void initApi(crow::SimpleApp& app)
|
||||
return crow::response{html.str()};
|
||||
});
|
||||
|
||||
if(initDb()){
|
||||
if(!shadowrun::initDb()){
|
||||
CROW_LOG_ERROR << "Failed to Init shadowrun database";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user