Fix small bugs in updating relays
This commit is contained in:
parent
e2cd84b136
commit
8b1affd8c7
3 changed files with 15 additions and 5 deletions
|
@ -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
|
||||||
|
|
|
@ -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 *",
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue