From c9b99b6f3e52f818f80851247b3a55403329dfad Mon Sep 17 00:00:00 2001 From: jvech Date: Tue, 13 Aug 2024 21:53:20 -0500 Subject: feat: month filter added --- src/cli.rs | 4 ++++ src/database.rs | 40 ++++++++++++++++++++++++++++++++-------- src/main.rs | 4 ++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e862034..c4f0a37 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -51,6 +51,10 @@ pub enum Commands { #[arg(short='n', long)] filter_note: Option>, + /// Filter registers by month + #[arg(short='m', long)] + month: Option, + }, /// Add, update or remove an agent diff --git a/src/database.rs b/src/database.rs index 24298ff..8736eb3 100644 --- a/src/database.rs +++ b/src/database.rs @@ -88,7 +88,9 @@ impl Database { pub fn view_history( conn: Connection, agent_filter: Option, - note_filter: Option>) -> Result<()> + note_filter: Option>, + month_filter: Option + ) -> Result<()> { let mut hist_query: String = " SELECT date(r.register_date, 'auto'), a.name, r.amount, r.note @@ -104,9 +106,9 @@ impl Database { if let Some(notes) = note_filter.clone() { let mut i: u32 = 0; for note in notes.iter() { - if i == 0 && agent_filter != None { + if i == 0 && (agent_filter != None || month_filter != None) { filter_note_string.push_str(" AND (note LIKE '"); - } else if i == 0 && agent_filter == None { + } else if i == 0 && (agent_filter == None || month_filter == None) { filter_note_string.push_str(" (note LIKE '"); } else { filter_note_string.push_str(" OR note LIKE '"); @@ -118,25 +120,47 @@ impl Database { filter_note_string.push(')'); } - match (agent_filter, note_filter) { - (Some(name), Some(_)) => { + match (agent_filter, note_filter, month_filter) { + (Some(name), Some(_), Some(month)) => { + hist_query.push_str("WHERE name = ?1 AND strftime('%m',register_date,'auto') = ?2"); + hist_query.push_str(filter_note_string.as_str()); + stmt = conn.prepare(hist_query.as_str())?; + regs = stmt.query([name, month])?; + }, + (Some(name), Some(_), None) => { hist_query.push_str("WHERE name = ?1"); hist_query.push_str(filter_note_string.as_str()); stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([name])?; }, - (Some(name), None) => { + (Some(name), None, Some(month)) => { + hist_query.push_str("WHERE name = ?1 AND strftime('%m',register_date,'auto') = ?2"); + stmt = conn.prepare(hist_query.as_str())?; + regs = stmt.query([name, month])?; + }, + (Some(name), None, None) => { hist_query.push_str("WHERE name = ?1"); stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([name])?; }, - (None, Some(_)) => { + (None, Some(_), Some(month)) => { + hist_query.push_str("WHERE strftime('%m', register_date, 'auto') = ?1"); + hist_query.push_str(filter_note_string.as_str()); + stmt = conn.prepare(hist_query.as_str())?; + regs = stmt.query([month])?; + }, + (None, Some(_), None) => { hist_query.push_str("WHERE"); hist_query.push_str(filter_note_string.as_str()); stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([])?; }, - (None, None) => { + (None, None, Some(month)) => { + hist_query.push_str("WHERE strftime('%m', register_date, 'auto') = ?1"); + stmt = conn.prepare(hist_query.as_str())?; + regs = stmt.query([month])?; + }, + (None, None, None) => { stmt = conn.prepare(hist_query.as_str())?; regs = stmt.query([])?; } diff --git a/src/main.rs b/src/main.rs index c4acc8b..6fda51f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,12 +27,12 @@ fn main() -> Result<()> { register.add_register(&datapath)?; }, - Commands::View {agent, filter_note, history, total} => { + Commands::View {agent, filter_note, history, total, month} => { let datapath = cli.database_path.expect("database path not found"); let conn = Connection::open(&datapath)?; match (history, total) { - (true, false) => Database::view_history(conn, agent, filter_note)?, + (true, false) => Database::view_history(conn, agent, filter_note, month)?, (false, true) => Database::view_total(conn)?, (false, false) => Database::view_total(conn)?, (true, true) => unreachable!() -- cgit v1.2.3-70-g09d2