diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index ccda0fd2b..c02c4179b 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -960,9 +960,8 @@ impl Backend { }) } - fn remove_notetype(&self, _id: i64) -> Result<()> { - println!("fixme: remove notetype"); - Ok(()) + fn remove_notetype(&self, id: i64) -> Result<()> { + self.with_col(|col| col.remove_notetype(NoteTypeID(id))) } fn new_note(&self, ntid: i64) -> Result { diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index 5a2bb5411..3fc48ca1d 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -375,4 +375,13 @@ impl Collection { }) .collect() } + + pub fn remove_notetype(&mut self, ntid: NoteTypeID) -> Result<()> { + // fixme: currently the storage layer is taking care of removing the notes and cards, + // but we need to do it in this layer in the future for undo handling + self.transact(None, |col| { + col.storage.set_schema_modified()?; + col.storage.remove_notetype(ntid) + }) + } } diff --git a/rslib/src/storage/notetype/mod.rs b/rslib/src/storage/notetype/mod.rs index 6cf875727..0d4effc3e 100644 --- a/rslib/src/storage/notetype/mod.rs +++ b/rslib/src/storage/notetype/mod.rs @@ -205,6 +205,26 @@ impl SqliteStorage { Ok(()) } + pub(crate) fn remove_notetype(&self, ntid: NoteTypeID) -> Result<()> { + self.db + .prepare_cached("delete from cards where nid in (select id from notes where mid=?)")? + .execute(&[ntid])?; + self.db + .prepare_cached("delete from notes where mid=?")? + .execute(&[ntid])?; + self.db + .prepare_cached("delete from templates where ntid=?")? + .execute(&[ntid])?; + self.db + .prepare_cached("delete from fields where ntid=?")? + .execute(&[ntid])?; + self.db + .prepare_cached("delete from notetypes where id=?")? + .execute(&[ntid])?; + + Ok(()) + } + pub(crate) fn move_cards_for_repositioned_templates( &self, ntid: NoteTypeID,