mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-03-06 08:39:47 +00:00
WIP working state
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "eGameMasterLevel.h"
|
||||
#include "Database.h"
|
||||
#include "json.hpp"
|
||||
|
||||
std::optional<IAccounts::Info> SQLiteDatabase::GetAccountInfo(const std::string_view username) {
|
||||
auto [_, result] = ExecuteSelect("SELECT * FROM accounts WHERE name = ? LIMIT 1", username);
|
||||
@@ -83,4 +84,102 @@ uint8_t SQLiteDatabase::GetFailedAttempts(const uint32_t accountId) {
|
||||
}
|
||||
|
||||
return result.getIntField("failed_attempts");
|
||||
}
|
||||
|
||||
nlohmann::json SQLiteDatabase::GetAccountsTable(uint32_t start, uint32_t length, const std::string_view search, uint32_t orderColumn, bool orderAsc) {
|
||||
// Build base query
|
||||
std::string baseQuery = "SELECT id, name, banned, locked, gm_level, mute_expire, created_at FROM accounts";
|
||||
std::string whereClause;
|
||||
std::string orderClause;
|
||||
|
||||
// Add search filter if provided
|
||||
if (!search.empty()) {
|
||||
whereClause = " WHERE name LIKE '%' || ? || '%'";
|
||||
}
|
||||
|
||||
// Map column indices to database columns
|
||||
std::string orderColumnName = "id";
|
||||
switch (orderColumn) {
|
||||
case 0: orderColumnName = "id"; break;
|
||||
case 1: orderColumnName = "name"; break;
|
||||
case 2: orderColumnName = "banned"; break;
|
||||
case 3: orderColumnName = "locked"; break;
|
||||
case 4: orderColumnName = "gm_level"; break;
|
||||
case 5: orderColumnName = "mute_expire"; break;
|
||||
case 6: orderColumnName = "created_at"; break;
|
||||
default: orderColumnName = "id";
|
||||
}
|
||||
|
||||
orderClause = " ORDER BY " + orderColumnName + (orderAsc ? " ASC" : " DESC");
|
||||
|
||||
// Build the main query
|
||||
std::string mainQuery = baseQuery + whereClause + orderClause + " LIMIT ? OFFSET ?;";
|
||||
|
||||
// Get total count
|
||||
std::string totalCountQuery = "SELECT COUNT(*) as count FROM accounts;";
|
||||
auto [_, totalCountResult] = ExecuteSelect(totalCountQuery);
|
||||
uint32_t totalRecords = totalCountResult.eof() ? 0 : totalCountResult.getIntField("count");
|
||||
|
||||
// Get filtered count
|
||||
uint32_t filteredRecords = totalRecords;
|
||||
if (!search.empty()) {
|
||||
std::string filteredCountQuery = "SELECT COUNT(*) as count FROM accounts WHERE name LIKE '%' || ? || '%';";
|
||||
auto [__, filteredCountResult] = ExecuteSelect(filteredCountQuery, search);
|
||||
filteredRecords = filteredCountResult.eof() ? 0 : filteredCountResult.getIntField("count");
|
||||
}
|
||||
|
||||
// Execute main query
|
||||
auto [stmt, result] = !search.empty() ?
|
||||
ExecuteSelect(mainQuery, search, length, start) :
|
||||
ExecuteSelect(mainQuery, length, start);
|
||||
|
||||
// Build response JSON
|
||||
nlohmann::json accountsArray = nlohmann::json::array();
|
||||
|
||||
while (!result.eof()) {
|
||||
nlohmann::json account = {
|
||||
{"id", result.getIntField("id")},
|
||||
{"name", result.getStringField("name")},
|
||||
{"banned", result.getIntField("banned")},
|
||||
{"locked", result.getIntField("locked")},
|
||||
{"gm_level", result.getIntField("gm_level")},
|
||||
{"mute_expire", result.getInt64Field("mute_expire")},
|
||||
{"created_at", result.getStringField("created_at")}
|
||||
};
|
||||
accountsArray.push_back(account);
|
||||
result.nextRow();
|
||||
}
|
||||
|
||||
nlohmann::json response = {
|
||||
{"draw", 1},
|
||||
{"recordsTotal", totalRecords},
|
||||
{"recordsFiltered", filteredRecords},
|
||||
{"data", accountsArray}
|
||||
};
|
||||
|
||||
return response;
|
||||
}
|
||||
nlohmann::json SQLiteDatabase::GetAccountById(uint32_t accountId) {
|
||||
try {
|
||||
auto [_, result] = ExecuteSelect("SELECT * FROM accounts WHERE id = ? LIMIT 1;", accountId);
|
||||
|
||||
if (result.eof()) {
|
||||
return nlohmann::json{{"error", "Account not found"}};
|
||||
}
|
||||
|
||||
nlohmann::json account = {
|
||||
{"id", result.getIntField("id")},
|
||||
{"name", result.getStringField("name")},
|
||||
{"banned", result.getIntField("banned")},
|
||||
{"locked", result.getIntField("locked")},
|
||||
{"gm_level", result.getIntField("gm_level")},
|
||||
{"mute_expire", result.getInt64Field("mute_expire")},
|
||||
{"created_at", result.getStringField("created_at")}
|
||||
};
|
||||
|
||||
return account;
|
||||
} catch (const CppSQLite3Exception& e) {
|
||||
LOG_DEBUG("SQLite Error: %s", e.errorMessage());
|
||||
return nlohmann::json{{"error", "Database error"}};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user