creating a node endpoint
This commit is contained in:
parent
c7f3b71fc3
commit
f347f0ded4
11
ApfelBruno/Node/Create node group.bru
Normal file
11
ApfelBruno/Node/Create node group.bru
Normal file
|
@ -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
|
||||
}
|
11
ApfelBruno/Node/Create node.bru
Normal file
11
ApfelBruno/Node/Create node.bru
Normal file
|
@ -0,0 +1,11 @@
|
|||
meta {
|
||||
name: Create node
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
post {
|
||||
url: http://localhost:8080/api/v1/nodes
|
||||
body: json
|
||||
auth: none
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
meta {
|
||||
name: get all nodes
|
||||
type: http
|
||||
seq: 3
|
||||
seq: 1
|
||||
}
|
||||
|
||||
get {
|
|
@ -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
|
||||
}
|
|
@ -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<AppState>) -> actix_web::Result<impl Res
|
|||
|
||||
Ok(web::Json(result))
|
||||
}
|
||||
|
||||
pub async fn create_node(
|
||||
state: web::Data<AppState>,
|
||||
node: web::Json<entity::node::Model>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
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))
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue