Fix small bugs in updating relays

This commit is contained in:
Tobias Reisinger 2023-12-05 03:12:47 +01:00
parent e2cd84b136
commit 8b1affd8c7
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
3 changed files with 15 additions and 5 deletions

View file

@ -136,6 +136,8 @@ pub async fn update_for_controller(
relay.set_tags(&mut pool_conn, tags.as_slice()).await?; 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)?; let return_relay = Relay::from_db_model(&mut pool_conn, relay)?;
app_server app_server

View file

@ -29,17 +29,15 @@ impl DbJunctionRelaySchedule {
.map_err(DatabaseError::from) .map_err(DatabaseError::from)
} }
pub async fn get_junction( pub async fn get_junction_by_relay_and_weekday(
conn: &mut PoolConnection<Sqlite>, conn: &mut PoolConnection<Sqlite>,
relay: &DbRelay, relay: &DbRelay,
schedule: &DbSchedule,
weekday: Weekday, weekday: Weekday,
) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> { ) -> Result<Option<DbJunctionRelaySchedule>, DatabaseError> {
sqlx::query_as!( sqlx::query_as!(
DbJunctionRelaySchedule, 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, relay.id,
schedule.id,
weekday weekday
) )
.fetch_optional(conn.deref_mut()) .fetch_optional(conn.deref_mut())
@ -88,7 +86,7 @@ impl DbJunctionRelaySchedule {
schedule: &DbSchedule, schedule: &DbSchedule,
weekday: Weekday, weekday: Weekday,
) -> Result<DbJunctionRelaySchedule, DatabaseError> { ) -> Result<DbJunctionRelaySchedule, DatabaseError> {
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!( None => sqlx::query_as!(
DbJunctionRelaySchedule, DbJunctionRelaySchedule,
"INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *", "INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *",

View file

@ -17,6 +17,7 @@ pub struct DbRelay {
pub controller_id: i64, pub controller_id: i64,
} }
impl DbRelay { impl DbRelay {
pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> { pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
sqlx::query_as!(DbRelay, "SELECT * FROM relays") sqlx::query_as!(DbRelay, "SELECT * FROM relays")
@ -152,4 +153,13 @@ impl DbRelay {
} }
Ok(()) Ok(())
} }
pub async fn reload(
&self,
conn: &mut PoolConnection<Sqlite>,
) -> Result<DbRelay, DatabaseError> {
Self::get(conn, self.id)
.await?
.ok_or(DatabaseError::NotFound)
}
} }