use crate::{print_err, print_info, print_ok, print_warn, tr_with, write_log};
use colored::*;
use rusqlite::Connection;
use std::io::{self, Write};
pub fn handle_del_book(conn: &Connection, key: &str, force: bool) -> rusqlite::Result<()> {
println!();
let is_isbn = key.len() >= 10 && !key.chars().all(|c| c.is_ascii_digit());
let exists_query = if is_isbn {
"SELECT COUNT(*) FROM books WHERE isbn = ?1"
} else {
"SELECT COUNT(*) FROM books WHERE id = ?1"
};
let exists: i64 = conn.query_row(exists_query, [key], |row| row.get(0))?;
if exists == 0 {
print_warn(&tr_with("del.book.not_found", &[("key", key)]).yellow());
return Ok(());
}
if !force {
print!(
"{} ",
tr_with("del.book.confirm", &[("key", key)]) );
io::stdout().flush().unwrap();
let mut answer = String::new();
io::stdin().read_line(&mut answer).unwrap();
if !matches!(answer.trim().to_lowercase().as_str(), "y" | "yes") {
print_info(&tr_with("del.book.cancelled", &[("key", key)]).cyan(), true);
return Ok(());
}
println!();
}
let delete_sql = if is_isbn {
"DELETE FROM books WHERE isbn = ?1"
} else {
"DELETE FROM books WHERE id = ?1"
};
let affected = conn.execute(delete_sql, [key])?;
if affected > 0 {
let action_type = if force { "forced" } else { "confirmed" };
let log_msg = format!("Book {} deleted ({})", key, action_type);
if let Err(e) = write_log(conn, "DELETE_BOOK", "", &log_msg) {
print_err(
&tr_with(
"log.record.unable_to_write",
&[("log_error", &e.to_string())],
)
.red()
.bold(),
);
}
print_ok(&tr_with("del.book.success", &[("key", key)]).green(), true);
} else {
print_err(&tr_with("del.book.not_found", &[("key", key)]).red().bold());
}
Ok(())
}