51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
use anyhow::Result;
|
|
use std::path::PathBuf;
|
|
|
|
use crate::config;
|
|
use crate::services::error::CoreError;
|
|
use crate::services::item_service::ItemService;
|
|
use clap::error::ErrorKind;
|
|
use clap::Command;
|
|
use log::warn;
|
|
use rusqlite::Connection;
|
|
|
|
pub fn mode_delete(
|
|
cmd: &mut Command,
|
|
_settings: &config::Settings,
|
|
_config: &config::Settings,
|
|
ids: &mut Vec<i64>,
|
|
tags: &mut Vec<String>,
|
|
conn: &mut Connection,
|
|
data_path: PathBuf,
|
|
) -> Result<()> {
|
|
if ids.is_empty() {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"No ID given, you must supply atleast one ID when using --delete",
|
|
)
|
|
.exit();
|
|
} else if !tags.is_empty() {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"Tags given but not supported, you must supply atleast one ID when using --delete",
|
|
)
|
|
.exit();
|
|
}
|
|
|
|
let item_service = ItemService::new(data_path);
|
|
|
|
for item_id in ids.iter() {
|
|
match item_service.delete_item(conn, *item_id) {
|
|
Ok(_) => {}
|
|
Err(e) => match e {
|
|
CoreError::ItemNotFound(_) => {
|
|
warn!("Unable to find item {item_id} in database");
|
|
}
|
|
_ => return Err(anyhow::Error::from(e).context(format!("Failed to delete item {}", item_id))),
|
|
},
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|