From 1ba5444d7d90c0706e168149bb5abb3cd5329298 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Sat, 9 Apr 2022 12:31:23 +0200 Subject: [PATCH] Make adding notetype with id undoable --- rslib/src/import_export/package/apkg/import.rs | 8 +++++--- rslib/src/notetype/undo.rs | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/rslib/src/import_export/package/apkg/import.rs b/rslib/src/import_export/package/apkg/import.rs index 8e0711acf..5c381c44e 100644 --- a/rslib/src/import_export/package/apkg/import.rs +++ b/rslib/src/import_export/package/apkg/import.rs @@ -204,10 +204,12 @@ impl<'a> Context<'a> { } fn add_notetype(&mut self, notetype: &mut Notetype) -> Result<()> { - notetype.usn = self.usn; - // TODO: make undoable + notetype.prepare_for_update(None, true)?; self.target_col - .add_or_update_notetype_with_existing_id_inner(notetype, None, self.usn, true) + .ensure_notetype_name_unique(notetype, self.usn)?; + notetype.usn = self.usn; + self.target_col + .add_notetype_with_unique_id_undoable(notetype) } fn update_notetype(&mut self, notetype: &mut Notetype, original: Notetype) -> Result<()> { diff --git a/rslib/src/notetype/undo.rs b/rslib/src/notetype/undo.rs index 74f6d34a6..5780ed9b2 100644 --- a/rslib/src/notetype/undo.rs +++ b/rslib/src/notetype/undo.rs @@ -42,6 +42,17 @@ impl Collection { Ok(()) } + /// Caller must ensure [NotetypeId] is unique. + pub(crate) fn add_notetype_with_unique_id_undoable( + &mut self, + notetype: &Notetype, + ) -> Result<()> { + self.storage + .add_or_update_notetype_with_existing_id(notetype)?; + self.save_undo(UndoableNotetypeChange::Added(Box::new(notetype.clone()))); + Ok(()) + } + pub(super) fn update_notetype_undoable( &mut self, notetype: &Notetype,