From aa820b29c2be0b2b6c946466ca6a274e8771ce4d Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 13 Nov 2022 21:18:48 +0100 Subject: [PATCH] make query optional --- ffplayout-api/src/api/routes.rs | 48 ++++++++++++++++++++------------- ffplayout-api/src/utils/mod.rs | 16 +++++------ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/ffplayout-api/src/api/routes.rs b/ffplayout-api/src/api/routes.rs index 56bf923f..90ee1ed9 100644 --- a/ffplayout-api/src/api/routes.rs +++ b/ffplayout-api/src/api/routes.rs @@ -17,7 +17,7 @@ use argon2::{ password_hash::{rand_core::OsRng, PasswordHash, SaltString}, Argon2, PasswordHasher, PasswordVerifier, }; -use chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone, Utc}; +use chrono::{DateTime, Datelike, Duration, Local, NaiveDateTime, TimeZone, Utc}; use regex::Regex; use serde::{Deserialize, Serialize}; use simplelog::*; @@ -79,16 +79,34 @@ pub struct ImportObj { date: String, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] pub struct ProgramObj { - #[serde(deserialize_with = "naive_date_time_from_str")] - start_after: Option, - #[serde(deserialize_with = "naive_date_time_from_str")] - start_before: Option, + #[serde(default = "def_after", deserialize_with = "naive_date_time_from_str")] + start_after: NaiveDateTime, + #[serde(default = "def_before", deserialize_with = "naive_date_time_from_str")] + start_before: NaiveDateTime, +} + +fn def_after() -> NaiveDateTime { + let today = Utc::now(); + + chrono::Local + .ymd(today.year(), today.month(), today.day()) + .and_hms(0, 0, 0) + .naive_local() +} + +fn def_before() -> NaiveDateTime { + let today = Utc::now(); + + chrono::Local + .ymd(today.year(), today.month(), today.day()) + .and_hms_milli(23, 59, 59, 999) + .naive_local() } #[derive(Debug, Serialize)] -struct ProgramTtem { +struct ProgramItem { source: String, start: String, r#in: f64, @@ -848,16 +866,8 @@ async fn get_program( let start_sec = config.playlist.start_sec.unwrap(); let mut days = 0; let mut program = vec![]; - - let after = match obj.start_after { - Some(d) => d, - None => Utc::now().naive_utc(), - }; - - let before = match obj.start_before { - Some(d) => d, - None => Utc::now().naive_utc(), - }; + let after = obj.start_after; + let before = obj.start_before; if start_sec > time_to_sec(&after.format("%H:%M:%S").to_string()) { days = 1; @@ -879,7 +889,7 @@ async fn get_program( let playlist = match read_playlist(*id, date.clone()).await { Ok(p) => p, Err(e) => { - error!("Error in PLaylist from {date}: {e}"); + error!("Error in Playlist from {date}: {e}"); continue; } }; @@ -895,7 +905,7 @@ async fn get_program( None => item.source, }; - let p_item = ProgramTtem { + let p_item = ProgramItem { source, start: start.format("%Y-%m-%d %H:%M:%S%.3f%:z").to_string(), r#in: item.seek, diff --git a/ffplayout-api/src/utils/mod.rs b/ffplayout-api/src/utils/mod.rs index 4580a5ed..86f21b25 100644 --- a/ffplayout-api/src/utils/mod.rs +++ b/ffplayout-api/src/utils/mod.rs @@ -238,24 +238,20 @@ pub fn local_utc_offset() -> i32 { utc_offset } -pub fn naive_date_time_from_str<'de, D>(deserializer: D) -> Result, D::Error> +pub fn naive_date_time_from_str<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { let s: String = Deserialize::deserialize(deserializer)?; + match NaiveDateTime::parse_from_str(&s, "%Y-%m-%dT%H:%M:%S") { - Ok(date_time) => Ok(Some(date_time)), + Ok(date_time) => Ok(date_time), Err(e) => { if e.kind() == ParseErrorKind::TooShort { - match NaiveDateTime::parse_from_str(&format!("{s}T00:00:00"), "%Y-%m-%dT%H:%M:%S") { - Ok(date_time) => Ok(Some(date_time)), - Err(e) => Err(de::Error::custom(e)), - } + NaiveDateTime::parse_from_str(&format!("{s}T00:00:00"), "%Y-%m-%dT%H:%M:%S") + .map_err(de::Error::custom) } else { - match NaiveDateTime::parse_from_str(&s, "%Y-%m-%dT%H:%M:%S%#z") { - Ok(date_time) => Ok(Some(date_time)), - Err(_) => Err(de::Error::custom(e)), - } + NaiveDateTime::parse_from_str(&s, "%Y-%m-%dT%H:%M:%S%#z").map_err(de::Error::custom) } } }