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 {
|
meta {
|
||||||
name: get all nodes
|
name: get all nodes
|
||||||
type: http
|
type: http
|
||||||
seq: 3
|
seq: 1
|
||||||
}
|
}
|
||||||
|
|
||||||
get {
|
get {
|
|
@ -1,11 +1,11 @@
|
||||||
meta {
|
meta {
|
||||||
name: get all users
|
name: get all users
|
||||||
type: http
|
type: http
|
||||||
seq: 2
|
seq: 1
|
||||||
}
|
}
|
||||||
|
|
||||||
get {
|
get {
|
||||||
url: http://localhost:8080/api/v1/
|
url: http://localhost:8080/api/v1/users
|
||||||
body: none
|
body: none
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use actix_web::{error::ErrorInternalServerError, web, Responder};
|
use actix_web::{error::ErrorInternalServerError, web, Responder};
|
||||||
use entity::node_group;
|
use entity::node_group;
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::AppState;
|
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))
|
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 crate::controller::{node, user};
|
||||||
use actix_web::web;
|
use actix_web::web::{self, route};
|
||||||
|
|
||||||
pub fn config(cfg: &mut web::ServiceConfig) {
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(
|
cfg.service(
|
||||||
|
@ -14,6 +14,10 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
.delete(user::delete_user)
|
.delete(user::delete_user)
|
||||||
.put(user::update_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")]
|
#[sea_orm(column_type = "Double")]
|
||||||
pub battery: f64,
|
pub battery: f64,
|
||||||
pub uptime: i64,
|
pub uptime: i64,
|
||||||
|
pub group: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {
|
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,
|
NodeGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,18 +9,11 @@ pub struct Model {
|
||||||
#[sea_orm(primary_key, auto_increment = false)]
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub node: Uuid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
#[sea_orm(
|
#[sea_orm(has_many = "super::node::Entity")]
|
||||||
belongs_to = "super::node::Entity",
|
|
||||||
from = "Column::Node",
|
|
||||||
to = "super::node::Column::Id",
|
|
||||||
on_update = "Cascade",
|
|
||||||
on_delete = "Cascade"
|
|
||||||
)]
|
|
||||||
Node,
|
Node,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,21 @@ pub struct Migration;
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl MigrationTrait for Migration {
|
impl MigrationTrait for Migration {
|
||||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
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
|
manager
|
||||||
.create_table(
|
.create_table(
|
||||||
Table::create()
|
Table::create()
|
||||||
|
@ -23,40 +38,32 @@ impl MigrationTrait for Migration {
|
||||||
.col(float(Node::Temperature))
|
.col(float(Node::Temperature))
|
||||||
.col(double(Node::Battery))
|
.col(double(Node::Battery))
|
||||||
.col(big_unsigned(Node::Uptime))
|
.col(big_unsigned(Node::Uptime))
|
||||||
.to_owned(),
|
.col(uuid(Node::Group))
|
||||||
)
|
|
||||||
.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))
|
|
||||||
.foreign_key(
|
.foreign_key(
|
||||||
ForeignKey::create()
|
ForeignKey::create()
|
||||||
.name("fk-node_id")
|
.name("fk-node-group_id")
|
||||||
.from(NodeGroup::Table, NodeGroup::Node)
|
.from(Node::Table, Node::Group)
|
||||||
.to(Node::Table, Node::Id)
|
.to(NodeGroup::Table, NodeGroup::Id)
|
||||||
.on_update(ForeignKeyAction::Cascade)
|
.on_update(ForeignKeyAction::Cascade)
|
||||||
.on_delete(ForeignKeyAction::Cascade),
|
.on_delete(ForeignKeyAction::Cascade),
|
||||||
)
|
)
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
manager
|
manager
|
||||||
.drop_table(Table::drop().table(Node::Table).to_owned())
|
.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,
|
Temperature,
|
||||||
Battery, //Measured in volts
|
Battery, //Measured in volts
|
||||||
Uptime,
|
Uptime,
|
||||||
|
Group,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(DeriveIden)]
|
#[derive(DeriveIden)]
|
||||||
|
@ -78,5 +86,4 @@ enum NodeGroup {
|
||||||
Table,
|
Table,
|
||||||
Id,
|
Id,
|
||||||
Name,
|
Name,
|
||||||
Node,
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue