Create decks/remove.rs

This commit is contained in:
RumovZ 2021-04-15 19:35:54 +02:00
parent 7225b7e4dc
commit a1617760d4
2 changed files with 56 additions and 50 deletions

View File

@ -6,6 +6,7 @@ mod counts;
mod current; mod current;
mod filtered; mod filtered;
mod name; mod name;
mod remove;
mod reparent; mod reparent;
mod schema11; mod schema11;
mod tree; mod tree;
@ -198,56 +199,6 @@ impl Collection {
self.storage.get_deck_id(&machine_name) self.storage.get_deck_id(&machine_name)
} }
pub fn remove_decks_and_child_decks(&mut self, dids: &[DeckId]) -> Result<OpOutput<usize>> {
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<usize> {
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<usize> {
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. /// Apply input delta to deck, and its parents.
/// Caller should ensure transaction. /// Caller should ensure transaction.
pub(crate) fn update_deck_stats( pub(crate) fn update_deck_stats(

55
rslib/src/decks/remove.rs Normal file
View File

@ -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<OpOutput<usize>> {
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<usize> {
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<usize> {
let cids = self.storage.all_cards_in_single_deck(did)?;
self.remove_cards_and_orphaned_notes(&cids)?;
Ok(cids.len())
}
}