Add app_state reload util and add handler for weekday change
This commit is contained in:
		
							parent
							
								
									97d9222a39
								
							
						
					
					
						commit
						6414083af0
					
				
					 5 changed files with 51 additions and 38 deletions
				
			
		| 
						 | 
				
			
			@ -1,11 +1,14 @@
 | 
			
		|||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbController, DbJunctionTag, DbTag};
 | 
			
		||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbJunctionTag, DbSchedule, DbTag};
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
use crate::types::Weekday;
 | 
			
		||||
use crate::utils;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, Serialize, Deserialize)]
 | 
			
		||||
pub struct DbRelay {
 | 
			
		||||
| 
						 | 
				
			
			@ -162,4 +165,14 @@ impl DbRelay {
 | 
			
		|||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_active_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		let weekday = utils::get_weekday();
 | 
			
		||||
		DbJunctionRelaySchedule::get_schedule(conn, &self, weekday as Weekday)
 | 
			
		||||
			.await?
 | 
			
		||||
			.ok_or(DatabaseError::NotFound)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,8 +6,7 @@ use sqlx::Sqlite;
 | 
			
		|||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
use crate::models::FromDbModel;
 | 
			
		||||
use crate::types::{ControllerUid, Weekday};
 | 
			
		||||
use crate::utils;
 | 
			
		||||
use crate::types::ControllerUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
| 
						 | 
				
			
			@ -42,14 +41,7 @@ impl FromDbModel for Relay {
 | 
			
		|||
		let controller_id = cache.uid.clone();
 | 
			
		||||
 | 
			
		||||
		let schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
 | 
			
		||||
 | 
			
		||||
		let weekday = utils::get_weekday();
 | 
			
		||||
		let active_schedule = block_on(DbJunctionRelaySchedule::get_schedule(
 | 
			
		||||
			conn,
 | 
			
		||||
			&db_model,
 | 
			
		||||
			weekday as Weekday,
 | 
			
		||||
		))?
 | 
			
		||||
		.ok_or(DatabaseError::NotFound)?;
 | 
			
		||||
		let active_schedule = block_on(db_model.get_active_schedule(conn))?;
 | 
			
		||||
 | 
			
		||||
		Ok(Relay {
 | 
			
		||||
			r: db_model,
 | 
			
		||||
| 
						 | 
				
			
			@ -66,15 +58,16 @@ impl Relay {
 | 
			
		|||
	pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		self.r = block_on(self.r.reload(conn))?;
 | 
			
		||||
		self.schedules = block_on(DbJunctionRelaySchedule::get_schedules(conn, &self.r))?;
 | 
			
		||||
 | 
			
		||||
		let weekday = utils::get_weekday();
 | 
			
		||||
		self.active_schedule = block_on(DbJunctionRelaySchedule::get_schedule(
 | 
			
		||||
			conn,
 | 
			
		||||
			&self.r,
 | 
			
		||||
			weekday as Weekday,
 | 
			
		||||
		))?
 | 
			
		||||
		.ok_or(DatabaseError::NotFound)?;
 | 
			
		||||
		self.reload_active_schedule(conn)?;
 | 
			
		||||
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn reload_active_schedule(
 | 
			
		||||
		&mut self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		self.active_schedule = block_on(self.r.get_active_schedule(conn))?;
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue