(&self, mut mutation: Mutation, store: &S)
| 71 | |
| 72 | impl DeleteTopic { |
| 73 | pub fn call<S>(&self, mut mutation: Mutation, store: &S) -> Result<DeleteTopicResult> |
| 74 | where |
| 75 | S: SaveChangesForPrefix, |
| 76 | { |
| 77 | let topic_id = &self.topic_id; |
| 78 | let added = chrono::Utc::now(); |
| 79 | |
| 80 | let topic = mutation.fetch_topic(self.repo_id, topic_id); |
| 81 | if topic.is_none() { |
| 82 | return Err(Error::NotFound(format!("not found: {topic_id}"))); |
| 83 | } |
| 84 | let topic = topic.unwrap(); |
| 85 | |
| 86 | if topic.root() { |
| 87 | return Err(Error::Repo("cannot delete root topic".to_owned())); |
| 88 | } |
| 89 | |
| 90 | mutation.mark_deleted(self.repo_id, topic_id)?; |
| 91 | |
| 92 | let parent_topics = topic |
| 93 | .parent_topics |
| 94 | .iter() |
| 95 | .map(|parent| ParentTopic { |
| 96 | id: parent.id.to_owned(), |
| 97 | }) |
| 98 | .collect::<BTreeSet<ParentTopic>>(); |
| 99 | |
| 100 | let mut topics = vec![]; |
| 101 | |
| 102 | let mut child_links = vec![]; |
| 103 | let mut child_topics = vec![]; |
| 104 | |
| 105 | // Remove the topic from its children |
| 106 | for child in &topic.children { |
| 107 | match mutation.fetch(self.repo_id, &child.id) { |
| 108 | Some(RepoObject::Link(child_link)) => { |
| 109 | let mut child_link = child_link.to_owned(); |
| 110 | child_link.parent_topics.remove(&topic.to_parent_topic()); |
| 111 | child_link.parent_topics.append(&mut parent_topics.clone()); |
| 112 | child_links.push(child_link.clone()); |
| 113 | mutation.save_link(self.repo_id, &child_link)?; |
| 114 | } |
| 115 | |
| 116 | Some(RepoObject::Topic(child_topic)) => { |
| 117 | let mut child_topic = child_topic.to_owned(); |
| 118 | child_topic.parent_topics.remove(&topic.to_parent_topic()); |
| 119 | child_topic.parent_topics.append(&mut parent_topics.clone()); |
| 120 | child_topics.push(child_topic.clone()); |
| 121 | mutation.save_topic(self.repo_id, &child_topic)?; |
| 122 | } |
| 123 | |
| 124 | None => {} |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | for parent in &topic.parent_topics { |
| 129 | if let Some(mut parent) = mutation.fetch_topic(self.repo_id, &parent.id) { |
| 130 | // Remove the topic from the children of the parent topics |
nothing calls this directly
no test coverage detected