diff --git a/ApfelBruno/Node/Create node group.bru b/ApfelBruno/Node/Create node group.bru index e33232f..4badd84 100644 --- a/ApfelBruno/Node/Create node group.bru +++ b/ApfelBruno/Node/Create node group.bru @@ -5,7 +5,11 @@ meta { } post { - url: http://localhost:8080/api/v1/nodes - body: none + url: http://localhost:8080/api/v1/groups + body: json auth: none } + +body:json { + {"name":"testgroup"} +} diff --git a/ApfelBruno/Node/Create node.bru b/ApfelBruno/Node/Create node.bru index 15ae404..256d087 100644 --- a/ApfelBruno/Node/Create node.bru +++ b/ApfelBruno/Node/Create node.bru @@ -9,3 +9,12 @@ post { body: json auth: none } + +body:json { + { + "name":"some mac address", + "coord_la":1.123123, + "coord_lo":5.3123123, + "group":"efbd70a9-dc89-4c8d-9e6c-e7607c823df3" + } +} diff --git a/crates/backend/src/controller/node.rs b/crates/backend/src/controller/node.rs index 9df275d..3c3bf74 100644 --- a/crates/backend/src/controller/node.rs +++ b/crates/backend/src/controller/node.rs @@ -1,10 +1,23 @@ -use actix_web::{error::ErrorInternalServerError, web, Responder}; +use actix_web::{error::ErrorInternalServerError, http::header::ACCEPT, web, Responder}; use entity::node_group; use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel}; use serde::{Deserialize, Serialize}; use crate::AppState; +#[derive(Deserialize)] +pub struct CreateGroupWithoutId { + name: String, +} + +#[derive(Deserialize)] +pub struct CreateLicense { + name: String, + coord_la: f64, + coord_lo: f64, + group: uuid::Uuid, +} + #[derive(Serialize)] struct GroupWithNode { #[serde(flatten)] @@ -27,16 +40,55 @@ pub async fn get_nodes(state: web::Data) -> actix_web::Result, + group: web::Json, +) -> actix_web::Result { + let db = &state.db; + + let group = group.into_inner(); + + let group = entity::node_group::ActiveModel { + id: ActiveValue::NotSet, + name: ActiveValue::Set(group.name), + }; + + let result = group.insert(db).await.map_err(ErrorInternalServerError)?; + + Ok(web::Json(result)) +} + pub async fn create_node( state: web::Data, - node: web::Json, + node: web::Json, ) -> actix_web::Result { let db = &state.db; let node = node.into_inner(); - let mut node = node.into_active_model(); - node.id = ActiveValue::NotSet; + println!("Checking group ID: {:?}", node.group); + + let group_exists = entity::node_group::Entity::find_by_id(node.group) + .one(db) + .await + .map_err(ErrorInternalServerError)? + .is_some(); + + if !group_exists { + return Err(ErrorInternalServerError("Group ID does not exist")); + } + + let node = entity::node::ActiveModel { + id: ActiveValue::NotSet, + name: ActiveValue::Set(node.name), + status: ActiveValue::NotSet, + coord_la: ActiveValue::Set(node.coord_la), + coord_lo: ActiveValue::Set(node.coord_lo), + temperature: ActiveValue::NotSet, + battery: ActiveValue::NotSet, + uptime: ActiveValue::NotSet, + group: ActiveValue::Set(node.group), + }; let result = node.insert(db).await.map_err(ErrorInternalServerError)?; diff --git a/crates/backend/src/routes.rs b/crates/backend/src/routes.rs index d4c7371..42aebff 100644 --- a/crates/backend/src/routes.rs +++ b/crates/backend/src/routes.rs @@ -1,5 +1,5 @@ use crate::controller::{node, user}; -use actix_web::web::{self, route}; +use actix_web::web::{self, route, service}; pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( @@ -18,6 +18,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { web::resource("/nodes") .get(node::get_nodes) .post(node::create_node), - ), + ) + .service(web::resource("/groups").post(node::create_group)), ); } diff --git a/crates/migration/src/m20241008_095058_create_table_node.rs b/crates/migration/src/m20241008_095058_create_table_node.rs index b767897..4c3e056 100644 --- a/crates/migration/src/m20241008_095058_create_table_node.rs +++ b/crates/migration/src/m20241008_095058_create_table_node.rs @@ -32,12 +32,12 @@ impl MigrationTrait for Migration { .primary_key(), ) .col(string(Node::Name)) - .col(boolean(Node::Status)) + .col(boolean(Node::Status).default(false)) .col(double(Node::CoordLa)) .col(double(Node::CoordLo)) - .col(float(Node::Temperature)) - .col(double(Node::Battery)) - .col(big_unsigned(Node::Uptime)) + .col(float(Node::Temperature).default(-127)) + .col(double(Node::Battery).default(-127)) + .col(big_unsigned(Node::Uptime).default(0)) .col(uuid(Node::Group)) .foreign_key( ForeignKey::create()