aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cli.rs15
-rw-r--r--src/database.rs28
-rw-r--r--src/main.rs11
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<String>,
+ /// Filter registers by agent
+ agent: Option<String>,
+
/// 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<String>,
+
},
/// 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<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
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!()
}
},
Feel free to download, copy and edit any repo