From 2a82cf79c49b8fdf8e92191b3f117b2da1a442a6 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Tue, 5 Dec 2023 01:51:06 +0100 Subject: [PATCH] Move request models --- emgauwa-controller/src/main.rs | 4 +-- emgauwa-core/src/handlers/v1/controllers.rs | 8 +----- emgauwa-core/src/handlers/v1/relays.rs | 19 +++++++------- emgauwa-core/src/handlers/v1/schedules.rs | 12 ++------- emgauwa-lib/src/types/mod.rs | 2 ++ emgauwa-lib/src/types/request.rs | 29 +++++++++++++++++++++ emgauwa-lib/src/utils.rs | 6 ++--- 7 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 emgauwa-lib/src/types/request.rs diff --git a/emgauwa-controller/src/main.rs b/emgauwa-controller/src/main.rs index 6ff6f65..5a2b1e1 100644 --- a/emgauwa-controller/src/main.rs +++ b/emgauwa-controller/src/main.rs @@ -1,6 +1,6 @@ use emgauwa_lib::constants::WEBSOCKET_RETRY_TIMEOUT; use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbSchedule}; -use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; +use emgauwa_lib::errors::EmgauwaError; use emgauwa_lib::models::{Controller, FromDbModel}; use emgauwa_lib::types::{ControllerUid, ControllerWsAction}; use emgauwa_lib::{db, utils}; @@ -101,7 +101,7 @@ async fn main() -> Result<(), std::io::Error> { .map_err(EmgauwaError::from)? .pop() { - None => futures::executor::block_on(create_this_controller(&mut conn, &settings)), + None => futures::executor::block_on(create_this_controller(&mut conn, &settings))?, Some(c) => c, }; diff --git a/emgauwa-core/src/handlers/v1/controllers.rs b/emgauwa-core/src/handlers/v1/controllers.rs index 10fb2dc..e3a208a 100644 --- a/emgauwa-core/src/handlers/v1/controllers.rs +++ b/emgauwa-core/src/handlers/v1/controllers.rs @@ -2,15 +2,9 @@ use actix_web::{delete, get, put, web, HttpResponse}; use emgauwa_lib::db::DbController; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel}; -use emgauwa_lib::types::ControllerUid; -use serde_derive::{Deserialize, Serialize}; +use emgauwa_lib::types::{ControllerUid, RequestController}; use sqlx::{Pool, Sqlite}; -#[derive(Debug, Serialize, Deserialize)] -pub struct RequestController { - name: String, -} - #[get("/api/v1/controllers")] pub async fn index(pool: web::Data>) -> Result { let mut pool_conn = pool.acquire().await?; diff --git a/emgauwa-core/src/handlers/v1/relays.rs b/emgauwa-core/src/handlers/v1/relays.rs index 0f4ce50..51a6c2a 100644 --- a/emgauwa-core/src/handlers/v1/relays.rs +++ b/emgauwa-core/src/handlers/v1/relays.rs @@ -3,19 +3,12 @@ use actix_web::{get, put, web, HttpResponse}; use emgauwa_lib::db::{DbController, DbRelay, DbTag}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay}; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction}; -use serde::{Deserialize, Serialize}; +use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestRelay}; use sqlx::{Pool, Sqlite}; use crate::app_state; use crate::app_state::AppServer; -#[derive(Debug, Serialize, Deserialize)] -pub struct RequestRelay { - name: String, - tags: Vec, -} - #[get("/api/v1/relays")] pub async fn index(pool: web::Data>) -> Result { let mut pool_conn = pool.acquire().await?; @@ -68,7 +61,6 @@ pub async fn index_for_controller( #[get("/api/v1/controllers/{controller_id}/relays/{relay_num}")] pub async fn show_for_controller( pool: web::Data>, - app_server: web::Data>, path: web::Path<(String, i64)>, ) -> Result { let mut pool_conn = pool.acquire().await?; @@ -91,6 +83,7 @@ pub async fn show_for_controller( #[put("/api/v1/controllers/{controller_id}/relays/{relay_num}")] pub async fn update_for_controller( pool: web::Data>, + app_server: web::Data>, path: web::Path<(String, i64)>, data: web::Json, ) -> Result { @@ -112,5 +105,13 @@ pub async fn update_for_controller( relay.set_tags(&mut pool_conn, data.tags.as_slice()).await?; let return_relay = Relay::from_db_model(&mut pool_conn, relay)?; + + app_server + .send(app_state::Action { + controller_uid: uid, + action: ControllerWsAction::Relays(vec![return_relay.clone()]), + }) + .await??; + Ok(HttpResponse::Ok().json(return_relay)) } diff --git a/emgauwa-core/src/handlers/v1/schedules.rs b/emgauwa-core/src/handlers/v1/schedules.rs index 7e19ffa..2fcd1aa 100644 --- a/emgauwa-core/src/handlers/v1/schedules.rs +++ b/emgauwa-core/src/handlers/v1/schedules.rs @@ -1,19 +1,11 @@ use actix_web::{delete, get, post, put, web, HttpResponse}; -use emgauwa_lib::db::{DbPeriods, DbSchedule, DbTag}; +use emgauwa_lib::db::{DbSchedule, DbTag}; use emgauwa_lib::errors::{ApiError, DatabaseError, EmgauwaError}; use emgauwa_lib::models::{convert_db_list, FromDbModel, Schedule}; -use emgauwa_lib::types::ScheduleUid; -use serde::{Deserialize, Serialize}; +use emgauwa_lib::types::{RequestSchedule, ScheduleUid}; use sqlx::pool::PoolConnection; use sqlx::{Pool, Sqlite}; -#[derive(Debug, Serialize, Deserialize)] -pub struct RequestSchedule { - name: String, - periods: DbPeriods, - tags: Vec, -} - #[get("/api/v1/schedules")] pub async fn index(pool: web::Data>) -> Result { let mut pool_conn = pool.acquire().await?; diff --git a/emgauwa-lib/src/types/mod.rs b/emgauwa-lib/src/types/mod.rs index 3a7046f..0f936eb 100644 --- a/emgauwa-lib/src/types/mod.rs +++ b/emgauwa-lib/src/types/mod.rs @@ -1,8 +1,10 @@ mod controller_uid; +mod request; mod schedule_uid; use actix::Message; pub use controller_uid::ControllerUid; +pub use request::*; pub use schedule_uid::ScheduleUid; use serde_derive::{Deserialize, Serialize}; diff --git a/emgauwa-lib/src/types/request.rs b/emgauwa-lib/src/types/request.rs new file mode 100644 index 0000000..1a17629 --- /dev/null +++ b/emgauwa-lib/src/types/request.rs @@ -0,0 +1,29 @@ +use serde_derive::{Deserialize, Serialize}; + +use crate::db::DbPeriods; +use crate::types::ScheduleUid; + +#[derive(Debug, Serialize, Deserialize)] +pub struct RequestSchedule { + pub name: String, + pub periods: DbPeriods, + pub tags: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct RequestRelay { + pub name: String, + pub active_schedule: Option, + pub schedules: Vec, + pub tags: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct RequestScheduleId { + pub id: ScheduleUid, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct RequestController { + pub name: String, +} diff --git a/emgauwa-lib/src/utils.rs b/emgauwa-lib/src/utils.rs index 9f442b9..8c4f17e 100644 --- a/emgauwa-lib/src/utils.rs +++ b/emgauwa-lib/src/utils.rs @@ -27,15 +27,13 @@ where pub fn init_logging(level: &str) -> Result<(), EmgauwaError> { let log_level: LevelFilter = LevelFilter::from_str(level) - .map_err(|_| EmgauwaError::Other(format!("Invalid log level: {}", level.to_string())))?; + .map_err(|_| EmgauwaError::Other(format!("Invalid log level: {}", level)))?; log::trace!("Log level set to {:?}", log_level); SimpleLogger::new() .with_level(log_level) .init() - .map_err(|err| { - EmgauwaError::Other(format!("Failed to initialize logger: {}", err.to_string())) - })?; + .map_err(|err| EmgauwaError::Other(format!("Failed to initialize logger: {}", err)))?; Ok(()) }