From e2cd84b1367a581e303d062578a465463cf5e3e0 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Tue, 5 Dec 2023 02:41:43 +0100 Subject: [PATCH] Set options on request types --- emgauwa-core/src/handlers/v1/controllers.rs | 4 +- emgauwa-core/src/handlers/v1/relays.rs | 44 ++++++++++++++++++--- emgauwa-core/src/handlers/v1/schedules.rs | 44 +++++++++++++-------- emgauwa-lib/src/types/request.rs | 37 +++++++++++++---- 4 files changed, 96 insertions(+), 33 deletions(-) diff --git a/emgauwa-core/src/handlers/v1/controllers.rs b/emgauwa-core/src/handlers/v1/controllers.rs index e3a208a..8157c19 100644 --- a/emgauwa-core/src/handlers/v1/controllers.rs +++ b/emgauwa-core/src/handlers/v1/controllers.rs @@ -2,7 +2,7 @@ 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, RequestController}; +use emgauwa_lib::types::{ControllerUid, RequestUpdateController}; use sqlx::{Pool, Sqlite}; #[get("/api/v1/controllers")] @@ -38,7 +38,7 @@ pub async fn show( pub async fn update( pool: web::Data>, path: web::Path<(String,)>, - data: web::Json, + data: web::Json, ) -> 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 51a6c2a..4cfebd6 100644 --- a/emgauwa-core/src/handlers/v1/relays.rs +++ b/emgauwa-core/src/handlers/v1/relays.rs @@ -1,9 +1,10 @@ use actix::Addr; use actix_web::{get, put, web, HttpResponse}; -use emgauwa_lib::db::{DbController, DbRelay, DbTag}; +use emgauwa_lib::db::{DbController, DbJunctionRelaySchedule, DbRelay, DbTag}; use emgauwa_lib::errors::{DatabaseError, EmgauwaError}; use emgauwa_lib::models::{convert_db_list, FromDbModel, Relay}; -use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestRelay}; +use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestUpdateRelay}; +use emgauwa_lib::utils; use sqlx::{Pool, Sqlite}; use crate::app_state; @@ -85,7 +86,7 @@ pub async fn update_for_controller( pool: web::Data>, app_server: web::Data>, path: web::Path<(String, i64)>, - data: web::Json, + data: web::Json, ) -> Result { let mut pool_conn = pool.acquire().await?; @@ -96,13 +97,44 @@ pub async fn update_for_controller( .await? .ok_or(DatabaseError::NotFound)?; - let relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num) + let mut relay = DbRelay::get_by_controller_and_num(&mut pool_conn, &controller, relay_num) .await? .ok_or(DatabaseError::NotFound)?; - let relay = relay.update(&mut pool_conn, data.name.as_str()).await?; + if let Some(name) = &data.name { + relay = relay.update(&mut pool_conn, name.as_str()).await?; + } - relay.set_tags(&mut pool_conn, data.tags.as_slice()).await?; + if let Some(schedule_uids) = &data.schedules { + if schedule_uids.len() == 7 { + let mut schedules = Vec::new(); + for s_uid in schedule_uids { + schedules.push(s_uid.get_schedule(&mut pool_conn).await?); + } + + DbJunctionRelaySchedule::set_schedules( + &mut pool_conn, + &relay, + schedules.iter().collect(), + ) + .await?; + } + } + + if let Some(s_uid) = &data.active_schedule { + let schedule = s_uid.get_schedule(&mut pool_conn).await?; + DbJunctionRelaySchedule::set_schedule( + &mut pool_conn, + &relay, + &schedule, + utils::get_weekday(), + ) + .await?; + } + + if let Some(tags) = &data.tags { + relay.set_tags(&mut pool_conn, tags.as_slice()).await?; + } let return_relay = Relay::from_db_model(&mut pool_conn, relay)?; diff --git a/emgauwa-core/src/handlers/v1/schedules.rs b/emgauwa-core/src/handlers/v1/schedules.rs index 2fcd1aa..a1d1583 100644 --- a/emgauwa-core/src/handlers/v1/schedules.rs +++ b/emgauwa-core/src/handlers/v1/schedules.rs @@ -2,7 +2,7 @@ use actix_web::{delete, get, post, put, web, HttpResponse}; 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::{RequestSchedule, ScheduleUid}; +use emgauwa_lib::types::{RequestCreateSchedule, RequestUpdateSchedule, ScheduleUid}; use sqlx::pool::PoolConnection; use sqlx::{Pool, Sqlite}; @@ -55,7 +55,7 @@ pub async fn show( #[post("/api/v1/schedules")] pub async fn add( pool: web::Data>, - data: web::Json, + data: web::Json, ) -> Result { let mut pool_conn = pool.acquire().await?; @@ -67,9 +67,11 @@ pub async fn add( ) .await?; - new_schedule - .set_tags(&mut pool_conn, data.tags.as_slice()) - .await?; + if let Some(tags) = &data.tags { + new_schedule + .set_tags(&mut pool_conn, tags.as_slice()) + .await?; + } let return_schedule = Schedule::from_db_model(&mut pool_conn, new_schedule)?; Ok(HttpResponse::Created().json(return_schedule)) @@ -77,7 +79,7 @@ pub async fn add( async fn add_list_single( conn: &mut PoolConnection, - request_schedule: &RequestSchedule, + request_schedule: &RequestCreateSchedule, ) -> Result { let new_schedule = DbSchedule::create( conn, @@ -87,9 +89,9 @@ async fn add_list_single( ) .await?; - new_schedule - .set_tags(conn, request_schedule.tags.as_slice()) - .await?; + if let Some(tags) = &request_schedule.tags { + new_schedule.set_tags(conn, tags.as_slice()).await?; + } Ok(new_schedule) } @@ -97,7 +99,7 @@ async fn add_list_single( #[post("/api/v1/schedules/list")] pub async fn add_list( pool: web::Data>, - data: web::Json>, + data: web::Json>, ) -> Result { let mut pool_conn = pool.acquire().await?; @@ -115,7 +117,7 @@ pub async fn add_list( pub async fn update( pool: web::Data>, path: web::Path<(String,)>, - data: web::Json, + data: web::Json, ) -> Result { let mut pool_conn = pool.acquire().await?; @@ -126,13 +128,21 @@ pub async fn update( .await? .ok_or(DatabaseError::NotFound)?; - let schedule = schedule - .update(&mut pool_conn, data.name.as_str(), &data.periods) - .await?; + let name = match &data.name { + None => schedule.name.as_str(), + Some(name) => name.as_str(), + }; - schedule - .set_tags(&mut pool_conn, data.tags.as_slice()) - .await?; + let periods = match &data.periods { + None => &schedule.periods, + Some(period) => period, + }; + + let schedule = schedule.update(&mut pool_conn, name, periods).await?; + + if let Some(tags) = &data.tags { + schedule.set_tags(&mut pool_conn, tags.as_slice()).await?; + } let return_schedule = Schedule::from_db_model(&mut pool_conn, schedule)?; Ok(HttpResponse::Ok().json(return_schedule)) diff --git a/emgauwa-lib/src/types/request.rs b/emgauwa-lib/src/types/request.rs index 1a17629..646b49a 100644 --- a/emgauwa-lib/src/types/request.rs +++ b/emgauwa-lib/src/types/request.rs @@ -1,21 +1,31 @@ use serde_derive::{Deserialize, Serialize}; +use sqlx::pool::PoolConnection; +use sqlx::Sqlite; -use crate::db::DbPeriods; +use crate::db::{DbPeriods, DbSchedule}; +use crate::errors::DatabaseError; use crate::types::ScheduleUid; #[derive(Debug, Serialize, Deserialize)] -pub struct RequestSchedule { +pub struct RequestCreateSchedule { pub name: String, pub periods: DbPeriods, - pub tags: Vec, + pub tags: Option>, } #[derive(Debug, Serialize, Deserialize)] -pub struct RequestRelay { - pub name: String, +pub struct RequestUpdateSchedule { + pub name: Option, + pub periods: Option, + pub tags: Option>, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct RequestUpdateRelay { + pub name: Option, pub active_schedule: Option, - pub schedules: Vec, - pub tags: Vec, + pub schedules: Option>, + pub tags: Option>, } #[derive(Debug, Serialize, Deserialize)] @@ -24,6 +34,17 @@ pub struct RequestScheduleId { } #[derive(Debug, Serialize, Deserialize)] -pub struct RequestController { +pub struct RequestUpdateController { pub name: String, } + +impl RequestScheduleId { + pub async fn get_schedule( + &self, + conn: &mut PoolConnection, + ) -> Result { + DbSchedule::get_by_uid(conn, &self.id) + .await? + .ok_or(DatabaseError::NotFound) + } +}