Move request models
This commit is contained in:
parent
b3228ea6b5
commit
2a82cf79c4
7 changed files with 48 additions and 32 deletions
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
@ -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<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
|
|
@ -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<String>,
|
||||
}
|
||||
|
||||
#[get("/api/v1/relays")]
|
||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
|
||||
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<Pool<Sqlite>>,
|
||||
app_server: web::Data<Addr<AppServer>>,
|
||||
path: web::Path<(String, i64)>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
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<Pool<Sqlite>>,
|
||||
app_server: web::Data<Addr<AppServer>>,
|
||||
path: web::Path<(String, i64)>,
|
||||
data: web::Json<RequestRelay>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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<String>,
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules")]
|
||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
|
|
@ -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};
|
||||
|
||||
|
|
29
emgauwa-lib/src/types/request.rs
Normal file
29
emgauwa-lib/src/types/request.rs
Normal file
|
@ -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<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestRelay {
|
||||
pub name: String,
|
||||
pub active_schedule: Option<RequestScheduleId>,
|
||||
pub schedules: Vec<RequestScheduleId>,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestScheduleId {
|
||||
pub id: ScheduleUid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RequestController {
|
||||
pub name: String,
|
||||
}
|
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue