From 0d87baece7df9dfb40969a5b34c5cf944967aba0 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Mon, 14 Nov 2022 07:57:43 +0100 Subject: [PATCH] fix range, when start_after is behind start_before --- docs/api.md | 14 ++++++++++++++ ffplayout-api/src/api/routes.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/api.md b/docs/api.md index b454691a..18608a18 100644 --- a/docs/api.md +++ b/docs/api.md @@ -339,8 +339,22 @@ curl -X PUT http://127.0.0.1:8787/api/file/1/import/ -H 'Authorization: Bearer < Get program infos about given date, or current day +Examples: + +* get program from current day +```BASH +curl -X GET http://127.0.0.1:8787/program/1/ -H 'Authorization: Bearer ' +``` + +* get a program range between two dates ```BASH curl -X GET http://127.0.0.1:8787/program/1/?start_after=2022-11-13T12:00:00&start_before=2022-11-20T11:59:59 \ -H 'Authorization: Bearer ' ``` +* get program from give day +```BASH +curl -X GET http://127.0.0.1:8787/program/1/?start_after=2022-11-13T10:00:00 \ +-H 'Authorization: Bearer ' +``` + diff --git a/ffplayout-api/src/api/routes.rs b/ffplayout-api/src/api/routes.rs index 2668603d..a8b37762 100644 --- a/ffplayout-api/src/api/routes.rs +++ b/ffplayout-api/src/api/routes.rs @@ -81,13 +81,13 @@ pub struct ImportObj { #[derive(Debug, Deserialize, Clone)] pub struct ProgramObj { - #[serde(default = "def_after", deserialize_with = "naive_date_time_from_str")] + #[serde(default = "time_after", deserialize_with = "naive_date_time_from_str")] start_after: NaiveDateTime, - #[serde(default = "def_before", deserialize_with = "naive_date_time_from_str")] + #[serde(default = "time_before", deserialize_with = "naive_date_time_from_str")] start_before: NaiveDateTime, } -fn def_after() -> NaiveDateTime { +fn time_after() -> NaiveDateTime { let today = Utc::now(); chrono::Local @@ -96,7 +96,7 @@ fn def_after() -> NaiveDateTime { .naive_local() } -fn def_before() -> NaiveDateTime { +fn time_before() -> NaiveDateTime { let today = Utc::now(); chrono::Local @@ -852,10 +852,24 @@ async fn import_playlist( /// /// Get program infos about given date, or current day /// +/// Examples: +/// +/// * get program from current day +/// ```BASH +/// curl -X GET http://127.0.0.1:8787/program/1/ -H 'Authorization: Bearer ' +/// ``` +/// +/// * get a program range between two dates /// ```BASH /// curl -X GET http://127.0.0.1:8787/program/1/?start_after=2022-11-13T12:00:00&start_before=2022-11-20T11:59:59 \ /// -H 'Authorization: Bearer ' /// ``` +/// +/// * get program from give day +/// ```BASH +/// curl -X GET http://127.0.0.1:8787/program/1/?start_after=2022-11-13T10:00:00 \ +/// -H 'Authorization: Bearer ' +/// ``` #[get("/program/{id}/")] #[has_any_role("Role::Admin", "Role::User", type = "Role")] async fn get_program( @@ -867,7 +881,14 @@ async fn get_program( let mut days = 0; let mut program = vec![]; let after = obj.start_after; - let before = obj.start_before; + let mut before = obj.start_before; + + if after > before { + before = chrono::Local + .ymd(after.year(), after.month(), after.day()) + .and_hms_milli(23, 59, 59, 999) + .naive_local() + } if start_sec > time_to_sec(&after.format("%H:%M:%S").to_string()) { days = 1;