Add AppState to Controller and split up models
This commit is contained in:
		
							parent
							
								
									8dc9072fe8
								
							
						
					
					
						commit
						83c1f033d5
					
				
					 11 changed files with 261 additions and 150 deletions
				
			
		
							
								
								
									
										51
									
								
								emgauwa-lib/src/models/controller.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								emgauwa-lib/src/models/controller.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
use actix::MessageResponse;
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::DbController;
 | 
			
		||||
use crate::errors::{DatabaseError, EmgauwaError};
 | 
			
		||||
use crate::models::{convert_db_list_cache, FromDbModel, Relay};
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone, MessageResponse)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub c: DbController,
 | 
			
		||||
	pub relays: Vec<Relay>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Controller {
 | 
			
		||||
	type DbModel = DbController;
 | 
			
		||||
	type DbModelCache = Vec<Relay>;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let relays_db = block_on(db_model.get_relays(conn))?;
 | 
			
		||||
		let cache = convert_db_list_cache(conn, relays_db, db_model.clone())?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		Ok(Controller {
 | 
			
		||||
			c: db_model,
 | 
			
		||||
			relays: cache,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Controller {
 | 
			
		||||
	pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), EmgauwaError> {
 | 
			
		||||
		self.c = block_on(self.c.reload(conn))?;
 | 
			
		||||
		for relay in &mut self.relays {
 | 
			
		||||
			relay.reload(conn)?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,12 +1,14 @@
 | 
			
		|||
use futures::executor;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
mod controller;
 | 
			
		||||
mod relay;
 | 
			
		||||
mod schedule;
 | 
			
		||||
 | 
			
		||||
pub use controller::Controller;
 | 
			
		||||
pub use relay::Relay;
 | 
			
		||||
pub use schedule::Schedule;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule};
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
use crate::types::{ControllerUid, Weekday};
 | 
			
		||||
use crate::utils;
 | 
			
		||||
 | 
			
		||||
pub trait FromDbModel {
 | 
			
		||||
	type DbModel: Clone;
 | 
			
		||||
| 
						 | 
				
			
			@ -28,124 +30,6 @@ pub trait FromDbModel {
 | 
			
		|||
		Self: Sized;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub s: DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub r: DbRelay,
 | 
			
		||||
	pub controller: DbController,
 | 
			
		||||
	pub controller_id: ControllerUid,
 | 
			
		||||
	pub schedules: Vec<DbSchedule>,
 | 
			
		||||
	pub active_schedule: DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub c: DbController,
 | 
			
		||||
	pub relays: Vec<Relay>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Schedule {
 | 
			
		||||
	type DbModel = DbSchedule;
 | 
			
		||||
	type DbModelCache = Vec<String>;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let cache = executor::block_on(db_model.get_tags(conn))?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let schedule = db_model.clone();
 | 
			
		||||
 | 
			
		||||
		Ok(Schedule {
 | 
			
		||||
			s: schedule,
 | 
			
		||||
			tags: cache,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Relay {
 | 
			
		||||
	type DbModel = DbRelay;
 | 
			
		||||
	type DbModelCache = DbController;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let cache = executor::block_on(db_model.get_controller(conn))?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let tags = executor::block_on(db_model.get_tags(conn))?;
 | 
			
		||||
		let controller_id = cache.uid.clone();
 | 
			
		||||
 | 
			
		||||
		let schedules =
 | 
			
		||||
			executor::block_on(DbJunctionRelaySchedule::get_schedules(conn, &db_model))?;
 | 
			
		||||
 | 
			
		||||
		let weekday = utils::get_weekday();
 | 
			
		||||
		let active_schedule = executor::block_on(DbJunctionRelaySchedule::get_schedule(
 | 
			
		||||
			conn,
 | 
			
		||||
			&db_model,
 | 
			
		||||
			weekday as Weekday,
 | 
			
		||||
		))?
 | 
			
		||||
		.ok_or(DatabaseError::NotFound)?;
 | 
			
		||||
 | 
			
		||||
		Ok(Relay {
 | 
			
		||||
			r: db_model,
 | 
			
		||||
			controller: cache,
 | 
			
		||||
			controller_id,
 | 
			
		||||
			schedules,
 | 
			
		||||
			active_schedule,
 | 
			
		||||
			tags,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Controller {
 | 
			
		||||
	type DbModel = DbController;
 | 
			
		||||
	type DbModelCache = Vec<Relay>;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let relays_db = executor::block_on(db_model.get_relays(conn))?;
 | 
			
		||||
		let cache = convert_db_list_cache(conn, relays_db, db_model.clone())?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		Ok(Controller {
 | 
			
		||||
			c: db_model,
 | 
			
		||||
			relays: cache,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn convert_db_list_generic<T: FromDbModel>(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	db_models: Vec<T::DbModel>,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										70
									
								
								emgauwa-lib/src/models/relay.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								emgauwa-lib/src/models/relay.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,70 @@
 | 
			
		|||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub r: DbRelay,
 | 
			
		||||
	pub controller: DbController,
 | 
			
		||||
	pub controller_id: ControllerUid,
 | 
			
		||||
	pub schedules: Vec<DbSchedule>,
 | 
			
		||||
	pub active_schedule: DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Relay {
 | 
			
		||||
	type DbModel = DbRelay;
 | 
			
		||||
	type DbModelCache = DbController;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let cache = block_on(db_model.get_controller(conn))?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let tags = block_on(db_model.get_tags(conn))?;
 | 
			
		||||
		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)?;
 | 
			
		||||
 | 
			
		||||
		Ok(Relay {
 | 
			
		||||
			r: db_model,
 | 
			
		||||
			controller: cache,
 | 
			
		||||
			controller_id,
 | 
			
		||||
			schedules,
 | 
			
		||||
			active_schedule,
 | 
			
		||||
			tags,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Relay {
 | 
			
		||||
	pub fn reload(&mut self, conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		self.r = block_on(self.r.reload(conn))?;
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								emgauwa-lib/src/models/schedule.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								emgauwa-lib/src/models/schedule.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
use futures::executor::block_on;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::DbSchedule;
 | 
			
		||||
use crate::errors::DatabaseError;
 | 
			
		||||
use crate::models::FromDbModel;
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub s: DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Schedule {
 | 
			
		||||
	type DbModel = DbSchedule;
 | 
			
		||||
	type DbModelCache = Vec<String>;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let cache = block_on(db_model.get_tags(conn))?;
 | 
			
		||||
		Self::from_db_model_cache(conn, db_model, cache)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn from_db_model_cache(
 | 
			
		||||
		_conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
		cache: Self::DbModelCache,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let schedule = db_model.clone();
 | 
			
		||||
 | 
			
		||||
		Ok(Schedule {
 | 
			
		||||
			s: schedule,
 | 
			
		||||
			tags: cache,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue