Improve controller endpoint and model

This commit is contained in:
Tobias Reisinger 2023-11-30 01:43:56 +01:00
parent 2f51ebf91e
commit 6400b7745c
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
5 changed files with 103 additions and 7 deletions
emgauwa-core/src

View file

@ -1,10 +1,18 @@
use actix_web::{get, web, HttpResponse};
use actix_web::{delete, get, put, web, HttpResponse};
use emgauwa_lib::db::errors::DatabaseError;
use emgauwa_lib::db::DbController;
use emgauwa_lib::models::{convert_db_list, Controller};
use emgauwa_lib::models::{convert_db_list, Controller, FromDbModel};
use emgauwa_lib::types::ControllerUid;
use serde_derive::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use crate::handlers::errors::ApiError;
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestController {
name: String,
}
#[get("/api/v1/controllers")]
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
@ -15,3 +23,58 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
Ok(HttpResponse::Ok().json(controllers))
}
#[get("/api/v1/controllers/{controller_id}")]
pub async fn show(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
Ok(HttpResponse::Ok().json(return_controller))
}
#[put("/api/v1/controllers/{controller_id}")]
pub async fn update(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
data: web::Json<RequestController>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
let controller = DbController::get_by_uid(&mut pool_conn, &uid)
.await?
.ok_or(DatabaseError::NotFound)?;
let controller = controller
.update(&mut pool_conn, data.name.as_str(), controller.relay_count)
.await?;
let return_controller = Controller::from_db_model(&mut pool_conn, controller)?;
Ok(HttpResponse::Ok().json(return_controller))
}
#[delete("/api/v1/controllers/{controller_id}")]
pub async fn delete(
pool: web::Data<Pool<Sqlite>>,
path: web::Path<(String,)>,
) -> Result<HttpResponse, ApiError> {
let mut pool_conn = pool.acquire().await?;
let (controller_uid,) = path.into_inner();
let uid = ControllerUid::try_from(controller_uid.as_str()).or(Err(ApiError::BadUid))?;
DbController::delete_by_uid(&mut pool_conn, uid).await?;
Ok(HttpResponse::Ok().json("controller got deleted"))
}

View file

@ -57,6 +57,9 @@ async fn main() -> std::io::Result<()> {
.app_data(web::Data::new(pool.clone()))
.app_data(web::Data::new(connected_controllers.clone()))
.service(handlers::v1::controllers::index)
.service(handlers::v1::controllers::show)
.service(handlers::v1::controllers::update)
.service(handlers::v1::controllers::delete)
.service(handlers::v1::relays::index)
.service(handlers::v1::schedules::index)
.service(handlers::v1::schedules::tagged)