diff options
author | jvech <jmvalenciae@unal.edu.co> | 2024-07-09 11:43:29 -0500 |
---|---|---|
committer | jvech <jmvalenciae@unal.edu.co> | 2024-07-09 11:43:29 -0500 |
commit | 18d46f7a8550fd0773df6ca838a0caf6f4b657c4 (patch) | |
tree | 2d93cb2740450d5c7101359184b745329c83d695 /src/database.rs | |
parent | 5c9abec6cb729365439a7f783d63dfc32eeb2955 (diff) |
add: note filter implemented
Diffstat (limited to 'src/database.rs')
-rw-r--r-- | src/database.rs | 28 |
1 files changed, 20 insertions, 8 deletions
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<String>) -> Result<()> { - let conn = Connection::open(filepath)?; + pub fn view_history( + conn: Connection, + agent_filter: Option<String>, + note_filter: Option<String>) -> 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 |