diff options
Diffstat (limited to 'src/handlers.rs')
| -rw-r--r-- | src/handlers.rs | 26 |
1 files changed, 3 insertions, 23 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 50ae9e2..3c38724 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -3,7 +3,6 @@ use axum::{ http::{HeaderName, StatusCode, header}, response::Json, }; -use chrono::NaiveTime; use sqlx::PgPool; use strum::IntoEnumIterator; use uuid::Uuid; @@ -27,30 +26,11 @@ pub async fn list_measurements( None => None, _ => return Err(AppError::BadRequest("limit must be positive")), }; - let (from, to) = match (query.date, query.from, query.to) { - (Some(date), None, None) => (Some(date), Some(date)), - (None, from, to) => (from, to), - _ => { - return Err(AppError::BadRequest( - "date can't be combined with from or to", - )); - } - }; - if let (Some(from), Some(to)) = (from, to) + if let (Some(from), Some(to)) = (query.from, query.to) && from > to { return Err(AppError::BadRequest("from must not be after to")); } - let start = from.map(|from| from.and_time(NaiveTime::MIN).and_utc()); - let end = match to { - Some(to) => { - let Some(next_day) = to.succ_opt() else { - return Err(AppError::BadRequest("to is out of range")); - }; - Some(next_day.and_time(NaiveTime::MIN).and_utc()) - } - None => None, - }; // SQL can't bind ASC/DESC as a parameter. The CASE yields measured_at only when // `order` is desc (sorted DESC) and NULL otherwise, so that key ties for every // row and the ASC key decides the order. If this gets slow, use an if/else with a @@ -65,8 +45,8 @@ pub async fn list_measurements( measured_at ASC LIMIT $3 ", - start, - end, + query.from, + query.to, limit, matches!(query.order, Order::Desc) ) |
