From 6a1260687bb35d20a4cbfae968e6fa8b322fae32 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 29 Oct 2025 15:07:44 +0100 Subject: [PATCH] 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. --- src/usercharacterworker.cpp | 38 +++++++++++++++++++++++++++++++++-- src/usercharacterworker.h | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/usercharacterworker.cpp b/src/usercharacterworker.cpp index 22778bd..8cd0a9e 100644 --- a/src/usercharacterworker.cpp +++ b/src/usercharacterworker.cpp @@ -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) { diff --git a/src/usercharacterworker.h b/src/usercharacterworker.h index 30eb682..eba378b 100644 --- a/src/usercharacterworker.h +++ b/src/usercharacterworker.h @@ -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