Fix bugs and add controller action for controller ws
This commit is contained in:
parent
8b1affd8c7
commit
6f8d63e7be
11 changed files with 177 additions and 56 deletions
emgauwa-core/src/handlers/v1
|
@ -1,10 +1,14 @@
|
|||
use actix::Addr;
|
||||
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, RequestUpdateController};
|
||||
use emgauwa_lib::types::{ControllerUid, ControllerWsAction, RequestUpdateController};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
use crate::app_state;
|
||||
use crate::app_state::AppServer;
|
||||
|
||||
#[get("/api/v1/controllers")]
|
||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
@ -37,6 +41,7 @@ pub async fn show(
|
|||
#[put("/api/v1/controllers/{controller_id}")]
|
||||
pub async fn update(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
app_server: web::Data<Addr<AppServer>>,
|
||||
path: web::Path<(String,)>,
|
||||
data: web::Json<RequestUpdateController>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
|
@ -54,12 +59,21 @@ pub async fn update(
|
|||
.await?;
|
||||
|
||||
let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
|
||||
|
||||
app_server
|
||||
.send(app_state::Action {
|
||||
controller_uid: uid.clone(),
|
||||
action: ControllerWsAction::Controller(return_controller.clone()),
|
||||
})
|
||||
.await??;
|
||||
|
||||
Ok(HttpResponse::Ok().json(return_controller))
|
||||
}
|
||||
|
||||
#[delete("/api/v1/controllers/{controller_id}")]
|
||||
pub async fn delete(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
app_server: web::Data<Addr<AppServer>>,
|
||||
path: web::Path<(String,)>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
@ -67,6 +81,12 @@ pub async fn delete(
|
|||
let (controller_uid,) = path.into_inner();
|
||||
let uid = ControllerUid::try_from(controller_uid.as_str())?;
|
||||
|
||||
app_server
|
||||
.send(app_state::DisconnectController {
|
||||
controller_uid: uid.clone(),
|
||||
})
|
||||
.await??;
|
||||
|
||||
DbController::delete_by_uid(&mut pool_conn, uid).await?;
|
||||
Ok(HttpResponse::Ok().json("controller got deleted"))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue