From 8b1affd8c7474f100876da04656efe120f4d160f Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Tue, 5 Dec 2023 03:12:47 +0100 Subject: [PATCH] Fix small bugs in updating relays --- emgauwa-core/src/handlers/v1/relays.rs | 2 ++ emgauwa-lib/src/db/junction_relay_schedule.rs | 8 +++----- emgauwa-lib/src/db/relays.rs | 10 ++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) 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) + } }