create node endpoint

This commit is contained in:
Mika Bomm 2024-10-08 17:37:42 +02:00
parent f347f0ded4
commit 5f782cb778
5 changed files with 78 additions and 12 deletions

View file

@ -5,7 +5,11 @@ meta {
} }
post { post {
url: http://localhost:8080/api/v1/nodes url: http://localhost:8080/api/v1/groups
body: none body: json
auth: none auth: none
} }
body:json {
{"name":"testgroup"}
}

View file

@ -9,3 +9,12 @@ post {
body: json body: json
auth: none auth: none
} }
body:json {
{
"name":"some mac address",
"coord_la":1.123123,
"coord_lo":5.3123123,
"group":"efbd70a9-dc89-4c8d-9e6c-e7607c823df3"
}
}

View file

@ -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 entity::node_group;
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel}; use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, IntoActiveModel};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::AppState; 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)] #[derive(Serialize)]
struct GroupWithNode { struct GroupWithNode {
#[serde(flatten)] #[serde(flatten)]
@ -27,16 +40,55 @@ 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_group(
state: web::Data<AppState>,
group: web::Json<CreateGroupWithoutId>,
) -> actix_web::Result<impl Responder> {
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( pub async fn create_node(
state: web::Data<AppState>, state: web::Data<AppState>,
node: web::Json<entity::node::Model>, node: web::Json<CreateLicense>,
) -> actix_web::Result<impl Responder> { ) -> actix_web::Result<impl Responder> {
let db = &state.db; let db = &state.db;
let node = node.into_inner(); let node = node.into_inner();
let mut node = node.into_active_model(); println!("Checking group ID: {:?}", node.group);
node.id = ActiveValue::NotSet;
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)?; let result = node.insert(db).await.map_err(ErrorInternalServerError)?;

View file

@ -1,5 +1,5 @@
use crate::controller::{node, user}; 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) { pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service( cfg.service(
@ -18,6 +18,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
web::resource("/nodes") web::resource("/nodes")
.get(node::get_nodes) .get(node::get_nodes)
.post(node::create_node), .post(node::create_node),
), )
.service(web::resource("/groups").post(node::create_group)),
); );
} }

View file

@ -32,12 +32,12 @@ impl MigrationTrait for Migration {
.primary_key(), .primary_key(),
) )
.col(string(Node::Name)) .col(string(Node::Name))
.col(boolean(Node::Status)) .col(boolean(Node::Status).default(false))
.col(double(Node::CoordLa)) .col(double(Node::CoordLa))
.col(double(Node::CoordLo)) .col(double(Node::CoordLo))
.col(float(Node::Temperature)) .col(float(Node::Temperature).default(-127))
.col(double(Node::Battery)) .col(double(Node::Battery).default(-127))
.col(big_unsigned(Node::Uptime)) .col(big_unsigned(Node::Uptime).default(0))
.col(uuid(Node::Group)) .col(uuid(Node::Group))
.foreign_key( .foreign_key(
ForeignKey::create() ForeignKey::create()