feat: add ToDelete state to entity and perform deletion while persisting
This commit is contained in:
@@ -41,6 +41,14 @@ fn write_attribute(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn delete_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<(), FailedTo> {
|
||||||
|
let entity_id = [&entity.id];
|
||||||
|
transaction
|
||||||
|
.execute(DELETE_ENTITY_STATEMENT, entity_id)
|
||||||
|
.map_err(|_| sqlite::FailedTo::ExecuteStatement)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<(), FailedTo> {
|
fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<(), FailedTo> {
|
||||||
let entity_id = [&entity.id];
|
let entity_id = [&entity.id];
|
||||||
let entity_values = (&entity.id, &entity.class, entity.ref_date);
|
let entity_values = (&entity.id, &entity.class, entity.ref_date);
|
||||||
@@ -64,9 +72,13 @@ pub fn run(path: &path::Path, catalog: &Catalog) -> result::Result<(), FailedTo>
|
|||||||
for entity in catalog
|
for entity in catalog
|
||||||
.items
|
.items
|
||||||
.values()
|
.values()
|
||||||
.filter(|item| item.state == EntityState::New)
|
.filter(|item| item.state == EntityState::New || item.state == EntityState::ToDelete)
|
||||||
{
|
{
|
||||||
write_entity(entity, &transaction)?;
|
match entity.state {
|
||||||
|
EntityState::New => write_entity(entity, &transaction)?,
|
||||||
|
EntityState::ToDelete => delete_entity(entity, &transaction)?,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
transaction
|
transaction
|
||||||
.commit()
|
.commit()
|
||||||
|
|||||||
@@ -138,6 +138,13 @@ impl O {
|
|||||||
.map(|item| T::from(item.clone()))
|
.map(|item| T::from(item.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn delete(&mut self, id: &str) {
|
||||||
|
let entity = self.items.get_mut(id);
|
||||||
|
if let Some(entity) = entity {
|
||||||
|
entity.state = EntityState::ToDelete;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Persists the current state of the catalog to the database.
|
/// Persists the current state of the catalog to the database.
|
||||||
pub fn persist(&self) -> result::Result<(), FailedTo> {
|
pub fn persist(&self) -> result::Result<(), FailedTo> {
|
||||||
let path = path::Path::new(&self.path);
|
let path = path::Path::new(&self.path);
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ pub enum E {
|
|||||||
New,
|
New,
|
||||||
Unknown,
|
Unknown,
|
||||||
Loaded,
|
Loaded,
|
||||||
|
ToDelete,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user