Allow specifying start date for grocery list

This commit is contained in:
Matteo Settenvini 2022-08-12 23:56:37 +02:00
parent 63860cd05a
commit f101168f8e
Signed by: matteo
GPG key ID: 8576CC1AD97D42DF
2 changed files with 30 additions and 6 deletions

View file

@ -5,7 +5,7 @@ use {
crate::api_client::ApiClient,
crate::recipe::{Ingredient, Recipe},
anyhow::{anyhow, Result},
chrono::{Duration, Local},
chrono::{Duration, Local, NaiveDate},
icalendar::Component,
regex::Regex,
reqwest::{Method, StatusCode, Url},
@ -17,9 +17,10 @@ pub async fn with(
api_client: &ApiClient,
calendar_name: &str,
location: &str,
start_date: Option<NaiveDate>,
days: u32,
) -> Result<()> {
let ids = map_events_to_recipe_ids(api_client, calendar_name, days).await?;
let ids = map_events_to_recipe_ids(api_client, calendar_name, start_date, days).await?;
let ingredients = get_ingredients(api_client, ids).await?;
let ingredients = merge_ingredients(ingredients);
let md = prepare_grocery_list(&ingredients)?;
@ -31,11 +32,21 @@ pub async fn with(
async fn map_events_to_recipe_ids(
api_client: &ApiClient,
calendar_name: &str,
start_date: Option<NaiveDate>,
days: u32,
) -> Result<HashSet<usize>> {
use chrono::{NaiveDateTime, NaiveTime, TimeZone};
let start = start_date
.map(|d| {
let day_start = NaiveDateTime::new(d, NaiveTime::from_hms(0, 0, 0));
Local.from_local_datetime(&day_start).unwrap()
})
.unwrap_or_else(|| Local::now());
let date_range = Range {
start: Local::now(),
end: Local::now() + Duration::days(days as i64),
start,
end: start + Duration::days(days as i64),
};
let all_events = api_client.get_events(calendar_name, date_range).await?;