Also delete removed entries

This commit is contained in:
Thomas Avé 2024-11-03 21:15:13 +01:00
parent cf1736f9e2
commit dd37800f0a
1 changed files with 17 additions and 0 deletions

View File

@ -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::<Vec<i32>>();
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(())
}