109 lines
3.3 KiB
C++
109 lines
3.3 KiB
C++
|
|
|
|
#include "ShadowrunApi.hpp"
|
|
#include "ShadowrunCharacterForm.hpp"
|
|
#include "ShadowrunDb.hpp"
|
|
#include <format>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
namespace shadowrun
|
|
{
|
|
|
|
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;
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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 = parse_query_string(req.body);
|
|
|
|
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");
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
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")
|
|
([](const crow::request& req) {
|
|
auto query = crow::query_string(req.url_params);
|
|
std::string name = query.get("name") ? query.get("name") : "";
|
|
|
|
auto data = getCharacterData(getKeyOfCharacter(name));
|
|
|
|
return crow::response{ShadowrunCharacterForm().htmx()};
|
|
});
|
|
|
|
CROW_ROUTE(app, "/api/shadowrun/character-list")
|
|
([] {
|
|
std::ostringstream html;
|
|
|
|
// Simulated character database
|
|
auto characters = getCharacters();
|
|
|
|
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(!shadowrun::initDb()){
|
|
CROW_LOG_ERROR << "Failed to Init shadowrun database";
|
|
}
|
|
}
|
|
|
|
} |