From 3428a637ce420baef9aa9f9803e71bd587867005 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 10 Apr 2024 00:16:55 +0200 Subject: Closes #24. Changed postgres to json directory storage --- src/routes/device.rs | 118 ++++++++++++--------------------------------------- 1 file changed, 28 insertions(+), 90 deletions(-) (limited to 'src/routes/device.rs') diff --git a/src/routes/device.rs b/src/routes/device.rs index 40b5cd8..b6bd9d0 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -1,47 +1,15 @@ -use crate::db::Device; use crate::error::Error; -use axum::extract::{Path, State}; +use crate::storage::Device; +use axum::extract::Path; use axum::Json; +use ipnetwork::IpNetwork; use mac_address::MacAddress; use serde::Deserialize; use serde_json::{json, Value}; -use sqlx::types::ipnetwork::IpNetwork; -use std::{str::FromStr, sync::Arc}; +use std::str::FromStr; use tracing::{debug, info}; use utoipa::ToSchema; -#[utoipa::path( - get, - path = "/device", - request_body = GetDevicePayload, - responses( - (status = 200, description = "Get `Device` information", body = [Device]) - ), - security(("api_key" = [])) -)] -#[deprecated] -pub async fn get_payload( - State(state): State>, - Json(payload): Json, -) -> Result, Error> { - info!("get device {}", payload.id); - let device = sqlx::query_as!( - Device, - r#" - SELECT id, mac, broadcast_addr, ip, times - FROM devices - WHERE id = $1; - "#, - payload.id - ) - .fetch_one(&state.db) - .await?; - - debug!("got device {:?}", device); - - Ok(Json(json!(device))) -} - #[utoipa::path( get, path = "/device/{id}", @@ -53,22 +21,10 @@ pub async fn get_payload( ), security((), ("api_key" = [])) )] -pub async fn get( - State(state): State>, - Path(path): Path, -) -> Result, Error> { - info!("get device from path {}", path); - let device = sqlx::query_as!( - Device, - r#" - SELECT id, mac, broadcast_addr, ip, times - FROM devices - WHERE id = $1; - "#, - path - ) - .fetch_one(&state.db) - .await?; +pub async fn get(Path(id): Path) -> Result, Error> { + info!("get device from path {}", id); + + let device = Device::read(&id)?; debug!("got device {:?}", device); @@ -76,13 +32,7 @@ pub async fn get( } #[derive(Deserialize, ToSchema)] -#[deprecated] -pub struct GetDevicePayload { - id: String, -} - -#[derive(Deserialize, ToSchema)] -pub struct DevicePayload { +pub struct Payload { id: String, mac: String, broadcast_addr: String, @@ -92,15 +42,14 @@ pub struct DevicePayload { #[utoipa::path( put, path = "/device", - request_body = DevicePayload, + request_body = Payload, responses( (status = 200, description = "add device to storage", body = [DeviceSchema]) ), security((), ("api_key" = [])) )] pub async fn put( - State(state): State>, - Json(payload): Json, + Json(payload): Json, ) -> Result, Error> { info!( "add device {} ({}, {}, {})", @@ -109,20 +58,14 @@ pub async fn put( let ip = IpNetwork::from_str(&payload.ip)?; let mac = MacAddress::from_str(&payload.mac)?; - let device = sqlx::query_as!( - Device, - r#" - INSERT INTO devices (id, mac, broadcast_addr, ip) - VALUES ($1, $2, $3, $4) - RETURNING id, mac, broadcast_addr, ip, times; - "#, - payload.id, + let device = Device { + id: payload.id, mac, - payload.broadcast_addr, - ip - ) - .fetch_one(&state.db) - .await?; + broadcast_addr: payload.broadcast_addr, + ip, + times: None, + }; + device.write()?; Ok(Json(json!(device))) } @@ -130,15 +73,14 @@ pub async fn put( #[utoipa::path( post, path = "/device", - request_body = DevicePayload, + request_body = Payload, responses( (status = 200, description = "update device in storage", body = [DeviceSchema]) ), security((), ("api_key" = [])) )] pub async fn post( - State(state): State>, - Json(payload): Json, + Json(payload): Json, ) -> Result, Error> { info!( "edit device {} ({}, {}, {})", @@ -146,20 +88,16 @@ pub async fn post( ); let ip = IpNetwork::from_str(&payload.ip)?; let mac = MacAddress::from_str(&payload.mac)?; - let device = sqlx::query_as!( - Device, - r#" - UPDATE devices - SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 - RETURNING id, mac, broadcast_addr, ip, times; - "#, + let times = Device::read(&payload.id)?.times; + + let device = Device { + id: payload.id, mac, - payload.broadcast_addr, + broadcast_addr: payload.broadcast_addr, ip, - payload.id - ) - .fetch_one(&state.db) - .await?; + times, + }; + device.write()?; Ok(Json(json!(device))) } -- cgit v1.2.3 From 07740f3d985b4cc921b69cd45ec9191a2daecd67 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 10 Apr 2024 13:29:01 +0200 Subject: Closes #31. Renamed Payloads --- src/main.rs | 4 ++-- src/routes/device.rs | 10 +++++----- src/routes/start.rs | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/routes/device.rs') diff --git a/src/main.rs b/src/main.rs index 779385f..8af8c63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,9 @@ mod wol; ), components( schemas( - start::Payload, + start::SPayload, start::Response, - device::Payload, + device::DPayload, storage::DeviceSchema, ) ), diff --git a/src/routes/device.rs b/src/routes/device.rs index b6bd9d0..49361f2 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -32,7 +32,7 @@ pub async fn get(Path(id): Path) -> Result, Error> { } #[derive(Deserialize, ToSchema)] -pub struct Payload { +pub struct DPayload { id: String, mac: String, broadcast_addr: String, @@ -42,14 +42,14 @@ pub struct Payload { #[utoipa::path( put, path = "/device", - request_body = Payload, + request_body = DPayload, responses( (status = 200, description = "add device to storage", body = [DeviceSchema]) ), security((), ("api_key" = [])) )] pub async fn put( - Json(payload): Json, + Json(payload): Json, ) -> Result, Error> { info!( "add device {} ({}, {}, {})", @@ -73,14 +73,14 @@ pub async fn put( #[utoipa::path( post, path = "/device", - request_body = Payload, + request_body = DPayload, responses( (status = 200, description = "update device in storage", body = [DeviceSchema]) ), security((), ("api_key" = [])) )] pub async fn post( - Json(payload): Json, + Json(payload): Json, ) -> Result, Error> { info!( "edit device {} ({}, {}, {})", diff --git a/src/routes/start.rs b/src/routes/start.rs index 6907193..ae2b384 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs @@ -14,7 +14,7 @@ use uuid::Uuid; #[utoipa::path( post, path = "/start/{id}", - request_body = Option, + request_body = Option, responses( (status = 200, description = "start the device with the given id", body = [Response]) ), @@ -26,7 +26,7 @@ use uuid::Uuid; pub async fn post( State(state): State>, Path(id): Path, - payload: Option>, + payload: Option>, ) -> Result, Error> { send_wol(state, &id, payload) } @@ -52,7 +52,7 @@ pub async fn get( fn send_wol( state: Arc, id: &str, - payload: Option>, + payload: Option>, ) -> Result, Error> { info!("start request for {id}"); let device = Device::read(id)?; @@ -134,7 +134,7 @@ fn get_eta(times: Option>) -> i64 { } #[derive(Deserialize, ToSchema)] -pub struct Payload { +pub struct SPayload { ping: Option, } -- cgit v1.2.3