diff --git a/emgauwa-core/src/handlers/v1/relays.rs b/emgauwa-core/src/handlers/v1/relays.rs index 4cfebd6..d536e97 100644 --- a/emgauwa-core/src/handlers/v1/relays.rs +++ b/emgauwa-core/src/handlers/v1/relays.rs @@ -136,6 +136,8 @@ pub async fn update_for_controller( relay.set_tags(&mut pool_conn, tags.as_slice()).await?; } + let relay = relay.reload(&mut pool_conn).await?; + let return_relay = Relay::from_db_model(&mut pool_conn, relay)?; app_server diff --git a/emgauwa-lib/src/db/junction_relay_schedule.rs b/emgauwa-lib/src/db/junction_relay_schedule.rs index a090604..128490b 100644 --- a/emgauwa-lib/src/db/junction_relay_schedule.rs +++ b/emgauwa-lib/src/db/junction_relay_schedule.rs @@ -29,17 +29,15 @@ impl DbJunctionRelaySchedule { .map_err(DatabaseError::from) } - pub async fn get_junction( + pub async fn get_junction_by_relay_and_weekday( conn: &mut PoolConnection, relay: &DbRelay, - schedule: &DbSchedule, weekday: Weekday, ) -> Result, DatabaseError> { sqlx::query_as!( DbJunctionRelaySchedule, - "SELECT * FROM junction_relay_schedule WHERE relay_id = ? AND schedule_id = ? AND weekday = ?", + "SELECT * FROM junction_relay_schedule WHERE relay_id = ? AND weekday = ?", relay.id, - schedule.id, weekday ) .fetch_optional(conn.deref_mut()) @@ -88,7 +86,7 @@ impl DbJunctionRelaySchedule { schedule: &DbSchedule, weekday: Weekday, ) -> Result { - match Self::get_junction(conn, relay, schedule, weekday).await? { + match Self::get_junction_by_relay_and_weekday(conn, relay, weekday).await? { None => sqlx::query_as!( DbJunctionRelaySchedule, "INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *", diff --git a/emgauwa-lib/src/db/relays.rs b/emgauwa-lib/src/db/relays.rs index 35468cc..6a9bb51 100644 --- a/emgauwa-lib/src/db/relays.rs +++ b/emgauwa-lib/src/db/relays.rs @@ -17,6 +17,7 @@ pub struct DbRelay { pub controller_id: i64, } + impl DbRelay { pub async fn get_all(conn: &mut PoolConnection) -> Result, DatabaseError> { sqlx::query_as!(DbRelay, "SELECT * FROM relays") @@ -152,4 +153,13 @@ impl DbRelay { } Ok(()) } + + pub async fn reload( + &self, + conn: &mut PoolConnection, + ) -> Result { + Self::get(conn, self.id) + .await? + .ok_or(DatabaseError::NotFound) + } }