From eb0c5d947a2e2755fac4a9b34d9dee6c2987afbb Mon Sep 17 00:00:00 2001 From: Dawid Rycerz Date: Mon, 14 Jul 2025 20:52:55 +0300 Subject: feat: Add dockerfile and docker-compose --- src/weather_thresholds.rs | 57 +++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'src/weather_thresholds.rs') 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, } +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, +} + 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, + input: WeatherThresholdUpdateInput, ) -> Result { 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())); } -- cgit v1.2.3