aboutsummaryrefslogtreecommitdiff
path: root/src/database.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/database.rs')
-rw-r--r--src/database.rs28
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
Feel free to download, copy and edit any repo