Fix small bugs in updating relays
This commit is contained in:
		
							parent
							
								
									e2cd84b136
								
							
						
					
					
						commit
						8b1affd8c7
					
				
					 3 changed files with 15 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -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<Sqlite>,
 | 
			
		||||
		relay: &DbRelay,
 | 
			
		||||
		schedule: &DbSchedule,
 | 
			
		||||
		weekday: Weekday,
 | 
			
		||||
	) -> Result<Option<DbJunctionRelaySchedule>, 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<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!(
 | 
			
		||||
				DbJunctionRelaySchedule,
 | 
			
		||||
				"INSERT INTO junction_relay_schedule (weekday, relay_id, schedule_id) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,6 +17,7 @@ pub struct DbRelay {
 | 
			
		|||
	pub controller_id: i64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
impl DbRelay {
 | 
			
		||||
	pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT * FROM relays")
 | 
			
		||||
| 
						 | 
				
			
			@ -152,4 +153,13 @@ impl DbRelay {
 | 
			
		|||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn reload(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
		Self::get(conn, self.id)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue