Skip to content

Commit

Permalink
[#112] Check romantic too
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed Nov 22, 2023
1 parent ff3b3b6 commit 4d0c007
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 14 additions & 6 deletions rpg_tools_core/src/usecase/delete/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ use crate::utils::storage::DeleteElementResult;

/// Tries to delete a [`character`](crate::model::character::Character).
pub fn delete_character(data: &mut RpgData, id: CharacterId) -> DeleteResult {
let relations = data
.relations
.relationships
.get_all_of(id)
.map(|map| map.len())
.unwrap_or_default();
let relations =
data.relations.relationships.count_all_of(id) + data.relations.romantic.count_all_of(id);

if relations > 0 {
return DeleteResult::Blocked(BlockingReason {
Expand All @@ -29,6 +25,7 @@ pub fn delete_character(data: &mut RpgData, id: CharacterId) -> DeleteResult {
mod tests {
use super::*;
use crate::model::character::relation::relationship::Relationship::Friend;
use crate::model::character::relation::romantic::RomanticRelationship::Spouse;
use DeleteResult::*;

const RESULT: DeleteResult = Blocked(BlockingReason {
Expand Down Expand Up @@ -64,4 +61,15 @@ mod tests {
assert_eq!(RESULT, delete_character(&mut data, id0));
assert_eq!(RESULT, delete_character(&mut data, id1));
}

#[test]
fn test_blocked_by_romantic() {
let mut data = RpgData::default();
let id0 = data.character_manager.create();
let id1 = data.character_manager.create();
data.relations.romantic.add(id0, id1, Spouse);

assert_eq!(RESULT, delete_character(&mut data, id0));
assert_eq!(RESULT, delete_character(&mut data, id1));
}
}
8 changes: 8 additions & 0 deletions rpg_tools_core/src/utils/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ impl<I: Id, T: Clone> RelationStorage<I, T> {
self.relations.contains_key(&id)
}

/// Counts all relations for a specific element.
pub fn count_all_of(&self, id: I) -> usize {
self.relations
.get(&id)
.map(|map| map.len())
.unwrap_or_default()
}

/// Gets all relations for a specific element.
pub fn get_all_of(&self, id: I) -> Option<&HashMap<I, T>> {
self.relations.get(&id)
Expand Down

0 comments on commit 4d0c007

Please sign in to comment.