From d3cf93fb6c9b7e0faf9b7907328f0a042009e164 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 15 Apr 2024 20:33:32 +0200 Subject: Closes #35. Entry of Ip optional, error on ping request without saved ip --- src/routes/device.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/routes/device.rs') diff --git a/src/routes/device.rs b/src/routes/device.rs index 49361f2..f767eab 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs @@ -36,7 +36,7 @@ pub struct DPayload { id: String, mac: String, broadcast_addr: String, - ip: String, + ip: Option, } #[utoipa::path( @@ -48,15 +48,17 @@ pub struct DPayload { ), security((), ("api_key" = [])) )] -pub async fn put( - Json(payload): Json, -) -> Result, Error> { +pub async fn put(Json(payload): Json) -> Result, Error> { info!( - "add device {} ({}, {}, {})", + "add device {} ({}, {}, {:?})", payload.id, payload.mac, payload.broadcast_addr, payload.ip ); - let ip = IpNetwork::from_str(&payload.ip)?; + let ip = if let Some(ip_s) = payload.ip { + Some(IpNetwork::from_str(&ip_s)?) + } else { + None + }; let mac = MacAddress::from_str(&payload.mac)?; let device = Device { id: payload.id, @@ -79,14 +81,17 @@ pub async fn put( ), security((), ("api_key" = [])) )] -pub async fn post( - Json(payload): Json, -) -> Result, Error> { +pub async fn post(Json(payload): Json) -> Result, Error> { info!( - "edit device {} ({}, {}, {})", + "edit device {} ({}, {}, {:?})", payload.id, payload.mac, payload.broadcast_addr, payload.ip ); - let ip = IpNetwork::from_str(&payload.ip)?; + + let ip = if let Some(ip_s) = payload.ip { + Some(IpNetwork::from_str(&ip_s)?) + } else { + None + }; let mac = MacAddress::from_str(&payload.mac)?; let times = Device::read(&payload.id)?.times; -- cgit v1.2.3