diff options
author | FxQnLr <[email protected]> | 2024-04-15 20:33:32 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-15 20:33:32 +0200 |
commit | d3cf93fb6c9b7e0faf9b7907328f0a042009e164 (patch) | |
tree | a55ec4f56e16de701c6764b559c776b38bc11637 /src/routes/device.rs | |
parent | b615f6e34e084d520dcc301058b5926074188500 (diff) | |
download | webol-d3cf93fb6c9b7e0faf9b7907328f0a042009e164.tar webol-d3cf93fb6c9b7e0faf9b7907328f0a042009e164.tar.gz webol-d3cf93fb6c9b7e0faf9b7907328f0a042009e164.zip |
Closes #35. Entry of Ip optional, error on ping request without saved ip
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r-- | src/routes/device.rs | 27 |
1 files changed, 16 insertions, 11 deletions
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 { | |||
36 | id: String, | 36 | id: String, |
37 | mac: String, | 37 | mac: String, |
38 | broadcast_addr: String, | 38 | broadcast_addr: String, |
39 | ip: String, | 39 | ip: Option<String>, |
40 | } | 40 | } |
41 | 41 | ||
42 | #[utoipa::path( | 42 | #[utoipa::path( |
@@ -48,15 +48,17 @@ pub struct DPayload { | |||
48 | ), | 48 | ), |
49 | security((), ("api_key" = [])) | 49 | security((), ("api_key" = [])) |
50 | )] | 50 | )] |
51 | pub async fn put( | 51 | pub async fn put(Json(payload): Json<DPayload>) -> Result<Json<Value>, Error> { |
52 | Json(payload): Json<DPayload>, | ||
53 | ) -> Result<Json<Value>, Error> { | ||
54 | info!( | 52 | info!( |
55 | "add device {} ({}, {}, {})", | 53 | "add device {} ({}, {}, {:?})", |
56 | payload.id, payload.mac, payload.broadcast_addr, payload.ip | 54 | payload.id, payload.mac, payload.broadcast_addr, payload.ip |
57 | ); | 55 | ); |
58 | 56 | ||
59 | let ip = IpNetwork::from_str(&payload.ip)?; | 57 | let ip = if let Some(ip_s) = payload.ip { |
58 | Some(IpNetwork::from_str(&ip_s)?) | ||
59 | } else { | ||
60 | None | ||
61 | }; | ||
60 | let mac = MacAddress::from_str(&payload.mac)?; | 62 | let mac = MacAddress::from_str(&payload.mac)?; |
61 | let device = Device { | 63 | let device = Device { |
62 | id: payload.id, | 64 | id: payload.id, |
@@ -79,14 +81,17 @@ pub async fn put( | |||
79 | ), | 81 | ), |
80 | security((), ("api_key" = [])) | 82 | security((), ("api_key" = [])) |
81 | )] | 83 | )] |
82 | pub async fn post( | 84 | pub async fn post(Json(payload): Json<DPayload>) -> Result<Json<Value>, Error> { |
83 | Json(payload): Json<DPayload>, | ||
84 | ) -> Result<Json<Value>, Error> { | ||
85 | info!( | 85 | info!( |
86 | "edit device {} ({}, {}, {})", | 86 | "edit device {} ({}, {}, {:?})", |
87 | payload.id, payload.mac, payload.broadcast_addr, payload.ip | 87 | payload.id, payload.mac, payload.broadcast_addr, payload.ip |
88 | ); | 88 | ); |
89 | let ip = IpNetwork::from_str(&payload.ip)?; | 89 | |
90 | let ip = if let Some(ip_s) = payload.ip { | ||
91 | Some(IpNetwork::from_str(&ip_s)?) | ||
92 | } else { | ||
93 | None | ||
94 | }; | ||
90 | let mac = MacAddress::from_str(&payload.mac)?; | 95 | let mac = MacAddress::from_str(&payload.mac)?; |
91 | let times = Device::read(&payload.id)?.times; | 96 | let times = Device::read(&payload.id)?.times; |
92 | 97 | ||