Add feature to import missing schedules
This commit is contained in:
		
							parent
							
								
									6400b7745c
								
							
						
					
					
						commit
						c8f40284ef
					
				
					 6 changed files with 81 additions and 26 deletions
				
			
		| 
						 | 
				
			
			@ -13,7 +13,7 @@ pub enum DatabaseError {
 | 
			
		|||
	Protected,
 | 
			
		||||
	UpdateError,
 | 
			
		||||
	UpdateGetError,
 | 
			
		||||
	Unknown,
 | 
			
		||||
	Unknown(String),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl DatabaseError {
 | 
			
		||||
| 
						 | 
				
			
			@ -53,7 +53,7 @@ impl From<&DatabaseError> for String {
 | 
			
		|||
			DatabaseError::UpdateGetError => {
 | 
			
		||||
				"error on retrieving updated model from database (your entry was saved)"
 | 
			
		||||
			}
 | 
			
		||||
			DatabaseError::Unknown => "unknown error",
 | 
			
		||||
			DatabaseError::Unknown(_) => "unknown error",
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -68,7 +68,7 @@ impl From<Error> for DatabaseError {
 | 
			
		|||
	fn from(value: Error) -> Self {
 | 
			
		||||
		match value {
 | 
			
		||||
			Error::RowNotFound => DatabaseError::NotFound,
 | 
			
		||||
			_ => DatabaseError::Unknown,
 | 
			
		||||
			_ => DatabaseError::Unknown(value.to_string()),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,10 +56,13 @@ impl DbRelay {
 | 
			
		|||
		controller: &DbController,
 | 
			
		||||
		number: i64,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
	) -> Result<DbRelay, DatabaseError> {
 | 
			
		||||
	) -> Result<(DbRelay, bool), DatabaseError> {
 | 
			
		||||
		match DbRelay::get_by_controller_and_num(conn, controller, number).await? {
 | 
			
		||||
			Some(relay) => Ok(relay),
 | 
			
		||||
			None => DbRelay::create(conn, new_name, number, controller).await,
 | 
			
		||||
			Some(relay) => Ok((relay, false)),
 | 
			
		||||
			None => {
 | 
			
		||||
				let relay = DbRelay::create(conn, new_name, number, controller).await?;
 | 
			
		||||
				Ok((relay, true))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -104,6 +104,21 @@ impl DbSchedule {
 | 
			
		|||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid_or_create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		uid: ScheduleUid,
 | 
			
		||||
		name: &str,
 | 
			
		||||
		periods: &DbPeriods,
 | 
			
		||||
	) -> Result<(DbSchedule, bool), DatabaseError> {
 | 
			
		||||
		match DbSchedule::get_by_uid(conn, &uid).await? {
 | 
			
		||||
			Some(schedule) => Ok((schedule, false)),
 | 
			
		||||
			None => {
 | 
			
		||||
				let schedule = DbSchedule::create(conn, uid, name, periods).await?;
 | 
			
		||||
				Ok((schedule, true))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_on(conn: &mut PoolConnection<Sqlite>) -> Result<DbSchedule, DatabaseError> {
 | 
			
		||||
		if let Some(schedule) = DbSchedule::get_by_uid(conn, &ScheduleUid::On).await? {
 | 
			
		||||
			return Ok(schedule);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
use std::fmt::{Display, Formatter};
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
 | 
			
		||||
| 
						 | 
				
			
			@ -17,6 +18,12 @@ impl Default for ControllerUid {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Display for ControllerUid {
 | 
			
		||||
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | 
			
		||||
		write!(f, "{}", String::from(self))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for ControllerUid {
 | 
			
		||||
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue