added delete node endpoint

This commit is contained in:
Mika Bomm 2024-10-09 17:23:38 +02:00
parent 190861da13
commit db902f5549
2 changed files with 27 additions and 3 deletions

View file

@ -1,8 +1,9 @@
use actix_web::{error::ErrorInternalServerError, http::header::ACCEPT, web, Responder}; use std::os::linux::raw::stat;
use actix_web::{error::ErrorInternalServerError, http::header::ACCEPT, web, HttpResponse, Responder};
use entity::node_group; use entity::node_group;
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel}; use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::AppState; use crate::AppState;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -85,7 +86,10 @@ pub async fn create_node(
coord_la: ActiveValue::Set(node.coord_la), coord_la: ActiveValue::Set(node.coord_la),
coord_lo: ActiveValue::Set(node.coord_lo), coord_lo: ActiveValue::Set(node.coord_lo),
temperature: ActiveValue::NotSet, temperature: ActiveValue::NotSet,
battery: ActiveValue::NotSet, battery_minimum: ActiveValue::NotSet,
battery_current: ActiveValue::NotSet,
battery_maximum: ActiveValue::NotSet,
voltage: ActiveValue::NotSet,
uptime: ActiveValue::NotSet, uptime: ActiveValue::NotSet,
group: ActiveValue::Set(node.group), group: ActiveValue::Set(node.group),
}; };
@ -94,3 +98,19 @@ pub async fn create_node(
Ok(web::Json(result)) Ok(web::Json(result))
} }
pub async fn delete_node(
state: web::Data<AppState>,
path: web::Path<Uuid>,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
let db = &state.db;
entity::node::Entity::delete_by_id(id)
.exec(db)
.await
.map_err(ErrorInternalServerError)?;
Ok(HttpResponse::Ok().finish())
}

View file

@ -19,6 +19,10 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.get(node::get_nodes) .get(node::get_nodes)
.post(node::create_node), .post(node::create_node),
) )
.service(
web::resource("/nodes/{id}")
.delete(node::delete_node)
)
.service(web::resource("/groups").post(node::create_group)), .service(web::resource("/groups").post(node::create_group)),
); );
} }