From 18d46f7a8550fd0773df6ca838a0caf6f4b657c4 Mon Sep 17 00:00:00 2001 From: jvech Date: Tue, 9 Jul 2024 11:43:29 -0500 Subject: add: note filter implemented --- src/cli.rs | 15 +++++++++++---- src/database.rs | 28 ++++++++++++++++++++-------- src/main.rs | 11 ++++++----- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 61b3d39..5cd8285 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -36,14 +36,21 @@ pub enum Commands { /// View Registered data View { - /// Filter history data (only works with -H) - filter: Option, + /// Filter registers by agent + agent: Option, + /// View register history #[arg(short='H', long, conflicts_with = "total")] history: bool, - /// Transaction accumulation by person + + /// Sum registers by agent #[arg(short, long, conflicts_with = "history")] - total: bool + total: bool, + + /// Filter registers by note (sql match syntax) + #[arg(short='n', long)] + filter_note: Option, + }, /// Add, update or remove an agent diff --git a/src/database.rs b/src/database.rs index e49b5a2..fe20bb2 100644 --- a/src/database.rs +++ b/src/database.rs @@ -67,7 +67,7 @@ impl Database { Ok(()) } - pub fn delete_agent(&self, filepath: &PathBuf) -> Result<()>{ + pub fn delete_agent(&self, filepath: &PathBuf) -> Result<()> { let conn = Connection::open(filepath)?; let prompt = format!("The Agent '{}' will be removed are you sure?", &self.person); @@ -85,8 +85,11 @@ impl Database { Ok(()) } - pub fn view_history(filepath: &PathBuf, filter: Option) -> Result<()> { - let conn = Connection::open(filepath)?; + pub fn view_history( + conn: Connection, + agent_filter: Option, + note_filter: Option) -> Result<()> + { let mut hist_query: String = " SELECT date(r.register_date, 'auto'), a.name, r.amount, r.note FROM Registers r @@ -97,13 +100,23 @@ impl Database { let mut stmt: Statement; let mut regs: Rows; - match filter { - Some(name) => { + match (agent_filter, note_filter) { + (Some(name), Some(note)) => { + hist_query.push_str("WHERE name = ?1 and note LIKE ?2"); + stmt = conn.prepare(hist_query.as_str())?; + regs = stmt.query([name, note])?; + }, + (Some(name), None) => { hist_query.push_str("WHERE name = ?1"); stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([name])?; }, - None => { + (None, Some(note)) => { + hist_query.push_str("WHERE note LIKE ?1"); + stmt = conn.prepare(hist_query.as_str())?; + regs = stmt.query([note])?; + }, + (None, None) => { stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([])?; } @@ -127,8 +140,7 @@ impl Database { Ok(()) } - pub fn view_total(filepath: &PathBuf) -> Result<()> { - let conn = Connection::open(filepath)?; + pub fn view_total(conn: Connection) -> Result<()> { let mut stmt = conn.prepare(" SELECT a.name, sum(r.amount) FROM Registers r diff --git a/src/main.rs b/src/main.rs index 8a4f338..c4acc8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use xdg; -use rusqlite::Result; +use rusqlite::{Result, Connection}; mod database; mod cli; @@ -27,13 +27,14 @@ fn main() -> Result<()> { register.add_register(&datapath)?; }, - Commands::View {history, filter, total} => { + Commands::View {agent, filter_note, history, total} => { let datapath = cli.database_path.expect("database path not found"); + let conn = Connection::open(&datapath)?; match (history, total) { - (true, false) => Database::view_history(&datapath, filter)?, - (false, true) => Database::view_total(&datapath)?, - (false, false) => Database::view_total(&datapath)?, + (true, false) => Database::view_history(conn, agent, filter_note)?, + (false, true) => Database::view_total(conn)?, + (false, false) => Database::view_total(conn)?, (true, true) => unreachable!() } }, -- cgit v1.2.3-70-g09d2