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,