From a1617760d4678d75a9bb4094b58b54d273a29710 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Thu, 15 Apr 2021 19:35:54 +0200 Subject: [PATCH] Create decks/remove.rs --- rslib/src/decks/mod.rs | 51 +----------------------------------- rslib/src/decks/remove.rs | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 rslib/src/decks/remove.rs diff --git a/rslib/src/decks/mod.rs b/rslib/src/decks/mod.rs index 86b549b6a..f2aeeaa2b 100644 --- a/rslib/src/decks/mod.rs +++ b/rslib/src/decks/mod.rs @@ -6,6 +6,7 @@ mod counts; mod current; mod filtered; mod name; +mod remove; mod reparent; mod schema11; mod tree; @@ -198,56 +199,6 @@ impl Collection { self.storage.get_deck_id(&machine_name) } - pub fn remove_decks_and_child_decks(&mut self, dids: &[DeckId]) -> Result> { - self.transact(Op::RemoveDeck, |col| { - let mut card_count = 0; - let usn = col.usn()?; - for did in dids { - if let Some(deck) = col.storage.get_deck(*did)? { - let child_decks = col.storage.child_decks(&deck)?; - - // top level - card_count += col.remove_single_deck(&deck, usn)?; - - // remove children - for deck in child_decks { - card_count += col.remove_single_deck(&deck, usn)?; - } - } - } - Ok(card_count) - }) - } - - pub(crate) fn remove_single_deck(&mut self, deck: &Deck, usn: Usn) -> Result { - let card_count = match deck.kind { - DeckKind::Normal(_) => self.delete_all_cards_in_normal_deck(deck.id)?, - DeckKind::Filtered(_) => { - self.return_all_cards_in_filtered_deck(deck.id)?; - 0 - } - }; - self.clear_aux_config_for_deck(deck.id)?; - if deck.id.0 == 1 { - // if the default deck is included, just ensure it's reset to the default - // name, as we've already removed its cards - let mut modified_default = deck.clone(); - modified_default.name = self.tr.deck_config_default_name().into(); - self.prepare_deck_for_update(&mut modified_default, usn)?; - modified_default.set_modified(usn); - self.update_single_deck_undoable(&mut modified_default, deck.clone())?; - } else { - self.remove_deck_and_add_grave_undoable(deck.clone(), usn)?; - } - Ok(card_count) - } - - fn delete_all_cards_in_normal_deck(&mut self, did: DeckId) -> Result { - let cids = self.storage.all_cards_in_single_deck(did)?; - self.remove_cards_and_orphaned_notes(&cids)?; - Ok(cids.len()) - } - /// Apply input delta to deck, and its parents. /// Caller should ensure transaction. pub(crate) fn update_deck_stats( diff --git a/rslib/src/decks/remove.rs b/rslib/src/decks/remove.rs new file mode 100644 index 000000000..b28ea7c3f --- /dev/null +++ b/rslib/src/decks/remove.rs @@ -0,0 +1,55 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +use crate::prelude::*; + +impl Collection { + pub fn remove_decks_and_child_decks(&mut self, dids: &[DeckId]) -> Result> { + self.transact(Op::RemoveDeck, |col| { + let mut card_count = 0; + let usn = col.usn()?; + for did in dids { + if let Some(deck) = col.storage.get_deck(*did)? { + let child_decks = col.storage.child_decks(&deck)?; + + // top level + card_count += col.remove_single_deck(&deck, usn)?; + + // remove children + for deck in child_decks { + card_count += col.remove_single_deck(&deck, usn)?; + } + } + } + Ok(card_count) + }) + } + + pub(crate) fn remove_single_deck(&mut self, deck: &Deck, usn: Usn) -> Result { + let card_count = match deck.kind { + DeckKind::Normal(_) => self.delete_all_cards_in_normal_deck(deck.id)?, + DeckKind::Filtered(_) => { + self.return_all_cards_in_filtered_deck(deck.id)?; + 0 + } + }; + self.clear_aux_config_for_deck(deck.id)?; + if deck.id.0 == 1 { + // if the default deck is included, just ensure it's reset to the default + // name, as we've already removed its cards + let mut modified_default = deck.clone(); + modified_default.name = self.tr.deck_config_default_name().into(); + self.prepare_deck_for_update(&mut modified_default, usn)?; + modified_default.set_modified(usn); + self.update_single_deck_undoable(&mut modified_default, deck.clone())?; + } else { + self.remove_deck_and_add_grave_undoable(deck.clone(), usn)?; + } + Ok(card_count) + } + + fn delete_all_cards_in_normal_deck(&mut self, did: DeckId) -> Result { + let cids = self.storage.all_cards_in_single_deck(did)?; + self.remove_cards_and_orphaned_notes(&cids)?; + Ok(cids.len()) + } +}