From eccd27ff6a29aa4ac8bb5f8b4845c453a21d5368 Mon Sep 17 00:00:00 2001 From: Damian Wessels Date: Sun, 18 May 2025 11:03:18 +0200 Subject: [PATCH] commands erweitert --- Cargo.toml | 3 ++- src/commands/edit.rs | 35 +++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 4 +++- src/database.rs | 23 +++++++++++++++++++++++ src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/commands/edit.rs diff --git a/Cargo.toml b/Cargo.toml index 12e5ce7..f3cef41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "timetracker" -version = "0.1.0" +version = "0.1.1" edition = "2021" +authors = ["Damian Wessels"] [dependencies] anyhow = "1.0.98" diff --git a/src/commands/edit.rs b/src/commands/edit.rs new file mode 100644 index 0000000..0a3a20a --- /dev/null +++ b/src/commands/edit.rs @@ -0,0 +1,35 @@ +use anyhow::Result; +use crate::database::{get_entry_by_id, running_entry}; +use crate::{Entry, State}; +use crate::style::{style_string, Styles}; + +pub fn edit_task( + id: &Option, + start: &Option, + end: &Option, + move_to: &Option, + notes: &Option, + state: &mut State, +) -> Result<()> { + let running_entry = running_entry(&state.database,&state.current_sheet)?; + + let entry = if let Some(id) = id { + get_entry_by_id(id,&state.database)? + } else { + None + }; + + if running_entry.is_none() && entry.is_none() { + println!( + "{}", + style_string("The task was not found. Either the given id is invalid or there is no task running", Styles::Message) + ); + return Ok(()); + } + + let mut entry = entry.unwrap_or_else(|| running_entry.unwrap()); + + + + Ok(()) +} \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9071a65..e773859 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,8 +2,10 @@ mod current; mod in_cmd; mod out; mod lists; +mod edit; pub use current::current_task; pub use in_cmd::start_task; pub use out::stop_task; -pub use lists::list_sheets; \ No newline at end of file +pub use lists::list_sheets; +pub use edit::edit_task; \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index c8d211a..ff71988 100644 --- a/src/database.rs +++ b/src/database.rs @@ -195,4 +195,27 @@ pub fn get_all_sheets(db: &Connection) -> Result> { sheets.push(sheet?); } Ok(sheets) +} + +pub fn get_entry_by_id(id: &usize, db: &Connection) -> Result> { + let query = "SELECT id, note, start, end, sheet FROM entries WHERE id = ?;"; + let mut stmt = db.prepare(query)?; + let mut entries = stmt.query_map([id], |row| { + let end = row + .get::>(3)? + .map(|t| str_to_datetime(&t).unwrap()); + + let start = str_to_datetime(&row.get::(2)?).unwrap(); + + Ok(Entry { + id: row.get(0)?, + name: row.get(1)?, + start, + end, + sheet: row.get(4)?, + }) + })?; + + let entry = entries.next(); + entry.transpose().context(format!("Failed to read entry")) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3233993..9a99a9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,8 +60,31 @@ enum SubCommands { /// The timesehet to display, or the current one sheet: Option, }, + /// Change timesheet + Sheet { + name: String, + #[arg(short, long)] + rename: Option + }, /// List available timesheet List, + /// Edit a task + Edit { + /// The ID of the Task to edit + #[arg(short, long)] + id: Option, + /// Set a new start date and time for this task + #[arg(short, long)] + start: Option, + /// Set a new end date and time for this task + #[arg(short, long)] + end: Option, + /// Move this task to a different timesheet + #[arg(short, long)] + move_to: Option, + /// The new task description + notes: Option, + }, /// Shows the active task for the current sheet Current, } @@ -103,12 +126,23 @@ fn cli() -> Result<()> { filter_by_date, sheet, } => {} + SubCommands::Sheet {name, rename} => {} SubCommands::List => { list_sheets(&state).context("Could not list sheets")?; } SubCommands::Current => { current_task(&state).context("could not get current task")?; } + SubCommands::Edit { + id, + start, + end, + move_to, + notes + } => { + edit_task(id,start,end,move_to,notes, &mut state).context("Could not edit task")?; + } + } Ok(())