diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-14 20:52:55 +0300 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-14 20:52:55 +0300 |
| commit | eb0c5d947a2e2755fac4a9b34d9dee6c2987afbb (patch) | |
| tree | 6c423fe456a3cee24e292ee24b609b08dc6704e4 /src/weather_thresholds.rs | |
| parent | 1c2873b3059f3e4d6bd02307ec5b22f761ce1c80 (diff) | |
feat: Add dockerfile and docker-compose
Diffstat (limited to 'src/weather_thresholds.rs')
| -rw-r--r-- | src/weather_thresholds.rs | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/weather_thresholds.rs b/src/weather_thresholds.rs index dadfeda..36237df 100644 --- a/src/weather_thresholds.rs +++ b/src/weather_thresholds.rs @@ -19,6 +19,16 @@ pub struct WeatherThreshold { pub description: Option<String>, } +pub struct WeatherThresholdUpdateInput { + pub id: i64, + pub user_id: i64, + pub condition_type: String, + pub threshold_value: f64, + pub operator: String, + pub enabled: bool, + pub description: Option<String>, +} + pub struct WeatherThresholdRepository<'a> { pub db: &'a sqlx::SqlitePool, } @@ -74,24 +84,18 @@ impl<'a> WeatherThresholdRepository<'a> { pub async fn update_threshold( &self, - id: i64, - user_id: i64, - condition_type: String, - threshold_value: f64, - operator: String, - enabled: bool, - description: Option<String>, + input: WeatherThresholdUpdateInput, ) -> Result<WeatherThreshold, sqlx::Error> { sqlx::query_as::<_, WeatherThreshold>( "UPDATE weather_thresholds SET condition_type = ?, threshold_value = ?, operator = ?, enabled = ?, description = ? WHERE id = ? AND user_id = ? RETURNING id, user_id, condition_type, threshold_value, operator, enabled, description" ) - .bind(condition_type) - .bind(threshold_value) - .bind(operator) - .bind(enabled) - .bind(description) - .bind(id) - .bind(user_id) + .bind(input.condition_type) + .bind(input.threshold_value) + .bind(input.operator) + .bind(input.enabled) + .bind(input.description) + .bind(input.id) + .bind(input.user_id) .fetch_one(self.db) .await } @@ -131,10 +135,9 @@ pub async fn list_thresholds( mod tests { use super::*; use crate::users::{UserRepository, UserRole}; - use axum::extract::{Json, State}; - use hyper::StatusCode; + use sqlx::{Executor, SqlitePool}; - use std::sync::Arc; + use tokio; async fn setup_db() -> SqlitePool { @@ -191,7 +194,7 @@ mod tests { assert_eq!(fetched.condition_type, "wind_speed"); assert_eq!(fetched.threshold_value, 10.0); assert_eq!(fetched.operator, ">"); - assert_eq!(fetched.enabled, true); + assert!(fetched.enabled); assert_eq!(fetched.description, Some("desc".to_string())); } @@ -212,21 +215,21 @@ mod tests { .await .unwrap(); let updated = repo - .update_threshold( - th.id, + .update_threshold(WeatherThresholdUpdateInput { + id: th.id, user_id, - "rain".to_string(), - 5.0, - "<".to_string(), - false, - Some("rain desc".to_string()), - ) + condition_type: "rain".to_string(), + threshold_value: 5.0, + operator: "<".to_string(), + enabled: false, + description: Some("rain desc".to_string()), + }) .await .unwrap(); assert_eq!(updated.condition_type, "rain"); assert_eq!(updated.threshold_value, 5.0); assert_eq!(updated.operator, "<"); - assert_eq!(updated.enabled, false); + assert!(!updated.enabled); assert_eq!(updated.description, Some("rain desc".to_string())); } |
