Set options on request types

This commit is contained in:
Tobias Reisinger 2023-12-05 02:41:43 +01:00
parent 2a82cf79c4
commit e2cd84b136
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
4 changed files with 96 additions and 33 deletions
emgauwa-core/src/handlers/v1

View file

@ -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<Pool<Sqlite>>,
app_server: web::Data<Addr<AppServer>>,
path: web::Path<(String, i64)>,
data: web::Json<RequestRelay>,
data: web::Json<RequestUpdateRelay>,
) -> Result<HttpResponse, EmgauwaError> {
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)?;