Add notification to controllers on schedule change
This commit is contained in:
parent
3b00001859
commit
dd850766fd
4 changed files with 48 additions and 2 deletions
BIN
Cargo.lock
generated
BIN
Cargo.lock
generated
Binary file not shown.
|
@ -19,6 +19,7 @@ log = "0.4"
|
|||
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
uuid = { version = "1.5", features = ["serde", "v4"] }
|
||||
itertools = "0.12"
|
||||
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
|
|
@ -1,11 +1,18 @@
|
|||
use actix::Addr;
|
||||
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::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, Sqlite};
|
||||
|
||||
use crate::app_state;
|
||||
use crate::app_state::AppState;
|
||||
|
||||
#[get("/schedules")]
|
||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
@ -116,6 +123,7 @@ pub async fn add_list(
|
|||
#[put("/schedules/{schedule_id}")]
|
||||
pub async fn update(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
app_state: web::Data<Addr<AppState>>,
|
||||
path: web::Path<(String,)>,
|
||||
data: web::Json<RequestUpdateSchedule>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
|
@ -144,6 +152,26 @@ pub async fn update(
|
|||
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)?;
|
||||
Ok(HttpResponse::Ok().json(return_schedule))
|
||||
}
|
||||
|
|
|
@ -45,6 +45,23 @@ impl DbJunctionRelaySchedule {
|
|||
.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(
|
||||
conn: &mut PoolConnection<Sqlite>,
|
||||
relay: &DbRelay,
|
||||
|
|
Loading…
Reference in a new issue