commands erweitert

This commit is contained in:
Damian Wessels
2025-05-18 11:03:18 +02:00
parent a6430a98f8
commit eccd27ff6a
5 changed files with 97 additions and 2 deletions

View File

@@ -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"

35
src/commands/edit.rs Normal file
View File

@@ -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<usize>,
start: &Option<String>,
end: &Option<String>,
move_to: &Option<String>,
notes: &Option<String>,
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(())
}

View File

@@ -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;
pub use lists::list_sheets;
pub use edit::edit_task;

View File

@@ -195,4 +195,27 @@ pub fn get_all_sheets(db: &Connection) -> Result<Vec<String>> {
sheets.push(sheet?);
}
Ok(sheets)
}
pub fn get_entry_by_id(id: &usize, db: &Connection) -> Result<Option<Entry>> {
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::<usize, Option<String>>(3)?
.map(|t| str_to_datetime(&t).unwrap());
let start = str_to_datetime(&row.get::<usize, String>(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"))
}

View File

@@ -60,8 +60,31 @@ enum SubCommands {
/// The timesehet to display, or the current one
sheet: Option<String>,
},
/// Change timesheet
Sheet {
name: String,
#[arg(short, long)]
rename: Option<String>
},
/// List available timesheet
List,
/// Edit a task
Edit {
/// The ID of the Task to edit
#[arg(short, long)]
id: Option<usize>,
/// Set a new start date and time for this task
#[arg(short, long)]
start: Option<String>,
/// Set a new end date and time for this task
#[arg(short, long)]
end: Option<String>,
/// Move this task to a different timesheet
#[arg(short, long)]
move_to: Option<String>,
/// The new task description
notes: Option<String>,
},
/// 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(())