From dd37800f0a9e681ee6c453f985bef475cebcd180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Sun, 3 Nov 2024 21:15:13 +0100 Subject: [PATCH] Also delete removed entries --- src/commands.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/commands.rs b/src/commands.rs index da2fe39..c2756bc 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -127,6 +127,18 @@ pub async fn update_period(settings: &Settings, period: WorkPeriod) -> Result<() Ok(()) } +pub async fn delete_period(settings: &Settings, id: i32) -> Result<(), reqwest::Error> { + let client = Client::new(); + let response = client.delete(settings.url.to_string() + "/api/history/" + id.to_string().as_str()) + .send() + .await?; + + if !response.status().is_success() { + println!("{:?}", response.text().await); + } + Ok(()) +} + pub async fn edit(settings: Settings, since: Option<&String>, until: Option<&String>, num: Option<&String>) -> Result<(), reqwest::Error> { let mut params = vec![ ("project", settings.project.to_owned()), @@ -152,10 +164,15 @@ pub async fn edit(settings: Settings, since: Option<&String>, until: Option<&Str } else { let body = response.text().await.unwrap(); let periods = parse_periods(body).unwrap(); + let mut ids = periods.iter().map(|p| p.id).collect::>(); let res = edit_periods(periods).unwrap(); for period in res { + ids.remove(ids.iter().position(|&x| x == period.id).unwrap()); update_period(&settings, period).await.unwrap(); } + for id in ids { + delete_period(&settings, id).await.unwrap(); + } } Ok(()) }