From db902f5549b143f4b25a8f6e293978572426c855 Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 9 Oct 2024 17:23:38 +0200 Subject: [PATCH] added delete node endpoint --- crates/backend/src/controller/node.rs | 26 +++++++++++++++++++++++--- crates/backend/src/routes.rs | 4 ++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/backend/src/controller/node.rs b/crates/backend/src/controller/node.rs index 3c3bf74..6c638c2 100644 --- a/crates/backend/src/controller/node.rs +++ b/crates/backend/src/controller/node.rs @@ -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 sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel}; use serde::{Deserialize, Serialize}; - +use uuid::Uuid; use crate::AppState; #[derive(Deserialize)] @@ -85,7 +86,10 @@ pub async fn create_node( coord_la: ActiveValue::Set(node.coord_la), coord_lo: ActiveValue::Set(node.coord_lo), temperature: ActiveValue::NotSet, - battery: ActiveValue::NotSet, + battery_minimum: ActiveValue::NotSet, + battery_current: ActiveValue::NotSet, + battery_maximum: ActiveValue::NotSet, + voltage: ActiveValue::NotSet, uptime: ActiveValue::NotSet, group: ActiveValue::Set(node.group), }; @@ -94,3 +98,19 @@ pub async fn create_node( Ok(web::Json(result)) } + +pub async fn delete_node( + state: web::Data, + path: web::Path, +) -> actix_web::Result { + 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()) +} \ No newline at end of file diff --git a/crates/backend/src/routes.rs b/crates/backend/src/routes.rs index 42aebff..9e6d66d 100644 --- a/crates/backend/src/routes.rs +++ b/crates/backend/src/routes.rs @@ -19,6 +19,10 @@ pub fn config(cfg: &mut web::ServiceConfig) { .get(node::get_nodes) .post(node::create_node), ) + .service( + web::resource("/nodes/{id}") + .delete(node::delete_node) + ) .service(web::resource("/groups").post(node::create_group)), ); }