Add WIP relays to database and api
This commit is contained in:
		
							parent
							
								
									4e3df272c3
								
							
						
					
					
						commit
						734f8b291c
					
				
					 14 changed files with 387 additions and 88 deletions
				
			
		| 
						 | 
					@ -10,10 +10,12 @@ use tokio_tungstenite::tungstenite::Error;
 | 
				
			||||||
use emgauwa_lib::db;
 | 
					use emgauwa_lib::db;
 | 
				
			||||||
use emgauwa_lib::db::Controller;
 | 
					use emgauwa_lib::db::Controller;
 | 
				
			||||||
use emgauwa_lib::db::types::ControllerUid;
 | 
					use emgauwa_lib::db::types::ControllerUid;
 | 
				
			||||||
 | 
					use crate::relay_loop::run_relay_loop;
 | 
				
			||||||
use crate::settings::Settings;
 | 
					use crate::settings::Settings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
mod settings;
 | 
					mod settings;
 | 
				
			||||||
mod driver;
 | 
					mod driver;
 | 
				
			||||||
 | 
					mod relay_loop;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn create_this_controller(conn: &mut PoolConnection<Sqlite>, settings: &Settings) -> Controller {
 | 
					fn create_this_controller(conn: &mut PoolConnection<Sqlite>, settings: &Settings) -> Controller {
 | 
				
			||||||
	futures::executor::block_on(async {
 | 
						futures::executor::block_on(async {
 | 
				
			||||||
| 
						 | 
					@ -43,8 +45,9 @@ async fn main() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let this_json = serde_json::to_string(&this).unwrap();
 | 
						let this_json = serde_json::to_string(&this).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						println!("{:?}", settings.relays);
 | 
				
			||||||
	println!("{:?}", this);
 | 
						println!("{:?}", this);
 | 
				
			||||||
	println!("{:?}", this_json);
 | 
						println!("{}", this_json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let url = format!(
 | 
						let url = format!(
 | 
				
			||||||
		"ws://{}:{}/api/v1/ws/controllers",
 | 
							"ws://{}:{}/api/v1/ws/controllers",
 | 
				
			||||||
| 
						 | 
					@ -61,10 +64,12 @@ async fn main() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	write.send(Message::text(this_json)).await.unwrap();
 | 
						write.send(Message::text(this_json)).await.unwrap();
 | 
				
			||||||
	let ws_to_stdout = read.for_each(handle_message);
 | 
						let ws_to_stdout = read.for_each(handle_message);
 | 
				
			||||||
 | 
						let stdin_to_ws = stdin_rx.map(Ok).forward(write);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ws_to_stdout.await;
 | 
						tokio::spawn(run_relay_loop(settings));
 | 
				
			||||||
	//pin_mut!(stdin_to_ws, ws_to_stdout);
 | 
					
 | 
				
			||||||
	//future::select(stdin_to_ws, ws_to_stdout).await;
 | 
						pin_mut!(stdin_to_ws, ws_to_stdout);
 | 
				
			||||||
 | 
						future::select(stdin_to_ws, ws_to_stdout).await;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Our helper method which will read data from stdin and send it along the
 | 
					// Our helper method which will read data from stdin and send it along the
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										9
									
								
								emgauwa-controller/src/relay_loop.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								emgauwa-controller/src/relay_loop.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					use chrono::Local;
 | 
				
			||||||
 | 
					use crate::settings::Settings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[allow(unused_variables)]
 | 
				
			||||||
 | 
					pub async fn run_relay_loop(settings: Settings) {
 | 
				
			||||||
 | 
					    let next_timestamp = Local::now().naive_local();
 | 
				
			||||||
 | 
					    loop {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,7 @@ async fn main() -> std::io::Result<()> {
 | 
				
			||||||
			.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
 | 
								.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
 | 
				
			||||||
			.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
 | 
								.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
 | 
				
			||||||
			.app_data(web::Data::new(pool.clone()))
 | 
								.app_data(web::Data::new(pool.clone()))
 | 
				
			||||||
 | 
								.service(handlers::v1::relays::index)
 | 
				
			||||||
			.service(handlers::v1::schedules::index)
 | 
								.service(handlers::v1::schedules::index)
 | 
				
			||||||
			.service(handlers::v1::schedules::tagged)
 | 
								.service(handlers::v1::schedules::tagged)
 | 
				
			||||||
			.service(handlers::v1::schedules::show)
 | 
								.service(handlers::v1::schedules::show)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,12 +1,11 @@
 | 
				
			||||||
use serde_derive::{Deserialize, Serialize};
 | 
					use serde_derive::Serialize;
 | 
				
			||||||
use std::ops::DerefMut;
 | 
					use std::ops::DerefMut;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use sqlx::pool::PoolConnection;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::Sqlite;
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::db::errors::DatabaseError;
 | 
					use crate::db::errors::DatabaseError;
 | 
				
			||||||
use crate::db::model_utils::Period;
 | 
					use crate::db::Tag;
 | 
				
			||||||
use crate::db::tag::Tag;
 | 
					 | 
				
			||||||
use crate::db::types::ControllerUid;
 | 
					use crate::db::types::ControllerUid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Clone)]
 | 
					#[derive(Debug, Serialize, Clone)]
 | 
				
			||||||
| 
						 | 
					@ -18,9 +17,6 @@ pub struct Controller {
 | 
				
			||||||
	pub active: bool,
 | 
						pub active: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
 | 
					 | 
				
			||||||
pub struct Periods(pub Vec<Period>);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl Controller {
 | 
					impl Controller {
 | 
				
			||||||
	pub async fn get_all(
 | 
						pub async fn get_all(
 | 
				
			||||||
		conn: &mut PoolConnection<Sqlite>,
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
| 
						 | 
					@ -117,6 +113,6 @@ impl Controller {
 | 
				
			||||||
		.execute(conn.deref_mut())
 | 
							.execute(conn.deref_mut())
 | 
				
			||||||
		.await?;
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Controller::get_by_uid(conn, &self.uid).await
 | 
							Self::get(conn, self.id).await
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,14 +10,16 @@ use crate::db::types::ScheduleUid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub mod errors;
 | 
					pub mod errors;
 | 
				
			||||||
mod model_utils;
 | 
					mod model_utils;
 | 
				
			||||||
mod models;
 | 
					 | 
				
			||||||
mod schedules;
 | 
					mod schedules;
 | 
				
			||||||
pub mod tag;
 | 
					mod tag;
 | 
				
			||||||
pub mod types;
 | 
					pub mod types;
 | 
				
			||||||
mod controllers;
 | 
					mod controllers;
 | 
				
			||||||
 | 
					mod relays;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub use controllers::Controller;
 | 
					pub use controllers::Controller;
 | 
				
			||||||
 | 
					pub use relays::Relay;
 | 
				
			||||||
pub use schedules::{Periods, Schedule};
 | 
					pub use schedules::{Periods, Schedule};
 | 
				
			||||||
 | 
					pub use tag::Tag;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
 | 
					static MIGRATOR: Migrator = sqlx::migrate!("../migrations"); // defaults to "./migrations"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +0,0 @@
 | 
				
			||||||
use serde::Serialize;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(Debug, Serialize)]
 | 
					 | 
				
			||||||
pub struct Relay {
 | 
					 | 
				
			||||||
	#[serde(skip)]
 | 
					 | 
				
			||||||
	pub id: i64,
 | 
					 | 
				
			||||||
	// TODO
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										130
									
								
								emgauwa-lib/src/db/relays.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								emgauwa-lib/src/db/relays.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,130 @@
 | 
				
			||||||
 | 
					use serde_derive::Serialize;
 | 
				
			||||||
 | 
					use std::ops::DerefMut;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
 | 
					use crate::db::Controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::db::errors::DatabaseError;
 | 
				
			||||||
 | 
					use crate::db::Tag;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Serialize, Clone)]
 | 
				
			||||||
 | 
					#[derive(sqlx::FromRow)]
 | 
				
			||||||
 | 
					pub struct Relay {
 | 
				
			||||||
 | 
						#[serde(skip)]
 | 
				
			||||||
 | 
						pub id: i64,
 | 
				
			||||||
 | 
						pub name: String,
 | 
				
			||||||
 | 
						pub number: i64,
 | 
				
			||||||
 | 
						#[serde(skip)]
 | 
				
			||||||
 | 
						pub controller_id: i64,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Relay {
 | 
				
			||||||
 | 
						pub async fn get_all(
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
						) -> Result<Vec<Relay>, DatabaseError> {
 | 
				
			||||||
 | 
							Ok(sqlx::query_as!(Relay, "SELECT * FROM relays")
 | 
				
			||||||
 | 
								.fetch_all(conn.deref_mut())
 | 
				
			||||||
 | 
								.await?)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get(
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							id: i64,
 | 
				
			||||||
 | 
						) -> Result<Relay, DatabaseError> {
 | 
				
			||||||
 | 
							sqlx::query_as!(
 | 
				
			||||||
 | 
								Relay,
 | 
				
			||||||
 | 
								"SELECT * FROM relays WHERE id = ?",
 | 
				
			||||||
 | 
								id
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
								.fetch_optional(conn.deref_mut())
 | 
				
			||||||
 | 
								.await
 | 
				
			||||||
 | 
								.map(|s| s.ok_or(DatabaseError::NotFound))?
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get_by_tag(
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							tag: &Tag,
 | 
				
			||||||
 | 
						) -> Result<Vec<Relay>, DatabaseError> {
 | 
				
			||||||
 | 
							Ok(sqlx::query_as!(Relay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
				
			||||||
 | 
								.fetch_all(conn.deref_mut())
 | 
				
			||||||
 | 
								.await?)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn create(
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							new_name: &str,
 | 
				
			||||||
 | 
							new_number: i64,
 | 
				
			||||||
 | 
							new_controller: &Controller,
 | 
				
			||||||
 | 
						) -> Result<Relay, DatabaseError> {
 | 
				
			||||||
 | 
							sqlx::query_as!(
 | 
				
			||||||
 | 
								Relay,
 | 
				
			||||||
 | 
								"INSERT INTO relays (name, number, controller_id) VALUES (?, ?, ?) RETURNING *",
 | 
				
			||||||
 | 
								new_name,
 | 
				
			||||||
 | 
								new_number,
 | 
				
			||||||
 | 
								new_controller.id,
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							.fetch_optional(conn.deref_mut())
 | 
				
			||||||
 | 
							.await?
 | 
				
			||||||
 | 
							.ok_or(DatabaseError::InsertGetError)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn delete(
 | 
				
			||||||
 | 
							&self,
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
						) -> Result<(), DatabaseError> {
 | 
				
			||||||
 | 
							sqlx::query!("DELETE FROM relays WHERE id = ?", self.id)
 | 
				
			||||||
 | 
								.execute(conn.deref_mut())
 | 
				
			||||||
 | 
								.await
 | 
				
			||||||
 | 
								.map(|res| match res.rows_affected() {
 | 
				
			||||||
 | 
									0 => Err(DatabaseError::DeleteError),
 | 
				
			||||||
 | 
									_ => Ok(()),
 | 
				
			||||||
 | 
								})?
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn update(
 | 
				
			||||||
 | 
							&self,
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							new_name: &str,
 | 
				
			||||||
 | 
							new_number: i64,
 | 
				
			||||||
 | 
							new_controller: &Controller,
 | 
				
			||||||
 | 
						) -> Result<Relay, DatabaseError> {
 | 
				
			||||||
 | 
							sqlx::query!(
 | 
				
			||||||
 | 
								"UPDATE relays SET name = ?, number = ?, controller_id = ? WHERE id = ?",
 | 
				
			||||||
 | 
								new_name,
 | 
				
			||||||
 | 
								new_number,
 | 
				
			||||||
 | 
								new_controller.id,
 | 
				
			||||||
 | 
								self.id,
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							.execute(conn.deref_mut())
 | 
				
			||||||
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							Relay::get(conn, self.id).await
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get_controller(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Controller, DatabaseError> {
 | 
				
			||||||
 | 
							Controller::get(conn, self.controller_id).await
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get_tags(&self, conn: &mut PoolConnection<Sqlite>) -> Result<Vec<String>, DatabaseError> {
 | 
				
			||||||
 | 
							Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
 | 
				
			||||||
 | 
								.fetch_all(conn.deref_mut())
 | 
				
			||||||
 | 
								.await?)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn set_tags(
 | 
				
			||||||
 | 
							&self,
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							new_tags: &[String],
 | 
				
			||||||
 | 
						) -> Result<(), DatabaseError> {
 | 
				
			||||||
 | 
							sqlx::query!("DELETE FROM junction_tag WHERE relay_id = ?", self.id)
 | 
				
			||||||
 | 
								.execute(conn.deref_mut())
 | 
				
			||||||
 | 
								.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for new_tag in new_tags {
 | 
				
			||||||
 | 
								let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
 | 
				
			||||||
 | 
								tag.link_relay(conn, self).await?;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							Ok(())
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,7 @@ use sqlx::Sqlite;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::db::errors::DatabaseError;
 | 
					use crate::db::errors::DatabaseError;
 | 
				
			||||||
use crate::db::model_utils::Period;
 | 
					use crate::db::model_utils::Period;
 | 
				
			||||||
use crate::db::tag::Tag;
 | 
					use crate::db::Tag;
 | 
				
			||||||
use crate::db::types::ScheduleUid;
 | 
					use crate::db::types::ScheduleUid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Clone)]
 | 
					#[derive(Debug, Serialize, Clone)]
 | 
				
			||||||
| 
						 | 
					@ -34,7 +34,7 @@ impl Schedule {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pub async fn get(
 | 
						pub async fn get(
 | 
				
			||||||
		conn: &mut PoolConnection<Sqlite>,
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
		id: &i64,
 | 
							id: i64,
 | 
				
			||||||
	) -> Result<Schedule, DatabaseError> {
 | 
						) -> Result<Schedule, DatabaseError> {
 | 
				
			||||||
		sqlx::query_as!(
 | 
							sqlx::query_as!(
 | 
				
			||||||
			Schedule,
 | 
								Schedule,
 | 
				
			||||||
| 
						 | 
					@ -127,7 +127,7 @@ impl Schedule {
 | 
				
			||||||
		.execute(conn.deref_mut())
 | 
							.execute(conn.deref_mut())
 | 
				
			||||||
		.await?;
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Schedule::get_by_uid(conn, &self.uid).await
 | 
							Schedule::get(conn, self.id).await
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pub async fn get_tags(
 | 
						pub async fn get_tags(
 | 
				
			||||||
| 
						 | 
					@ -149,16 +149,7 @@ impl Schedule {
 | 
				
			||||||
			.await?;
 | 
								.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for new_tag in new_tags {
 | 
							for new_tag in new_tags {
 | 
				
			||||||
			let tag: Option<Tag> =
 | 
								let tag: Tag = Tag::get_by_tag_or_create(conn, new_tag).await?;
 | 
				
			||||||
				sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", new_tag)
 | 
					 | 
				
			||||||
					.fetch_optional(conn.deref_mut())
 | 
					 | 
				
			||||||
					.await?;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			let tag = match tag {
 | 
					 | 
				
			||||||
				Some(id) => id,
 | 
					 | 
				
			||||||
				None => Tag::create(conn, new_tag).await?,
 | 
					 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			tag.link_schedule(conn, self).await?;
 | 
								tag.link_schedule(conn, self).await?;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		Ok(())
 | 
							Ok(())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,8 +5,7 @@ use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::Sqlite;
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::db::errors::DatabaseError;
 | 
					use crate::db::errors::DatabaseError;
 | 
				
			||||||
use crate::db::models::*;
 | 
					use crate::db::{Relay, Schedule};
 | 
				
			||||||
use crate::db::Schedule;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Clone)]
 | 
					#[derive(Debug, Serialize, Clone)]
 | 
				
			||||||
pub struct Tag {
 | 
					pub struct Tag {
 | 
				
			||||||
| 
						 | 
					@ -46,6 +45,17 @@ impl Tag {
 | 
				
			||||||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
								.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub async fn get_by_tag_or_create(
 | 
				
			||||||
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
							target_tag: &str,
 | 
				
			||||||
 | 
						) -> Result<Tag, DatabaseError> {
 | 
				
			||||||
 | 
							match Tag::get_by_tag(conn, target_tag).await {
 | 
				
			||||||
 | 
								Ok(tag) => Ok(tag),
 | 
				
			||||||
 | 
								Err(DatabaseError::NotFound) => Tag::create(conn, target_tag).await,
 | 
				
			||||||
 | 
								Err(e) => Err(e),
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pub async fn get_by_tag(
 | 
						pub async fn get_by_tag(
 | 
				
			||||||
		conn: &mut PoolConnection<Sqlite>,
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
		target_tag: &str,
 | 
							target_tag: &str,
 | 
				
			||||||
| 
						 | 
					@ -56,7 +66,6 @@ impl Tag {
 | 
				
			||||||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
								.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#[allow(dead_code)]
 | 
					 | 
				
			||||||
	pub async fn link_relay(
 | 
						pub async fn link_relay(
 | 
				
			||||||
		&self,
 | 
							&self,
 | 
				
			||||||
		conn: &mut PoolConnection<Sqlite>,
 | 
							conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,2 +1,3 @@
 | 
				
			||||||
pub mod schedules;
 | 
					pub mod schedules;
 | 
				
			||||||
pub mod ws;
 | 
					pub mod ws;
 | 
				
			||||||
 | 
					pub mod relays;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										163
									
								
								emgauwa-lib/src/handlers/v1/relays.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								emgauwa-lib/src/handlers/v1/relays.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,163 @@
 | 
				
			||||||
 | 
					use actix_web::{delete, get, post, put, web, HttpResponse};
 | 
				
			||||||
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
 | 
					use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::db::errors::DatabaseError;
 | 
				
			||||||
 | 
					use crate::db::Relay;
 | 
				
			||||||
 | 
					use crate::db::Tag;
 | 
				
			||||||
 | 
					use crate::handlers::errors::ApiError;
 | 
				
			||||||
 | 
					use crate::return_models::ReturnRelay;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
 | 
					pub struct RequestRelay {
 | 
				
			||||||
 | 
						name: String,
 | 
				
			||||||
 | 
						tags: Vec<String>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[get("/api/v1/relays")]
 | 
				
			||||||
 | 
					pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
						let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let relays = Relay::get_all(&mut pool_conn).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let return_relays: Vec<ReturnRelay> =
 | 
				
			||||||
 | 
							relays.iter().map(|s| ReturnRelay::from_relay_ref(s, &mut pool_conn)).collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Ok(HttpResponse::Ok().json(return_relays))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//#[get("/api/v1/tags/tag/{tag}")]
 | 
				
			||||||
 | 
					//pub async fn tagged(
 | 
				
			||||||
 | 
					//	pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
					//	path: web::Path<(String,)>,
 | 
				
			||||||
 | 
					//) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
					//	let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let (tag,) = path.into_inner();
 | 
				
			||||||
 | 
					//	let tag_db = Tag::get_by_tag(&mut pool_conn, &tag).await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let relays = Relay::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let return_relays: Vec<ReturnRelay> =
 | 
				
			||||||
 | 
					//		relays.iter().map(|s| ReturnRelay::from_relay_ref(s, &mut pool_conn)).collect();
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	Ok(HttpResponse::Ok().json(return_relays))
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//#[get("/api/v1/tags/{relay_id}")]
 | 
				
			||||||
 | 
					//pub async fn show(
 | 
				
			||||||
 | 
					//	pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
					//	path: web::Path<(String,)>,
 | 
				
			||||||
 | 
					//) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
					//	let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let (relay_uid,) = path.into_inner();
 | 
				
			||||||
 | 
					//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let relay = Relay::get_by_uid(&mut pool_conn, &uid).await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let return_relay = ReturnRelay::from_relay(relay, &mut pool_conn);
 | 
				
			||||||
 | 
					//	Ok(HttpResponse::Ok().json(return_relay))
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//#[post("/api/v1/tags")]
 | 
				
			||||||
 | 
					//pub async fn add(
 | 
				
			||||||
 | 
					//	pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
					//	data: web::Json<RequestRelay>,
 | 
				
			||||||
 | 
					//) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
					//	let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let new_relay = Relay::create(&mut pool_conn, &data.name, &data.periods).await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	new_relay
 | 
				
			||||||
 | 
					//		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
				
			||||||
 | 
					//		.await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let return_relay = ReturnRelay::from_relay(new_relay, &mut pool_conn);
 | 
				
			||||||
 | 
					//	Ok(HttpResponse::Created().json(return_relay))
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//async fn add_list_single(
 | 
				
			||||||
 | 
					//	conn: &mut PoolConnection<Sqlite>,
 | 
				
			||||||
 | 
					//	request_relay: &RequestRelay,
 | 
				
			||||||
 | 
					//) -> Result<Relay, DatabaseError> {
 | 
				
			||||||
 | 
					//	let new_relay =
 | 
				
			||||||
 | 
					//		Relay::create(conn, &request_relay.name, &request_relay.periods).await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	new_relay
 | 
				
			||||||
 | 
					//		.set_tags(conn, request_relay.tags.as_slice())
 | 
				
			||||||
 | 
					//		.await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	Ok(new_relay)
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//#[post("/api/v1/tags/list")]
 | 
				
			||||||
 | 
					//pub async fn add_list(
 | 
				
			||||||
 | 
					//	pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
					//	data: web::Json<Vec<RequestRelay>>,
 | 
				
			||||||
 | 
					//) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
					//	let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let result: Vec<Result<Relay, DatabaseError>> = data
 | 
				
			||||||
 | 
					//		.as_slice()
 | 
				
			||||||
 | 
					//		.iter()
 | 
				
			||||||
 | 
					//		.map(|request_relay| {
 | 
				
			||||||
 | 
					//			futures::executor::block_on(add_list_single(&mut pool_conn, request_relay))
 | 
				
			||||||
 | 
					//		})
 | 
				
			||||||
 | 
					//		.collect();
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let mut return_relays: Vec<ReturnRelay> = Vec::new();
 | 
				
			||||||
 | 
					//	for relay in result {
 | 
				
			||||||
 | 
					//		match relay {
 | 
				
			||||||
 | 
					//			Ok(relay) => return_relays.push(ReturnRelay::from_relay(relay, &mut pool_conn)),
 | 
				
			||||||
 | 
					//			Err(e) => return Ok(HttpResponse::from(e)),
 | 
				
			||||||
 | 
					//		}
 | 
				
			||||||
 | 
					//	}
 | 
				
			||||||
 | 
					//	Ok(HttpResponse::Created().json(return_relays))
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//#[put("/api/v1/tags/{relay_id}")]
 | 
				
			||||||
 | 
					//pub async fn update(
 | 
				
			||||||
 | 
					//	pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
					//	path: web::Path<(String,)>,
 | 
				
			||||||
 | 
					//	data: web::Json<RequestRelay>,
 | 
				
			||||||
 | 
					//) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
					//	let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let (relay_uid,) = path.into_inner();
 | 
				
			||||||
 | 
					//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let relay = Relay::get_by_uid(&mut pool_conn, &uid).await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let relay = relay
 | 
				
			||||||
 | 
					//		.update(&mut pool_conn, data.name.as_str(), &data.periods)
 | 
				
			||||||
 | 
					//		.await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	relay
 | 
				
			||||||
 | 
					//		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
				
			||||||
 | 
					//		.await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let return_relay = ReturnRelay::from_relay(relay, &mut pool_conn);
 | 
				
			||||||
 | 
					//	Ok(HttpResponse::Ok().json(return_relay))
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//#[delete("/api/v1/tags/{relay_id}")]
 | 
				
			||||||
 | 
					//pub async fn delete(
 | 
				
			||||||
 | 
					//	pool: web::Data<Pool<Sqlite>>,
 | 
				
			||||||
 | 
					//	path: web::Path<(String,)>,
 | 
				
			||||||
 | 
					//) -> Result<HttpResponse, ApiError> {
 | 
				
			||||||
 | 
					//	let mut pool_conn = pool.acquire().await?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	let (relay_uid,) = path.into_inner();
 | 
				
			||||||
 | 
					//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//	match uid {
 | 
				
			||||||
 | 
					//		RelayUid::Off => Err(ApiError::ProtectedRelay),
 | 
				
			||||||
 | 
					//		RelayUid::On => Err(ApiError::ProtectedRelay),
 | 
				
			||||||
 | 
					//		RelayUid::Any(_) => {
 | 
				
			||||||
 | 
					//			Relay::delete_by_uid(&mut pool_conn, uid).await?;
 | 
				
			||||||
 | 
					//			Ok(HttpResponse::Ok().json("relay got deleted"))
 | 
				
			||||||
 | 
					//		}
 | 
				
			||||||
 | 
					//	}
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
| 
						 | 
					@ -5,11 +5,10 @@ use sqlx::{Pool, Sqlite};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::db::errors::DatabaseError;
 | 
					use crate::db::errors::DatabaseError;
 | 
				
			||||||
use crate::db::{Periods, Schedule};
 | 
					use crate::db::{Periods, Schedule};
 | 
				
			||||||
use crate::db::tag::Tag;
 | 
					use crate::db::Tag;
 | 
				
			||||||
use crate::db::types::ScheduleUid;
 | 
					use crate::db::types::ScheduleUid;
 | 
				
			||||||
use crate::handlers::errors::ApiError;
 | 
					use crate::handlers::errors::ApiError;
 | 
				
			||||||
use crate::return_models::ReturnSchedule;
 | 
					use crate::return_models::ReturnSchedule;
 | 
				
			||||||
use crate::utils::vec_has_error;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize, Deserialize)]
 | 
					#[derive(Debug, Serialize, Deserialize)]
 | 
				
			||||||
pub struct RequestSchedule {
 | 
					pub struct RequestSchedule {
 | 
				
			||||||
| 
						 | 
					@ -24,11 +23,8 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let schedules = Schedule::get_all(&mut pool_conn).await?;
 | 
						let schedules = Schedule::get_all(&mut pool_conn).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut return_schedules: Vec<ReturnSchedule> =
 | 
						let return_schedules: Vec<ReturnSchedule> =
 | 
				
			||||||
		schedules.iter().map(ReturnSchedule::from).collect();
 | 
							schedules.iter().map(|s| ReturnSchedule::from_schedule_ref(s, &mut pool_conn)).collect();
 | 
				
			||||||
	for schedule in return_schedules.iter_mut() {
 | 
					 | 
				
			||||||
		schedule.load_tags(&mut pool_conn);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
						Ok(HttpResponse::Ok().json(return_schedules))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -45,11 +41,9 @@ pub async fn tagged(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
						let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut return_schedules: Vec<ReturnSchedule> =
 | 
						let return_schedules: Vec<ReturnSchedule> =
 | 
				
			||||||
		schedules.iter().map(ReturnSchedule::from).collect();
 | 
							schedules.iter().map(|s| ReturnSchedule::from_schedule_ref(s, &mut pool_conn)).collect();
 | 
				
			||||||
	for schedule in return_schedules.iter_mut() {
 | 
					
 | 
				
			||||||
		schedule.load_tags(&mut pool_conn);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
						Ok(HttpResponse::Ok().json(return_schedules))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,8 +59,7 @@ pub async fn show(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
						let schedule = Schedule::get_by_uid(&mut pool_conn, &uid).await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut return_schedule = ReturnSchedule::from(schedule);
 | 
						let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
 | 
				
			||||||
	return_schedule.load_tags(&mut pool_conn);
 | 
					 | 
				
			||||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
						Ok(HttpResponse::Ok().json(return_schedule))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,8 +76,7 @@ pub async fn add(
 | 
				
			||||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
							.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
				
			||||||
		.await?;
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut return_schedule = ReturnSchedule::from(new_schedule);
 | 
						let return_schedule = ReturnSchedule::from_schedule(new_schedule, &mut pool_conn);
 | 
				
			||||||
	return_schedule.load_tags(&mut pool_conn);
 | 
					 | 
				
			||||||
	Ok(HttpResponse::Created().json(return_schedule))
 | 
						Ok(HttpResponse::Created().json(return_schedule))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -117,27 +109,15 @@ pub async fn add_list(
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		.collect();
 | 
							.collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	match vec_has_error(&result) {
 | 
						let mut return_schedules: Vec<ReturnSchedule> = Vec::new();
 | 
				
			||||||
		true => Ok(HttpResponse::from(
 | 
						for schedule in result {
 | 
				
			||||||
			result
 | 
							match schedule {
 | 
				
			||||||
				.into_iter()
 | 
								Ok(schedule) => return_schedules.push(ReturnSchedule::from_schedule(schedule, &mut pool_conn)),
 | 
				
			||||||
				.find(|r| r.is_err())
 | 
								Err(e) => return Ok(HttpResponse::from(e)),
 | 
				
			||||||
				.unwrap()
 | 
							}
 | 
				
			||||||
				.unwrap_err(),
 | 
					 | 
				
			||||||
		)),
 | 
					 | 
				
			||||||
		false => {
 | 
					 | 
				
			||||||
			let mut return_schedules: Vec<ReturnSchedule> = result
 | 
					 | 
				
			||||||
				.iter()
 | 
					 | 
				
			||||||
				.map(|s| ReturnSchedule::from(s.as_ref().unwrap()))
 | 
					 | 
				
			||||||
				.collect();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			for schedule in return_schedules.iter_mut() {
 | 
					 | 
				
			||||||
				schedule.load_tags(&mut pool_conn);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	Ok(HttpResponse::Created().json(return_schedules))
 | 
						Ok(HttpResponse::Created().json(return_schedules))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[put("/api/v1/schedules/{schedule_id}")]
 | 
					#[put("/api/v1/schedules/{schedule_id}")]
 | 
				
			||||||
pub async fn update(
 | 
					pub async fn update(
 | 
				
			||||||
| 
						 | 
					@ -160,8 +140,7 @@ pub async fn update(
 | 
				
			||||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
							.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
				
			||||||
		.await?;
 | 
							.await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let mut return_schedule = ReturnSchedule::from(schedule);
 | 
						let return_schedule = ReturnSchedule::from_schedule(schedule, &mut pool_conn);
 | 
				
			||||||
	return_schedule.load_tags(&mut pool_conn);
 | 
					 | 
				
			||||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
						Ok(HttpResponse::Ok().json(return_schedule))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,9 @@
 | 
				
			||||||
use crate::db::Schedule;
 | 
					use crate::db::{Controller, Relay, Schedule};
 | 
				
			||||||
use futures::executor;
 | 
					use futures::executor;
 | 
				
			||||||
use serde::Serialize;
 | 
					use serde::Serialize;
 | 
				
			||||||
use sqlx::pool::PoolConnection;
 | 
					use sqlx::pool::PoolConnection;
 | 
				
			||||||
use sqlx::Sqlite;
 | 
					use sqlx::Sqlite;
 | 
				
			||||||
 | 
					use crate::db::types::ControllerUid;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Serialize)]
 | 
					#[derive(Debug, Serialize)]
 | 
				
			||||||
pub struct ReturnSchedule {
 | 
					pub struct ReturnSchedule {
 | 
				
			||||||
| 
						 | 
					@ -12,22 +13,46 @@ pub struct ReturnSchedule {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl ReturnSchedule {
 | 
					impl ReturnSchedule {
 | 
				
			||||||
	pub fn load_tags(&mut self, conn: &mut PoolConnection<Sqlite>) {
 | 
						pub fn from_schedule(schedule: Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
				
			||||||
		self.tags = executor::block_on(self.schedule.get_tags(conn)).unwrap();
 | 
							let schedule = schedule.clone();
 | 
				
			||||||
	}
 | 
							let tags = executor::block_on(schedule.get_tags(conn)).unwrap();
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl From<Schedule> for ReturnSchedule {
 | 
					 | 
				
			||||||
	fn from(schedule: Schedule) -> Self {
 | 
					 | 
				
			||||||
		ReturnSchedule {
 | 
							ReturnSchedule {
 | 
				
			||||||
			schedule,
 | 
								schedule,
 | 
				
			||||||
			tags: vec![],
 | 
								tags,
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl From<&Schedule> for ReturnSchedule {
 | 
						pub fn from_schedule_ref(schedule: &Schedule, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
				
			||||||
	fn from(schedule: &Schedule) -> Self {
 | 
							Self::from_schedule(schedule.clone(), conn)
 | 
				
			||||||
		ReturnSchedule::from(schedule.clone())
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Serialize)]
 | 
				
			||||||
 | 
					pub struct ReturnRelay {
 | 
				
			||||||
 | 
						#[serde(flatten)]
 | 
				
			||||||
 | 
						pub relay: Relay,
 | 
				
			||||||
 | 
						pub controller: Controller,
 | 
				
			||||||
 | 
						pub controller_id: ControllerUid,
 | 
				
			||||||
 | 
						pub tags: Vec<String>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl ReturnRelay {
 | 
				
			||||||
 | 
						pub fn from_relay(relay: Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
				
			||||||
 | 
							let relay = relay.clone();
 | 
				
			||||||
 | 
							let controller = executor::block_on(Controller::get(conn, relay.controller_id)).unwrap();
 | 
				
			||||||
 | 
							let controller_uid = controller.uid.clone();
 | 
				
			||||||
 | 
							let tags = executor::block_on(relay.get_tags(conn)).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ReturnRelay {
 | 
				
			||||||
 | 
								relay,
 | 
				
			||||||
 | 
								controller,
 | 
				
			||||||
 | 
								controller_id: controller_uid,
 | 
				
			||||||
 | 
								tags,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pub fn from_relay_ref(relay: &Relay, conn: &mut PoolConnection<Sqlite>) -> Self {
 | 
				
			||||||
 | 
							Self::from_relay(relay.clone(), conn)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,3 @@
 | 
				
			||||||
pub fn vec_has_error<T, E>(target: &[Result<T, E>]) -> bool {
 | 
					 | 
				
			||||||
	target.iter().any(|t| t.is_err())
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub fn load_settings<T>(config_name: &str, env_prefix: &str) -> T
 | 
					pub fn load_settings<T>(config_name: &str, env_prefix: &str) -> T
 | 
				
			||||||
where
 | 
					where
 | 
				
			||||||
	for<'de> T: serde::Deserialize<'de>
 | 
						for<'de> T: serde::Deserialize<'de>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue