Add notification to controllers on schedule change
This commit is contained in:
		
							parent
							
								
									3b00001859
								
							
						
					
					
						commit
						dd850766fd
					
				
					 4 changed files with 59 additions and 3 deletions
				
			
		
							
								
								
									
										12
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										12
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							| 
						 | 
					@ -729,6 +729,7 @@ dependencies = [
 | 
				
			||||||
 "chrono",
 | 
					 "chrono",
 | 
				
			||||||
 "emgauwa-lib",
 | 
					 "emgauwa-lib",
 | 
				
			||||||
 "futures",
 | 
					 "futures",
 | 
				
			||||||
 | 
					 "itertools 0.12.1",
 | 
				
			||||||
 "log",
 | 
					 "log",
 | 
				
			||||||
 "serde",
 | 
					 "serde",
 | 
				
			||||||
 "serde_derive",
 | 
					 "serde_derive",
 | 
				
			||||||
| 
						 | 
					@ -1181,6 +1182,15 @@ dependencies = [
 | 
				
			||||||
 "either",
 | 
					 "either",
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "itertools"
 | 
				
			||||||
 | 
					version = "0.12.1"
 | 
				
			||||||
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
 | 
					checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
 | 
				
			||||||
 | 
					dependencies = [
 | 
				
			||||||
 | 
					 "either",
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "itoa"
 | 
					name = "itoa"
 | 
				
			||||||
version = "1.0.9"
 | 
					version = "1.0.9"
 | 
				
			||||||
| 
						 | 
					@ -1998,7 +2008,7 @@ version = "0.2.2"
 | 
				
			||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
checksum = "6b7b278788e7be4d0d29c0f39497a0eef3fba6bbc8e70d8bf7fde46edeaa9e85"
 | 
					checksum = "6b7b278788e7be4d0d29c0f39497a0eef3fba6bbc8e70d8bf7fde46edeaa9e85"
 | 
				
			||||||
dependencies = [
 | 
					dependencies = [
 | 
				
			||||||
 "itertools",
 | 
					 "itertools 0.11.0",
 | 
				
			||||||
 "nom",
 | 
					 "nom",
 | 
				
			||||||
 "unicode_categories",
 | 
					 "unicode_categories",
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,6 +19,7 @@ log = "0.4"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
chrono = { version = "0.4", features = ["serde"] }
 | 
					chrono = { version = "0.4", features = ["serde"] }
 | 
				
			||||||
uuid = { version = "1.5", features = ["serde", "v4"] }
 | 
					uuid = { version = "1.5", features = ["serde", "v4"] }
 | 
				
			||||||
 | 
					itertools = "0.12"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
serde = "1.0"
 | 
					serde = "1.0"
 | 
				
			||||||
serde_json = "1.0"
 | 
					serde_json = "1.0"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,18 @@
 | 
				
			||||||
 | 
					use actix::Addr;
 | 
				
			||||||
use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
					use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
				
			||||||
use emgauwa_lib::db::{DbSchedule, DbTag};
 | 
					use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbSchedule, DbTag};
 | 
				
			||||||
use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
 | 
					use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError};
 | 
				
			||||||
use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule};
 | 
					use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule};
 | 
				
			||||||
use emgauwa_lib::types::{RequestCreateSchedule, RequestUpdateSchedule, ScheduleUid};
 | 
					use emgauwa_lib::types::{
 | 
				
			||||||
 | 
						ControllerWsAction, RequestCreateSchedule, RequestUpdateSchedule, ScheduleUid,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					use itertools::Itertools;
 | 
				
			||||||
use sqlx::pool::PoolConnection;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::{Pool, Sqlite};
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::app_state;
 | 
				
			||||||
 | 
					use crate::app_state::AppState;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[get("/schedules")]
 | 
					#[get("/schedules")]
 | 
				
			||||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
 | 
					pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
	let mut pool_conn = pool.acquire().await?;
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
| 
						 | 
					@ -116,6 +123,7 @@ pub async fn add_list(
 | 
				
			||||||
#[put("/schedules/{schedule_id}")]
 | 
					#[put("/schedules/{schedule_id}")]
 | 
				
			||||||
pub async fn update(
 | 
					pub async fn update(
 | 
				
			||||||
	pool: web::Data<Pool<Sqlite>>,
 | 
						pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
						app_state: web::Data<Addr<AppState>>,
 | 
				
			||||||
	path: web::Path<(String,)>,
 | 
						path: web::Path<(String,)>,
 | 
				
			||||||
	data: web::Json<RequestUpdateSchedule>,
 | 
						data: web::Json<RequestUpdateSchedule>,
 | 
				
			||||||
) -> Result<HttpResponse, EmgauwaError> {
 | 
					) -> Result<HttpResponse, EmgauwaError> {
 | 
				
			||||||
| 
						 | 
					@ -144,6 +152,26 @@ pub async fn update(
 | 
				
			||||||
		schedule.set_tags(&mut pool_conn, tags.as_slice()).await?;
 | 
							schedule.set_tags(&mut pool_conn, tags.as_slice()).await?;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let controller_ids: Vec<i64> = DbJunctionRelaySchedule::get_relays(&mut pool_conn, &schedule)
 | 
				
			||||||
 | 
							.await?
 | 
				
			||||||
 | 
							.into_iter()
 | 
				
			||||||
 | 
							.map(|r| r.controller_id)
 | 
				
			||||||
 | 
							.unique()
 | 
				
			||||||
 | 
							.collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for controller_id in controller_ids {
 | 
				
			||||||
 | 
							let controller = DbController::get(&mut pool_conn, controller_id)
 | 
				
			||||||
 | 
								.await?
 | 
				
			||||||
 | 
								.ok_or(DatabaseError::NotFound)?;
 | 
				
			||||||
 | 
							app_state
 | 
				
			||||||
 | 
								.send(app_state::Action {
 | 
				
			||||||
 | 
									controller_uid: controller.uid,
 | 
				
			||||||
 | 
									action: ControllerWsAction::Schedules(vec![schedule.clone()]),
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								.await??;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?;
 | 
						let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?;
 | 
				
			||||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
						Ok(HttpResponse::Ok().json(return_schedule))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,6 +45,23 @@ impl DbJunctionRelaySchedule {
 | 
				
			||||||
		.map_err(DatabaseError::from)
 | 
							.map_err(DatabaseError::from)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get_relays(
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							schedule: &DbSchedule,
 | 
				
			||||||
 | 
						) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
				
			||||||
 | 
							sqlx::query_as!(
 | 
				
			||||||
 | 
								DbRelay,
 | 
				
			||||||
 | 
								r#"SELECT relays.* FROM relays INNER JOIN junction_relay_schedule
 | 
				
			||||||
 | 
								ON junction_relay_schedule.relay_id = relays.id
 | 
				
			||||||
 | 
								WHERE junction_relay_schedule.schedule_id = ?
 | 
				
			||||||
 | 
								ORDER BY junction_relay_schedule.weekday"#,
 | 
				
			||||||
 | 
								schedule.id
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							.fetch_all(conn.deref_mut())
 | 
				
			||||||
 | 
							.await
 | 
				
			||||||
 | 
							.map_err(DatabaseError::from)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pub async fn get_schedule(
 | 
						pub async fn get_schedule(
 | 
				
			||||||
		conn: &mut PoolConnection<Sqlite>,
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
		relay: &DbRelay,
 | 
							relay: &DbRelay,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue