Refactor project into workspaces
This commit is contained in:
		
							parent
							
								
									131bdeec78
								
							
						
					
					
						commit
						bacea1e3e9
					
				
					 31 changed files with 119 additions and 99 deletions
				
			
		
							
								
								
									
										23
									
								
								emgauwa-lib/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								emgauwa-lib/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
[package]
 | 
			
		||||
name = "emgauwa-lib"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2021"
 | 
			
		||||
authors = ["Tobias Reisinger <tobias@msrg.cc>"]
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
actix = "0.13"
 | 
			
		||||
actix-web = "4.4"
 | 
			
		||||
actix-web-actors = "4.2"
 | 
			
		||||
 | 
			
		||||
serde = "1.0"
 | 
			
		||||
serde_json = "1.0"
 | 
			
		||||
serde_derive = "1.0"
 | 
			
		||||
 | 
			
		||||
chrono = { version = "0.4", features = ["serde"] }
 | 
			
		||||
 | 
			
		||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] }
 | 
			
		||||
libsqlite3-sys = { version = "*", features = ["bundled"] }
 | 
			
		||||
 | 
			
		||||
log = "0.4"
 | 
			
		||||
uuid = "1.6"
 | 
			
		||||
futures = "0.3"
 | 
			
		||||
							
								
								
									
										4
									
								
								emgauwa-lib/build.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								emgauwa-lib/build.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,4 @@
 | 
			
		|||
fn main() {
 | 
			
		||||
	println!("cargo:rerun-if-changed=migrations");
 | 
			
		||||
	println!("cargo:rustc-env=DATABASE_URL=sqlite://emgauwa-dev.sqlite")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								emgauwa-lib/migrations/20231120000000_init.down.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								emgauwa-lib/migrations/20231120000000_init.down.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
DROP TABLE macro_actions;
 | 
			
		||||
DROP TABLE macros;
 | 
			
		||||
DROP TABLE junction_relay_schedule;
 | 
			
		||||
DROP TABLE junction_tag;
 | 
			
		||||
DROP TABLE tags;
 | 
			
		||||
DROP TABLE schedules;
 | 
			
		||||
DROP TABLE relays;
 | 
			
		||||
DROP TABLE controllers;
 | 
			
		||||
							
								
								
									
										162
									
								
								emgauwa-lib/migrations/20231120000000_init.up.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										162
									
								
								emgauwa-lib/migrations/20231120000000_init.up.sql
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,162 @@
 | 
			
		|||
CREATE TABLE controllers
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    uid
 | 
			
		||||
        VARCHAR(36)
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        UNIQUE,
 | 
			
		||||
    name
 | 
			
		||||
        VARCHAR(128)
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    ip
 | 
			
		||||
        VARCHAR(16),
 | 
			
		||||
    port
 | 
			
		||||
        INTEGER,
 | 
			
		||||
    relay_count
 | 
			
		||||
        INTEGER,
 | 
			
		||||
    active
 | 
			
		||||
        BOOLEAN
 | 
			
		||||
        NOT NULL
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE relays
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    name
 | 
			
		||||
        VARCHAR(128)
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    number
 | 
			
		||||
        INTEGER
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    controller_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        REFERENCES controllers (id)
 | 
			
		||||
            ON DELETE CASCADE
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE schedules
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    uid
 | 
			
		||||
        BLOB
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        UNIQUE,
 | 
			
		||||
    name
 | 
			
		||||
        VARCHAR(128)
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    periods
 | 
			
		||||
        BLOB
 | 
			
		||||
        NOT NULL
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE tags
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    tag
 | 
			
		||||
        VARCHAR(128)
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        UNIQUE
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE junction_tag
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    tag_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        REFERENCES tags (id)
 | 
			
		||||
            ON DELETE CASCADE,
 | 
			
		||||
    relay_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        REFERENCES relays (id)
 | 
			
		||||
            ON DELETE CASCADE,
 | 
			
		||||
    schedule_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        REFERENCES schedules (id)
 | 
			
		||||
            ON DELETE CASCADE
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE junction_relay_schedule
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    weekday
 | 
			
		||||
        SMALLINT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    relay_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        REFERENCES relays (id)
 | 
			
		||||
            ON DELETE CASCADE,
 | 
			
		||||
    schedule_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        DEFAULT 1
 | 
			
		||||
        REFERENCES schedules (id)
 | 
			
		||||
            ON DELETE SET DEFAULT
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE macros
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    uid
 | 
			
		||||
        VARCHAR(36)
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        UNIQUE,
 | 
			
		||||
    name
 | 
			
		||||
        VARCHAR(128)
 | 
			
		||||
        NOT NULL
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE macro_actions
 | 
			
		||||
(
 | 
			
		||||
    id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        PRIMARY KEY
 | 
			
		||||
        AUTOINCREMENT
 | 
			
		||||
        NOT NULL,
 | 
			
		||||
    macro_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        REFERENCES macros (id)
 | 
			
		||||
            ON DELETE CASCADE,
 | 
			
		||||
    relay_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        REFERENCES relays (id)
 | 
			
		||||
            ON DELETE CASCADE,
 | 
			
		||||
    schedule_id
 | 
			
		||||
        INTEGER
 | 
			
		||||
        NOT NULL
 | 
			
		||||
        REFERENCES schedules (id)
 | 
			
		||||
            ON DELETE CASCADE,
 | 
			
		||||
    weekday
 | 
			
		||||
        SMALLINT
 | 
			
		||||
        NOT NULL
 | 
			
		||||
);
 | 
			
		||||
							
								
								
									
										70
									
								
								emgauwa-lib/src/db/errors.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								emgauwa-lib/src/db/errors.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,70 @@
 | 
			
		|||
use actix_web::http::StatusCode;
 | 
			
		||||
use actix_web::HttpResponse;
 | 
			
		||||
use serde::ser::SerializeStruct;
 | 
			
		||||
use serde::{Serialize, Serializer};
 | 
			
		||||
use sqlx::Error;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum DatabaseError {
 | 
			
		||||
	DeleteError,
 | 
			
		||||
	InsertError,
 | 
			
		||||
	InsertGetError,
 | 
			
		||||
	NotFound,
 | 
			
		||||
	Protected,
 | 
			
		||||
	UpdateError,
 | 
			
		||||
	Unknown,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl DatabaseError {
 | 
			
		||||
	pub fn get_code(&self) -> StatusCode {
 | 
			
		||||
		match self {
 | 
			
		||||
			DatabaseError::NotFound => StatusCode::NOT_FOUND,
 | 
			
		||||
			DatabaseError::Protected => StatusCode::FORBIDDEN,
 | 
			
		||||
			_ => StatusCode::INTERNAL_SERVER_ERROR,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for DatabaseError {
 | 
			
		||||
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
		S: Serializer,
 | 
			
		||||
	{
 | 
			
		||||
		let mut s = serializer.serialize_struct("error", 3)?;
 | 
			
		||||
		s.serialize_field("type", "database-error")?;
 | 
			
		||||
		s.serialize_field("code", &self.get_code().as_u16())?;
 | 
			
		||||
		s.serialize_field("description", &String::from(self))?;
 | 
			
		||||
		s.end()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&DatabaseError> for String {
 | 
			
		||||
	fn from(err: &DatabaseError) -> Self {
 | 
			
		||||
		match err {
 | 
			
		||||
			DatabaseError::InsertError => String::from("error on inserting into database"),
 | 
			
		||||
			DatabaseError::InsertGetError => {
 | 
			
		||||
				String::from("error on retrieving new entry from database (your entry was saved)")
 | 
			
		||||
			}
 | 
			
		||||
			DatabaseError::NotFound => String::from("model was not found in database"),
 | 
			
		||||
			DatabaseError::DeleteError => String::from("error on deleting from database"),
 | 
			
		||||
			DatabaseError::Protected => String::from("model is protected"),
 | 
			
		||||
			DatabaseError::UpdateError => String::from("error on updating the model"),
 | 
			
		||||
			DatabaseError::Unknown => String::from("unknown error"),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<DatabaseError> for HttpResponse {
 | 
			
		||||
	fn from(err: DatabaseError) -> Self {
 | 
			
		||||
		HttpResponse::build(err.get_code()).json(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Error> for DatabaseError {
 | 
			
		||||
	fn from(value: Error) -> Self {
 | 
			
		||||
		match value {
 | 
			
		||||
			Error::RowNotFound => DatabaseError::NotFound,
 | 
			
		||||
			_ => DatabaseError::Unknown,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										83
									
								
								emgauwa-lib/src/db/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								emgauwa-lib/src/db/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,83 @@
 | 
			
		|||
use log::{info, trace};
 | 
			
		||||
use sqlx::migrate::Migrator;
 | 
			
		||||
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::db::schedules::{Periods, Schedule};
 | 
			
		||||
use crate::db::types::EmgauwaUid;
 | 
			
		||||
 | 
			
		||||
pub(crate) mod errors;
 | 
			
		||||
mod model_utils;
 | 
			
		||||
mod models;
 | 
			
		||||
pub(crate) mod schedules;
 | 
			
		||||
pub(crate) mod tag;
 | 
			
		||||
pub mod types;
 | 
			
		||||
 | 
			
		||||
static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations"
 | 
			
		||||
 | 
			
		||||
pub async fn run_migrations(pool: &Pool<Sqlite>) {
 | 
			
		||||
	info!("Running migrations");
 | 
			
		||||
	MIGRATOR.run(pool).await.expect("Failed to run migrations.");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn init_schedule(
 | 
			
		||||
	pool: &Pool<Sqlite>,
 | 
			
		||||
	uid: &EmgauwaUid,
 | 
			
		||||
	name: &str,
 | 
			
		||||
	periods: Periods,
 | 
			
		||||
) -> Result<(), DatabaseError> {
 | 
			
		||||
	trace!("Initializing schedule {:?}", name);
 | 
			
		||||
	match Schedule::get_by_uid(&mut pool.acquire().await.unwrap(), uid).await {
 | 
			
		||||
		Ok(_) => Ok(()),
 | 
			
		||||
		Err(err) => match err {
 | 
			
		||||
			DatabaseError::NotFound => {
 | 
			
		||||
				trace!("Schedule {:?} not found, inserting", name);
 | 
			
		||||
				sqlx::query_as!(
 | 
			
		||||
					Schedule,
 | 
			
		||||
					"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
					uid,
 | 
			
		||||
					name,
 | 
			
		||||
					periods,
 | 
			
		||||
				)
 | 
			
		||||
				.fetch_optional(pool)
 | 
			
		||||
				.await?
 | 
			
		||||
				.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
				.map(|_| ())
 | 
			
		||||
			}
 | 
			
		||||
			_ => Err(err),
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn init(db: &str) -> Pool<Sqlite> {
 | 
			
		||||
	let options = SqliteConnectOptions::from_str(db)
 | 
			
		||||
		.expect("Error parsing database path")
 | 
			
		||||
		.create_if_missing(true);
 | 
			
		||||
 | 
			
		||||
	let pool: Pool<Sqlite> = SqlitePoolOptions::new()
 | 
			
		||||
		.acquire_timeout(std::time::Duration::from_secs(1))
 | 
			
		||||
		.max_connections(5)
 | 
			
		||||
		.connect_with(options)
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Error connecting to database");
 | 
			
		||||
 | 
			
		||||
	run_migrations(&pool).await;
 | 
			
		||||
 | 
			
		||||
	init_schedule(&pool, &EmgauwaUid::Off, "Off", Periods(vec![]))
 | 
			
		||||
		.await
 | 
			
		||||
		.expect("Error initializing schedule Off");
 | 
			
		||||
 | 
			
		||||
	init_schedule(
 | 
			
		||||
		&pool,
 | 
			
		||||
		&EmgauwaUid::On,
 | 
			
		||||
		"On",
 | 
			
		||||
		Periods(vec![Period::new_on()]),
 | 
			
		||||
	)
 | 
			
		||||
	.await
 | 
			
		||||
	.expect("Error initializing schedule On");
 | 
			
		||||
 | 
			
		||||
	pool
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										111
									
								
								emgauwa-lib/src/db/model_utils.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								emgauwa-lib/src/db/model_utils.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,111 @@
 | 
			
		|||
use crate::db::schedules::Periods;
 | 
			
		||||
use chrono::{NaiveTime, Timelike};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::database::HasArguments;
 | 
			
		||||
use sqlx::encode::IsNull;
 | 
			
		||||
use sqlx::error::BoxDynError;
 | 
			
		||||
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
 | 
			
		||||
use sqlx::{Decode, Encode, Sqlite, Type};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
 | 
			
		||||
pub struct Period {
 | 
			
		||||
	#[serde(with = "period_format")]
 | 
			
		||||
	pub start: NaiveTime,
 | 
			
		||||
	#[serde(with = "period_format")]
 | 
			
		||||
	pub end: NaiveTime,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
mod period_format {
 | 
			
		||||
	use chrono::NaiveTime;
 | 
			
		||||
	use serde::{self, Deserialize, Deserializer, Serializer};
 | 
			
		||||
 | 
			
		||||
	const FORMAT: &str = "%H:%M";
 | 
			
		||||
 | 
			
		||||
	pub fn serialize<S>(time: &NaiveTime, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
		S: Serializer,
 | 
			
		||||
	{
 | 
			
		||||
		let s = format!("{}", time.format(FORMAT));
 | 
			
		||||
		serializer.serialize_str(&s)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveTime, D::Error>
 | 
			
		||||
	where
 | 
			
		||||
		D: Deserializer<'de>,
 | 
			
		||||
	{
 | 
			
		||||
		let s = String::deserialize(deserializer)?;
 | 
			
		||||
		NaiveTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Period {
 | 
			
		||||
	pub fn new(start: NaiveTime, end: NaiveTime) -> Self {
 | 
			
		||||
		Period { start, end }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub fn new_on() -> Self {
 | 
			
		||||
		Period {
 | 
			
		||||
			start: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
 | 
			
		||||
			end: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Type<Sqlite> for Periods {
 | 
			
		||||
	fn type_info() -> SqliteTypeInfo {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::type_info()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn compatible(ty: &SqliteTypeInfo) -> bool {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::compatible(ty)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'q> Encode<'q, Sqlite> for Periods {
 | 
			
		||||
	//noinspection DuplicatedCode
 | 
			
		||||
	fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
 | 
			
		||||
		<&Vec<u8> as Encode<Sqlite>>::encode(&Vec::from(self), buf)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'r> Decode<'r, Sqlite> for Periods {
 | 
			
		||||
	fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
 | 
			
		||||
		let blob = <&[u8] as Decode<Sqlite>>::decode(value)?;
 | 
			
		||||
		Ok(Periods::from(Vec::from(blob)))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&Periods> for Vec<u8> {
 | 
			
		||||
	fn from(periods: &Periods) -> Vec<u8> {
 | 
			
		||||
		periods
 | 
			
		||||
			.0
 | 
			
		||||
			.iter()
 | 
			
		||||
			.flat_map(|period| {
 | 
			
		||||
				let vec = vec![
 | 
			
		||||
					period.start.hour() as u8,
 | 
			
		||||
					period.start.minute() as u8,
 | 
			
		||||
					period.end.hour() as u8,
 | 
			
		||||
					period.end.minute() as u8,
 | 
			
		||||
				];
 | 
			
		||||
				vec
 | 
			
		||||
			})
 | 
			
		||||
			.collect()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Vec<u8>> for Periods {
 | 
			
		||||
	fn from(value: Vec<u8>) -> Self {
 | 
			
		||||
		let mut vec = Vec::new();
 | 
			
		||||
		for i in (3..value.len()).step_by(4) {
 | 
			
		||||
			let start_val_h: u32 = value[i - 3] as u32;
 | 
			
		||||
			let start_val_m: u32 = value[i - 2] as u32;
 | 
			
		||||
			let end_val_h: u32 = value[i - 1] as u32;
 | 
			
		||||
			let end_val_m: u32 = value[i] as u32;
 | 
			
		||||
			vec.push(Period {
 | 
			
		||||
				start: NaiveTime::from_hms_opt(start_val_h, start_val_m, 0).unwrap(),
 | 
			
		||||
				end: NaiveTime::from_hms_opt(end_val_h, end_val_m, 0).unwrap(),
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
		Periods(vec)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								emgauwa-lib/src/db/models.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								emgauwa-lib/src/db/models.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
use serde::Serialize;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	// TODO
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										152
									
								
								emgauwa-lib/src/db/schedules.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										152
									
								
								emgauwa-lib/src/db/schedules.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,152 @@
 | 
			
		|||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use std::borrow::Borrow;
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::model_utils::Period;
 | 
			
		||||
use crate::db::tag::Tag;
 | 
			
		||||
use crate::db::types::EmgauwaUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
	#[serde(skip)]
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	#[serde(rename(serialize = "id"))]
 | 
			
		||||
	pub uid: EmgauwaUid,
 | 
			
		||||
	pub name: String,
 | 
			
		||||
	pub periods: Periods,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
 | 
			
		||||
pub struct Periods(pub Vec<Period>);
 | 
			
		||||
 | 
			
		||||
impl Schedule {
 | 
			
		||||
	pub async fn get_all(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<Schedule>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Schedule, "SELECT * FROM schedules")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		filter_uid: &EmgauwaUid,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Schedule,
 | 
			
		||||
			"SELECT * FROM schedules WHERE uid = ?",
 | 
			
		||||
			filter_uid
 | 
			
		||||
		)
 | 
			
		||||
		.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<Schedule>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(Schedule, "SELECT schedule.* FROM schedules 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 delete_by_uid(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		filter_uid: EmgauwaUid,
 | 
			
		||||
	) -> Result<(), DatabaseError> {
 | 
			
		||||
		let filter_uid = match filter_uid {
 | 
			
		||||
			EmgauwaUid::Off => Err(DatabaseError::Protected),
 | 
			
		||||
			EmgauwaUid::On => Err(DatabaseError::Protected),
 | 
			
		||||
			EmgauwaUid::Any(_) => Ok(filter_uid),
 | 
			
		||||
		}?;
 | 
			
		||||
 | 
			
		||||
		sqlx::query!("DELETE FROM schedules WHERE uid = ?", filter_uid)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|res| match res.rows_affected() {
 | 
			
		||||
				0 => Err(DatabaseError::DeleteError),
 | 
			
		||||
				_ => Ok(()),
 | 
			
		||||
			})?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &Periods,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		let uid = EmgauwaUid::default();
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Schedule,
 | 
			
		||||
			"INSERT INTO schedules (uid, name, periods) VALUES (?, ?, ?) RETURNING *",
 | 
			
		||||
			uid,
 | 
			
		||||
			new_name,
 | 
			
		||||
			new_periods,
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn update(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_name: &str,
 | 
			
		||||
		new_periods: &Periods,
 | 
			
		||||
	) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
		// overwrite periods on protected schedules
 | 
			
		||||
		let new_periods = match self.uid {
 | 
			
		||||
			EmgauwaUid::Off | EmgauwaUid::On => self.periods.borrow(),
 | 
			
		||||
			EmgauwaUid::Any(_) => new_periods,
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
		sqlx::query!(
 | 
			
		||||
			"UPDATE schedules SET name = ?, periods = ? WHERE id = ?",
 | 
			
		||||
			new_name,
 | 
			
		||||
			new_periods,
 | 
			
		||||
			self.id,
 | 
			
		||||
		)
 | 
			
		||||
		.execute(conn.deref_mut())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
		Schedule::get_by_uid(conn, &self.uid).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.schedule_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 schedule_id = ?", self.id)
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
			.await?;
 | 
			
		||||
 | 
			
		||||
		for new_tag in new_tags {
 | 
			
		||||
			let tag: Option<Tag> =
 | 
			
		||||
				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?;
 | 
			
		||||
		}
 | 
			
		||||
		Ok(())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										81
									
								
								emgauwa-lib/src/db/tag.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								emgauwa-lib/src/db/tag.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,81 @@
 | 
			
		|||
use serde_derive::Serialize;
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::models::*;
 | 
			
		||||
use crate::db::schedules::Schedule;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Clone)]
 | 
			
		||||
pub struct Tag {
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub tag: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub struct JunctionTag {
 | 
			
		||||
	pub id: i64,
 | 
			
		||||
	pub tag_id: i64,
 | 
			
		||||
	pub relay_id: Option<i64>,
 | 
			
		||||
	pub schedule_id: Option<i64>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Tag {
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		new_tag: &str,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			Tag,
 | 
			
		||||
			"INSERT INTO tags (tag) VALUES (?) RETURNING *",
 | 
			
		||||
			new_tag
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_tag: &str,
 | 
			
		||||
	) -> Result<Tag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(Tag, "SELECT * FROM tags WHERE tag = ?", target_tag)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
			.await
 | 
			
		||||
			.map(|t| t.ok_or(DatabaseError::NotFound))?
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	#[allow(dead_code)]
 | 
			
		||||
	pub async fn link_relay(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_relay: &Relay,
 | 
			
		||||
	) -> Result<JunctionTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			JunctionTag,
 | 
			
		||||
			"INSERT INTO junction_tag (tag_id, relay_id) VALUES (?, ?) RETURNING *",
 | 
			
		||||
			self.id,
 | 
			
		||||
			target_relay.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn link_schedule(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		target_schedule: &Schedule,
 | 
			
		||||
	) -> Result<JunctionTag, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			JunctionTag,
 | 
			
		||||
			"INSERT INTO junction_tag (tag_id, schedule_id) VALUES (?, ?) RETURNING *",
 | 
			
		||||
			self.id,
 | 
			
		||||
			target_schedule.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await?
 | 
			
		||||
		.ok_or(DatabaseError::InsertGetError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										146
									
								
								emgauwa-lib/src/db/types/emgauwa_uid.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								emgauwa-lib/src/db/types/emgauwa_uid.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,146 @@
 | 
			
		|||
use std::convert::TryFrom;
 | 
			
		||||
use std::fmt::{Debug, Formatter};
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
use serde::{Serialize, Serializer};
 | 
			
		||||
use sqlx::database::HasArguments;
 | 
			
		||||
use sqlx::encode::IsNull;
 | 
			
		||||
use sqlx::error::BoxDynError;
 | 
			
		||||
use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
 | 
			
		||||
use sqlx::{Decode, Encode, Sqlite, Type};
 | 
			
		||||
use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub enum EmgauwaUid {
 | 
			
		||||
	Off,
 | 
			
		||||
	On,
 | 
			
		||||
	Any(Uuid),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl EmgauwaUid {
 | 
			
		||||
	const OFF_STR: &'static str = "off";
 | 
			
		||||
	const ON_STR: &'static str = "on";
 | 
			
		||||
	const OFF_U8: u8 = 0;
 | 
			
		||||
	const ON_U8: u8 = 1;
 | 
			
		||||
	const OFF_U128: u128 = 0;
 | 
			
		||||
	const ON_U128: u128 = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for EmgauwaUid {
 | 
			
		||||
	fn default() -> Self {
 | 
			
		||||
		EmgauwaUid::Any(Uuid::new_v4())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Debug for EmgauwaUid {
 | 
			
		||||
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | 
			
		||||
		match self {
 | 
			
		||||
			EmgauwaUid::Off => EmgauwaUid::OFF_STR.fmt(f),
 | 
			
		||||
			EmgauwaUid::On => EmgauwaUid::ON_STR.fmt(f),
 | 
			
		||||
			EmgauwaUid::Any(value) => value.fmt(f),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Type<Sqlite> for EmgauwaUid {
 | 
			
		||||
	fn type_info() -> SqliteTypeInfo {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::type_info()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn compatible(ty: &SqliteTypeInfo) -> bool {
 | 
			
		||||
		<&[u8] as Type<Sqlite>>::compatible(ty)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'q> Encode<'q, Sqlite> for EmgauwaUid {
 | 
			
		||||
	//noinspection DuplicatedCode
 | 
			
		||||
	fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
 | 
			
		||||
		<&Vec<u8> as Encode<Sqlite>>::encode(&Vec::from(self), buf)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl<'r> Decode<'r, Sqlite> for EmgauwaUid {
 | 
			
		||||
	fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
 | 
			
		||||
		Ok(EmgauwaUid::from(<&[u8] as Decode<Sqlite>>::decode(value)?))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for EmgauwaUid {
 | 
			
		||||
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
		S: Serializer,
 | 
			
		||||
	{
 | 
			
		||||
		String::from(self).serialize(serializer)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Uuid> for EmgauwaUid {
 | 
			
		||||
	fn from(uid: Uuid) -> EmgauwaUid {
 | 
			
		||||
		match uid.as_u128() {
 | 
			
		||||
			EmgauwaUid::OFF_U128 => EmgauwaUid::Off,
 | 
			
		||||
			EmgauwaUid::ON_U128 => EmgauwaUid::On,
 | 
			
		||||
			_ => EmgauwaUid::Any(uid),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl TryFrom<&str> for EmgauwaUid {
 | 
			
		||||
	type Error = uuid::Error;
 | 
			
		||||
 | 
			
		||||
	fn try_from(value: &str) -> Result<Self, Self::Error> {
 | 
			
		||||
		match value {
 | 
			
		||||
			EmgauwaUid::OFF_STR => Ok(EmgauwaUid::Off),
 | 
			
		||||
			EmgauwaUid::ON_STR => Ok(EmgauwaUid::On),
 | 
			
		||||
			any => match Uuid::from_str(any) {
 | 
			
		||||
				Ok(uuid) => Ok(EmgauwaUid::Any(uuid)),
 | 
			
		||||
				Err(err) => Err(err),
 | 
			
		||||
			},
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&EmgauwaUid> for Uuid {
 | 
			
		||||
	fn from(emgauwa_uid: &EmgauwaUid) -> Uuid {
 | 
			
		||||
		match emgauwa_uid {
 | 
			
		||||
			EmgauwaUid::Off => Uuid::from_u128(EmgauwaUid::OFF_U128),
 | 
			
		||||
			EmgauwaUid::On => Uuid::from_u128(EmgauwaUid::ON_U128),
 | 
			
		||||
			EmgauwaUid::Any(value) => *value,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&EmgauwaUid> for String {
 | 
			
		||||
	fn from(emgauwa_uid: &EmgauwaUid) -> String {
 | 
			
		||||
		match emgauwa_uid {
 | 
			
		||||
			EmgauwaUid::Off => String::from(EmgauwaUid::OFF_STR),
 | 
			
		||||
			EmgauwaUid::On => String::from(EmgauwaUid::ON_STR),
 | 
			
		||||
			EmgauwaUid::Any(value) => value.as_hyphenated().to_string(),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&EmgauwaUid> for Vec<u8> {
 | 
			
		||||
	fn from(emgauwa_uid: &EmgauwaUid) -> Vec<u8> {
 | 
			
		||||
		match emgauwa_uid {
 | 
			
		||||
			EmgauwaUid::Off => vec![EmgauwaUid::OFF_U8],
 | 
			
		||||
			EmgauwaUid::On => vec![EmgauwaUid::ON_U8],
 | 
			
		||||
			EmgauwaUid::Any(value) => value.as_bytes().to_vec(),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&[u8]> for EmgauwaUid {
 | 
			
		||||
	fn from(value: &[u8]) -> Self {
 | 
			
		||||
		match value {
 | 
			
		||||
			[EmgauwaUid::OFF_U8] => EmgauwaUid::Off,
 | 
			
		||||
			[EmgauwaUid::ON_U8] => EmgauwaUid::On,
 | 
			
		||||
			value_bytes => EmgauwaUid::Any(Uuid::from_slice(value_bytes).unwrap()),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Vec<u8>> for EmgauwaUid {
 | 
			
		||||
	fn from(value: Vec<u8>) -> Self {
 | 
			
		||||
		EmgauwaUid::from(value.as_slice())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								emgauwa-lib/src/db/types/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								emgauwa-lib/src/db/types/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
pub mod emgauwa_uid;
 | 
			
		||||
pub use emgauwa_uid::EmgauwaUid;
 | 
			
		||||
							
								
								
									
										82
									
								
								emgauwa-lib/src/handlers/errors.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								emgauwa-lib/src/handlers/errors.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,82 @@
 | 
			
		|||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use actix_web::http::StatusCode;
 | 
			
		||||
use actix_web::HttpResponse;
 | 
			
		||||
use serde::ser::SerializeStruct;
 | 
			
		||||
use serde::{Serialize, Serializer};
 | 
			
		||||
use std::fmt::{Display, Formatter};
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum ApiError {
 | 
			
		||||
	BadUid,
 | 
			
		||||
	ProtectedSchedule,
 | 
			
		||||
	DatabaseError(DatabaseError),
 | 
			
		||||
	InternalError(String),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ApiError {
 | 
			
		||||
	fn get_code(&self) -> StatusCode {
 | 
			
		||||
		match self {
 | 
			
		||||
			ApiError::BadUid => StatusCode::BAD_REQUEST,
 | 
			
		||||
			ApiError::ProtectedSchedule => StatusCode::FORBIDDEN,
 | 
			
		||||
			ApiError::DatabaseError(db_error) => db_error.get_code(),
 | 
			
		||||
			ApiError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for ApiError {
 | 
			
		||||
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
		S: Serializer,
 | 
			
		||||
	{
 | 
			
		||||
		let mut s = serializer.serialize_struct("error", 2)?;
 | 
			
		||||
		s.serialize_field("code", &self.get_code().as_u16())?;
 | 
			
		||||
		s.serialize_field("description", &String::from(self))?;
 | 
			
		||||
		s.end()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&ApiError> for String {
 | 
			
		||||
	fn from(err: &ApiError) -> Self {
 | 
			
		||||
		match err {
 | 
			
		||||
			ApiError::BadUid => String::from("the uid is in a bad format"),
 | 
			
		||||
			ApiError::ProtectedSchedule => String::from("the targeted schedule is protected"),
 | 
			
		||||
			ApiError::DatabaseError(db_err) => String::from(db_err),
 | 
			
		||||
			ApiError::InternalError(msg) => msg.clone(),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&ApiError> for HttpResponse {
 | 
			
		||||
	fn from(err: &ApiError) -> Self {
 | 
			
		||||
		HttpResponse::build(err.get_code()).json(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Display for ApiError {
 | 
			
		||||
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | 
			
		||||
		write!(f, "{}: {}", self.get_code(), String::from(self))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl actix_web::error::ResponseError for ApiError {
 | 
			
		||||
	fn status_code(&self) -> StatusCode {
 | 
			
		||||
		self.get_code()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn error_response(&self) -> HttpResponse {
 | 
			
		||||
		HttpResponse::from(self)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<sqlx::Error> for ApiError {
 | 
			
		||||
	fn from(err: sqlx::Error) -> Self {
 | 
			
		||||
		ApiError::DatabaseError(DatabaseError::from(err))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<DatabaseError> for ApiError {
 | 
			
		||||
	fn from(err: DatabaseError) -> Self {
 | 
			
		||||
		ApiError::DatabaseError(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								emgauwa-lib/src/handlers/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								emgauwa-lib/src/handlers/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,38 @@
 | 
			
		|||
use actix_web::{error, Error, HttpRequest, HttpResponse};
 | 
			
		||||
use serde::ser::SerializeStruct;
 | 
			
		||||
use serde::{Serialize, Serializer};
 | 
			
		||||
 | 
			
		||||
pub(crate) mod errors;
 | 
			
		||||
pub mod v1;
 | 
			
		||||
 | 
			
		||||
enum EmgauwaJsonPayLoadError {
 | 
			
		||||
	Error(error::JsonPayloadError),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Serialize for EmgauwaJsonPayLoadError {
 | 
			
		||||
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
			
		||||
	where
 | 
			
		||||
		S: Serializer,
 | 
			
		||||
	{
 | 
			
		||||
		let mut s = serializer.serialize_struct("error", 3)?;
 | 
			
		||||
		s.serialize_field("type", "json-payload-error")?;
 | 
			
		||||
		s.serialize_field("code", &400)?;
 | 
			
		||||
		s.serialize_field(
 | 
			
		||||
			"description",
 | 
			
		||||
			&match self {
 | 
			
		||||
				EmgauwaJsonPayLoadError::Error(err) => format!("{}", err),
 | 
			
		||||
			},
 | 
			
		||||
		)?;
 | 
			
		||||
		s.end()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn json_error_handler(err: error::JsonPayloadError, _: &HttpRequest) -> Error {
 | 
			
		||||
	error::InternalError::from_response(
 | 
			
		||||
		"",
 | 
			
		||||
		HttpResponse::BadRequest()
 | 
			
		||||
			.content_type("application/json")
 | 
			
		||||
			.json(EmgauwaJsonPayLoadError::Error(err)),
 | 
			
		||||
	)
 | 
			
		||||
	.into()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								emgauwa-lib/src/handlers/v1/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								emgauwa-lib/src/handlers/v1/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
pub mod schedules;
 | 
			
		||||
pub mod ws;
 | 
			
		||||
							
								
								
									
										186
									
								
								emgauwa-lib/src/handlers/v1/schedules.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										186
									
								
								emgauwa-lib/src/handlers/v1/schedules.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,186 @@
 | 
			
		|||
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::schedules::*;
 | 
			
		||||
use crate::db::tag::Tag;
 | 
			
		||||
use crate::db::types::EmgauwaUid;
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::return_models::ReturnSchedule;
 | 
			
		||||
use crate::utils::vec_has_error;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct RequestSchedule {
 | 
			
		||||
	name: String,
 | 
			
		||||
	periods: Periods,
 | 
			
		||||
	tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/schedules")]
 | 
			
		||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let schedules = Schedule::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.iter().map(ReturnSchedule::from).collect();
 | 
			
		||||
	for schedule in return_schedules.iter_mut() {
 | 
			
		||||
		schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/schedules/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(&mut pool_conn, &tag).await?;
 | 
			
		||||
 | 
			
		||||
	let schedules = Schedule::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedules: Vec<ReturnSchedule> =
 | 
			
		||||
		schedules.iter().map(ReturnSchedule::from).collect();
 | 
			
		||||
	for schedule in return_schedules.iter_mut() {
 | 
			
		||||
		schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	}
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedules))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/schedules/{schedule_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 (schedule_uid,) = path.into_inner();
 | 
			
		||||
	let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
 | 
			
		||||
	let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedule = ReturnSchedule::from(schedule);
 | 
			
		||||
	return_schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[post("/api/v1/schedules")]
 | 
			
		||||
pub async fn add(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	data: web::Json<RequestSchedule>,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let new_schedule = Schedule::create(&mut pool_conn, &data.name, &data.periods).await?;
 | 
			
		||||
 | 
			
		||||
	new_schedule
 | 
			
		||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedule = ReturnSchedule::from(new_schedule);
 | 
			
		||||
	return_schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Created().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn add_list_single(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	request_schedule: &RequestSchedule,
 | 
			
		||||
) -> Result<Schedule, DatabaseError> {
 | 
			
		||||
	let new_schedule =
 | 
			
		||||
		Schedule::create(conn, &request_schedule.name, &request_schedule.periods).await?;
 | 
			
		||||
 | 
			
		||||
	new_schedule
 | 
			
		||||
		.set_tags(conn, request_schedule.tags.as_slice())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	Ok(new_schedule)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[post("/api/v1/schedules/list")]
 | 
			
		||||
pub async fn add_list(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	data: web::Json<Vec<RequestSchedule>>,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let result: Vec<Result<Schedule, DatabaseError>> = data
 | 
			
		||||
		.as_slice()
 | 
			
		||||
		.iter()
 | 
			
		||||
		.map(|request_schedule| {
 | 
			
		||||
			futures::executor::block_on(add_list_single(&mut pool_conn, request_schedule))
 | 
			
		||||
		})
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	match vec_has_error(&result) {
 | 
			
		||||
		true => Ok(HttpResponse::from(
 | 
			
		||||
			result
 | 
			
		||||
				.into_iter()
 | 
			
		||||
				.find(|r| r.is_err())
 | 
			
		||||
				.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))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[put("/api/v1/schedules/{schedule_id}")]
 | 
			
		||||
pub async fn update(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	path: web::Path<(String,)>,
 | 
			
		||||
	data: web::Json<RequestSchedule>,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let (schedule_uid,) = path.into_inner();
 | 
			
		||||
	let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
 | 
			
		||||
	let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
 | 
			
		||||
 | 
			
		||||
	let schedule = schedule
 | 
			
		||||
		.update(&mut pool_conn, data.name.as_str(), &data.periods)
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	schedule
 | 
			
		||||
		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
		.await?;
 | 
			
		||||
 | 
			
		||||
	let mut return_schedule = ReturnSchedule::from(schedule);
 | 
			
		||||
	return_schedule.load_tags(&mut pool_conn);
 | 
			
		||||
	Ok(HttpResponse::Ok().json(return_schedule))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[delete("/api/v1/schedules/{schedule_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 (schedule_uid,) = path.into_inner();
 | 
			
		||||
	let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
 | 
			
		||||
	match emgauwa_uid {
 | 
			
		||||
		EmgauwaUid::Off => Err(ApiError::ProtectedSchedule),
 | 
			
		||||
		EmgauwaUid::On => Err(ApiError::ProtectedSchedule),
 | 
			
		||||
		EmgauwaUid::Any(_) => {
 | 
			
		||||
			Schedule::delete_by_uid(&mut pool_conn, emgauwa_uid).await?;
 | 
			
		||||
			Ok(HttpResponse::Ok().json("schedule got deleted"))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								emgauwa-lib/src/handlers/v1/ws/controllers.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								emgauwa-lib/src/handlers/v1/ws/controllers.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,53 @@
 | 
			
		|||
use crate::db::schedules::Schedule;
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use actix::{Actor, StreamHandler};
 | 
			
		||||
use actix_web::{get, web, HttpRequest, HttpResponse};
 | 
			
		||||
use actix_web_actors::ws;
 | 
			
		||||
use actix_web_actors::ws::ProtocolError;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
use ws::Message;
 | 
			
		||||
 | 
			
		||||
struct ControllerWs {
 | 
			
		||||
	pub pool: Pool<Sqlite>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Actor for ControllerWs {
 | 
			
		||||
	type Context = ws::WebsocketContext<Self>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn get_schedules(pool: &mut Pool<Sqlite>) -> Result<Vec<Schedule>, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	Ok(Schedule::get_all(&mut pool_conn).await?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Handler for ws::Message message
 | 
			
		||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		||||
	fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
 | 
			
		||||
		let schedules = futures::executor::block_on(get_schedules(&mut self.pool)).unwrap();
 | 
			
		||||
		let schedules_json = serde_json::to_string(&schedules).unwrap();
 | 
			
		||||
		match msg {
 | 
			
		||||
			Ok(Message::Ping(msg)) => ctx.pong(&msg),
 | 
			
		||||
			Ok(Message::Text(_text)) => ctx.text(schedules_json),
 | 
			
		||||
			_ => {}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/ws/controllers")]
 | 
			
		||||
pub async fn index(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	req: HttpRequest,
 | 
			
		||||
	stream: web::Payload,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let resp = ws::start(
 | 
			
		||||
		ControllerWs {
 | 
			
		||||
			pool: pool.get_ref().clone(),
 | 
			
		||||
		},
 | 
			
		||||
		&req,
 | 
			
		||||
		stream,
 | 
			
		||||
	)
 | 
			
		||||
	.map_err(|_| ApiError::InternalError(String::from("error starting websocket")));
 | 
			
		||||
	println!("{:?}", resp);
 | 
			
		||||
	resp
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								emgauwa-lib/src/handlers/v1/ws/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								emgauwa-lib/src/handlers/v1/ws/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
pub mod controllers;
 | 
			
		||||
							
								
								
									
										4
									
								
								emgauwa-lib/src/lib.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								emgauwa-lib/src/lib.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,4 @@
 | 
			
		|||
pub mod db;
 | 
			
		||||
pub mod handlers;
 | 
			
		||||
pub mod return_models;
 | 
			
		||||
pub mod utils;
 | 
			
		||||
							
								
								
									
										33
									
								
								emgauwa-lib/src/return_models.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								emgauwa-lib/src/return_models.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
use crate::db::schedules::Schedule;
 | 
			
		||||
use futures::executor;
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize)]
 | 
			
		||||
pub struct ReturnSchedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub schedule: Schedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ReturnSchedule {
 | 
			
		||||
	pub fn load_tags(&mut self, conn: &mut PoolConnection<Sqlite>) {
 | 
			
		||||
		self.tags = executor::block_on(self.schedule.get_tags(conn)).unwrap();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Schedule> for ReturnSchedule {
 | 
			
		||||
	fn from(schedule: Schedule) -> Self {
 | 
			
		||||
		ReturnSchedule {
 | 
			
		||||
			schedule,
 | 
			
		||||
			tags: vec![],
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<&Schedule> for ReturnSchedule {
 | 
			
		||||
	fn from(schedule: &Schedule) -> Self {
 | 
			
		||||
		ReturnSchedule::from(schedule.clone())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								emgauwa-lib/src/utils.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								emgauwa-lib/src/utils.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
pub fn vec_has_error<T, E>(target: &[Result<T, E>]) -> bool {
 | 
			
		||||
	target.iter().any(|t| t.is_err())
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue