Implement comprehensive character deletion process in UserCharacterWorker

- Add queries and logic to delete associated data when a character dies, including directors, relationships, child relations, knowledge, debtors prism, political offices, and election candidates.
- Enhance error handling to log issues during the deletion process.
This commit is contained in:
Torsten Schulz (local)
2025-10-29 15:07:44 +01:00
committed by Torsten (PC)
parent 7591787583
commit 6a1260687b
2 changed files with 76 additions and 2 deletions

View File

@@ -123,8 +123,42 @@ void UserCharacterWorker::handleCharacterDeath(int characterId) {
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
db.prepare("delete_character", "DELETE FROM falukant_data.character WHERE id = $1");
db.execute("delete_character", { std::to_string(characterId) });
try {
// 1) Director löschen (falls Character ein Director ist)
db.prepare("delete_director", QUERY_DELETE_DIRECTOR);
db.execute("delete_director", { std::to_string(characterId) });
// 2) Relationships löschen (Ehepartner, etc.)
db.prepare("delete_relationship", QUERY_DELETE_RELATIONSHIP);
db.execute("delete_relationship", { std::to_string(characterId) });
// 3) Child-Relations löschen (als Kind, Vater oder Mutter)
db.prepare("delete_child_relation", QUERY_DELETE_CHILD_RELATION);
db.execute("delete_child_relation", { std::to_string(characterId) });
// 4) Knowledge löschen
db.prepare("delete_knowledge", QUERY_DELETE_KNOWLEDGE);
db.execute("delete_knowledge", { std::to_string(characterId) });
// 5) Debtors_prism löschen
db.prepare("delete_debtors_prism", QUERY_DELETE_DEBTORS_PRISM);
db.execute("delete_debtors_prism", { std::to_string(characterId) });
// 6) Political Office löschen
db.prepare("delete_political_office", QUERY_DELETE_POLITICAL_OFFICE);
db.execute("delete_political_office", { std::to_string(characterId) });
// 7) Election Candidate löschen
db.prepare("delete_election_candidate", QUERY_DELETE_ELECTION_CANDIDATE);
db.execute("delete_election_candidate", { std::to_string(characterId) });
// 8) Character löschen
db.prepare("delete_character", "DELETE FROM falukant_data.character WHERE id = $1");
db.execute("delete_character", { std::to_string(characterId) });
} catch (const std::exception &e) {
std::cerr << "[UserCharacterWorker] Fehler beim Löschen der Character-Verknüpfungen: "
<< e.what() << std::endl;
}
}
void UserCharacterWorker::setHeir(int characterId) {

View File

@@ -369,6 +369,46 @@ private:
OR mother_character_id = $1
)";
// Queries zum Löschen von Character-Verknüpfungen beim Tod
static constexpr const char *QUERY_DELETE_DIRECTOR = R"(
DELETE FROM falukant_data.director
WHERE director_character_id = $1
RETURNING employer_user_id;
)";
static constexpr const char *QUERY_DELETE_RELATIONSHIP = R"(
DELETE FROM falukant_data.relationship
WHERE character1_id = $1
OR character2_id = $1;
)";
static constexpr const char *QUERY_DELETE_CHILD_RELATION = R"(
DELETE FROM falukant_data.child_relation
WHERE child_character_id = $1
OR father_character_id = $1
OR mother_character_id = $1;
)";
static constexpr const char *QUERY_DELETE_KNOWLEDGE = R"(
DELETE FROM falukant_data.knowledge
WHERE character_id = $1;
)";
static constexpr const char *QUERY_DELETE_DEBTORS_PRISM = R"(
DELETE FROM falukant_data.debtors_prism
WHERE character_id = $1;
)";
static constexpr const char *QUERY_DELETE_POLITICAL_OFFICE = R"(
DELETE FROM falukant_data.political_office
WHERE character_id = $1;
)";
static constexpr const char *QUERY_DELETE_ELECTION_CANDIDATE = R"(
DELETE FROM falukant_data.election_candidate
WHERE character_id = $1;
)";
};
#endif // USERCHARACTERWORKER_H