Allow specifying start date for grocery list
This commit is contained in:
parent
63860cd05a
commit
f101168f8e
2 changed files with 30 additions and 6 deletions
|
@ -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?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue