diff --git a/ApfelBruno/Node/Create node group.bru b/ApfelBruno/Node/Create node group.bru new file mode 100644 index 0000000..e33232f --- /dev/null +++ b/ApfelBruno/Node/Create node group.bru @@ -0,0 +1,11 @@ +meta { + name: Create node group + type: http + seq: 3 +} + +post { + url: http://localhost:8080/api/v1/nodes + body: none + auth: none +} diff --git a/ApfelBruno/Node/Create node.bru b/ApfelBruno/Node/Create node.bru new file mode 100644 index 0000000..15ae404 --- /dev/null +++ b/ApfelBruno/Node/Create node.bru @@ -0,0 +1,11 @@ +meta { + name: Create node + type: http + seq: 2 +} + +post { + url: http://localhost:8080/api/v1/nodes + body: json + auth: none +} diff --git a/ApfelBruno/get all nodes.bru b/ApfelBruno/Node/get all nodes.bru similarity index 93% rename from ApfelBruno/get all nodes.bru rename to ApfelBruno/Node/get all nodes.bru index 4419086..c3a66bf 100644 --- a/ApfelBruno/get all nodes.bru +++ b/ApfelBruno/Node/get all nodes.bru @@ -1,7 +1,7 @@ meta { name: get all nodes type: http - seq: 3 + seq: 1 } get { diff --git a/ApfelBruno/get all users.bru b/ApfelBruno/User/get all users.bru similarity index 60% rename from ApfelBruno/get all users.bru rename to ApfelBruno/User/get all users.bru index d0269d2..72a5629 100644 --- a/ApfelBruno/get all users.bru +++ b/ApfelBruno/User/get all users.bru @@ -1,11 +1,11 @@ meta { name: get all users type: http - seq: 2 + seq: 1 } get { - url: http://localhost:8080/api/v1/ + url: http://localhost:8080/api/v1/users body: none auth: none } diff --git a/crates/backend/src/controller/node.rs b/crates/backend/src/controller/node.rs index ca36bb0..9df275d 100644 --- a/crates/backend/src/controller/node.rs +++ b/crates/backend/src/controller/node.rs @@ -1,6 +1,6 @@ use actix_web::{error::ErrorInternalServerError, web, Responder}; use entity::node_group; -use sea_orm::EntityTrait; +use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel}; use serde::{Deserialize, Serialize}; use crate::AppState; @@ -26,3 +26,19 @@ pub async fn get_nodes(state: web::Data) -> actix_web::Result, + 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; + + let result = node.insert(db).await.map_err(ErrorInternalServerError)?; + + Ok(web::Json(result)) +} diff --git a/crates/backend/src/routes.rs b/crates/backend/src/routes.rs index 9fbf0d0..d4c7371 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; +use actix_web::web::{self, route}; pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( @@ -14,6 +14,10 @@ pub fn config(cfg: &mut web::ServiceConfig) { .delete(user::delete_user) .put(user::update_user), ) - .service(web::resource("/nodes").get(node::get_nodes)), + .service( + web::resource("/nodes") + .get(node::get_nodes) + .post(node::create_node), + ), ); } diff --git a/crates/entity/src/node.rs b/crates/entity/src/node.rs index 6eb416b..2171f7c 100644 --- a/crates/entity/src/node.rs +++ b/crates/entity/src/node.rs @@ -19,11 +19,18 @@ pub struct Model { #[sea_orm(column_type = "Double")] pub battery: f64, pub uptime: i64, + pub group: Uuid, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { - #[sea_orm(has_many = "super::node_group::Entity")] + #[sea_orm( + belongs_to = "super::node_group::Entity", + from = "Column::Group", + to = "super::node_group::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] NodeGroup, } diff --git a/crates/entity/src/node_group.rs b/crates/entity/src/node_group.rs index ff119f1..e20b6a5 100644 --- a/crates/entity/src/node_group.rs +++ b/crates/entity/src/node_group.rs @@ -9,18 +9,11 @@ pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: Uuid, pub name: String, - pub node: Uuid, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { - #[sea_orm( - belongs_to = "super::node::Entity", - from = "Column::Node", - to = "super::node::Column::Id", - on_update = "Cascade", - on_delete = "Cascade" - )] + #[sea_orm(has_many = "super::node::Entity")] Node, } diff --git a/crates/migration/src/m20241008_095058_create_table_node.rs b/crates/migration/src/m20241008_095058_create_table_node.rs index 4d45dad..b767897 100644 --- a/crates/migration/src/m20241008_095058_create_table_node.rs +++ b/crates/migration/src/m20241008_095058_create_table_node.rs @@ -6,6 +6,21 @@ pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(NodeGroup::Table) + .if_not_exists() + .col( + uuid(NodeGroup::Id) + .extra("DEFAULT gen_random_uuid()") + .primary_key(), + ) + .col(string(NodeGroup::Name)) + .to_owned(), + ) + .await?; + manager .create_table( Table::create() @@ -23,40 +38,32 @@ impl MigrationTrait for Migration { .col(float(Node::Temperature)) .col(double(Node::Battery)) .col(big_unsigned(Node::Uptime)) - .to_owned(), - ) - .await?; - - manager - .create_table( - Table::create() - .table(NodeGroup::Table) - .if_not_exists() - .col( - uuid(NodeGroup::Id) - .extra("DEFAULT gen_random_uuid()") - .primary_key(), - ) - .col(string(NodeGroup::Name)) - .col(uuid(NodeGroup::Node)) + .col(uuid(Node::Group)) .foreign_key( ForeignKey::create() - .name("fk-node_id") - .from(NodeGroup::Table, NodeGroup::Node) - .to(Node::Table, Node::Id) + .name("fk-node-group_id") + .from(Node::Table, Node::Group) + .to(NodeGroup::Table, NodeGroup::Id) .on_update(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade), ) .to_owned(), ) .await?; + Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table(Table::drop().table(Node::Table).to_owned()) - .await + .await?; + + manager + .drop_table(Table::drop().table(NodeGroup::Table).to_owned()) + .await?; + + Ok(()) } } @@ -71,6 +78,7 @@ enum Node { Temperature, Battery, //Measured in volts Uptime, + Group, } #[derive(DeriveIden)] @@ -78,5 +86,4 @@ enum NodeGroup { Table, Id, Name, - Node, }