Move request models

This commit is contained in:
Tobias Reisinger 2023-12-05 01:51:06 +01:00
parent b3228ea6b5
commit 2a82cf79c4
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
7 changed files with 48 additions and 32 deletions
emgauwa-core/src/handlers/v1

View file

@ -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))
}