diff options
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r-- | src/routes/device.rs | 118 |
1 files changed, 28 insertions, 90 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs index 40b5cd8..49361f2 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -1,49 +1,17 @@ | |||
1 | use crate::db::Device; | ||
2 | use crate::error::Error; | 1 | use crate::error::Error; |
3 | use axum::extract::{Path, State}; | 2 | use crate::storage::Device; |
3 | use axum::extract::Path; | ||
4 | use axum::Json; | 4 | use axum::Json; |
5 | use ipnetwork::IpNetwork; | ||
5 | use mac_address::MacAddress; | 6 | use mac_address::MacAddress; |
6 | use serde::Deserialize; | 7 | use serde::Deserialize; |
7 | use serde_json::{json, Value}; | 8 | use serde_json::{json, Value}; |
8 | use sqlx::types::ipnetwork::IpNetwork; | 9 | use std::str::FromStr; |
9 | use std::{str::FromStr, sync::Arc}; | ||
10 | use tracing::{debug, info}; | 10 | use tracing::{debug, info}; |
11 | use utoipa::ToSchema; | 11 | use utoipa::ToSchema; |
12 | 12 | ||
13 | #[utoipa::path( | 13 | #[utoipa::path( |
14 | get, | 14 | get, |
15 | path = "/device", | ||
16 | request_body = GetDevicePayload, | ||
17 | responses( | ||
18 | (status = 200, description = "Get `Device` information", body = [Device]) | ||
19 | ), | ||
20 | security(("api_key" = [])) | ||
21 | )] | ||
22 | #[deprecated] | ||
23 | pub async fn get_payload( | ||
24 | State(state): State<Arc<crate::AppState>>, | ||
25 | Json(payload): Json<GetDevicePayload>, | ||
26 | ) -> Result<Json<Value>, Error> { | ||
27 | info!("get device {}", payload.id); | ||
28 | let device = sqlx::query_as!( | ||
29 | Device, | ||
30 | r#" | ||
31 | SELECT id, mac, broadcast_addr, ip, times | ||
32 | FROM devices | ||
33 | WHERE id = $1; | ||
34 | "#, | ||
35 | payload.id | ||
36 | ) | ||
37 | .fetch_one(&state.db) | ||
38 | .await?; | ||
39 | |||
40 | debug!("got device {:?}", device); | ||
41 | |||
42 | Ok(Json(json!(device))) | ||
43 | } | ||
44 | |||
45 | #[utoipa::path( | ||
46 | get, | ||
47 | path = "/device/{id}", | 15 | path = "/device/{id}", |
48 | responses( | 16 | responses( |
49 | (status = 200, description = "Get `Device` information", body = [Device]) | 17 | (status = 200, description = "Get `Device` information", body = [Device]) |
@@ -53,22 +21,10 @@ pub async fn get_payload( | |||
53 | ), | 21 | ), |
54 | security((), ("api_key" = [])) | 22 | security((), ("api_key" = [])) |
55 | )] | 23 | )] |
56 | pub async fn get( | 24 | pub async fn get(Path(id): Path<String>) -> Result<Json<Value>, Error> { |
57 | State(state): State<Arc<crate::AppState>>, | 25 | info!("get device from path {}", id); |
58 | Path(path): Path<String>, | 26 | |
59 | ) -> Result<Json<Value>, Error> { | 27 | let device = Device::read(&id)?; |
60 | info!("get device from path {}", path); | ||
61 | let device = sqlx::query_as!( | ||
62 | Device, | ||
63 | r#" | ||
64 | SELECT id, mac, broadcast_addr, ip, times | ||
65 | FROM devices | ||
66 | WHERE id = $1; | ||
67 | "#, | ||
68 | path | ||
69 | ) | ||
70 | .fetch_one(&state.db) | ||
71 | .await?; | ||
72 | 28 | ||
73 | debug!("got device {:?}", device); | 29 | debug!("got device {:?}", device); |
74 | 30 | ||
@@ -76,13 +32,7 @@ pub async fn get( | |||
76 | } | 32 | } |
77 | 33 | ||
78 | #[derive(Deserialize, ToSchema)] | 34 | #[derive(Deserialize, ToSchema)] |
79 | #[deprecated] | 35 | pub struct DPayload { |
80 | pub struct GetDevicePayload { | ||
81 | id: String, | ||
82 | } | ||
83 | |||
84 | #[derive(Deserialize, ToSchema)] | ||
85 | pub struct DevicePayload { | ||
86 | id: String, | 36 | id: String, |
87 | mac: String, | 37 | mac: String, |
88 | broadcast_addr: String, | 38 | broadcast_addr: String, |
@@ -92,15 +42,14 @@ pub struct DevicePayload { | |||
92 | #[utoipa::path( | 42 | #[utoipa::path( |
93 | put, | 43 | put, |
94 | path = "/device", | 44 | path = "/device", |
95 | request_body = DevicePayload, | 45 | request_body = DPayload, |
96 | responses( | 46 | responses( |
97 | (status = 200, description = "add device to storage", body = [DeviceSchema]) | 47 | (status = 200, description = "add device to storage", body = [DeviceSchema]) |
98 | ), | 48 | ), |
99 | security((), ("api_key" = [])) | 49 | security((), ("api_key" = [])) |
100 | )] | 50 | )] |
101 | pub async fn put( | 51 | pub async fn put( |
102 | State(state): State<Arc<crate::AppState>>, | 52 | Json(payload): Json<DPayload>, |
103 | Json(payload): Json<DevicePayload>, | ||
104 | ) -> Result<Json<Value>, Error> { | 53 | ) -> Result<Json<Value>, Error> { |
105 | info!( | 54 | info!( |
106 | "add device {} ({}, {}, {})", | 55 | "add device {} ({}, {}, {})", |
@@ -109,20 +58,14 @@ pub async fn put( | |||
109 | 58 | ||
110 | let ip = IpNetwork::from_str(&payload.ip)?; | 59 | let ip = IpNetwork::from_str(&payload.ip)?; |
111 | let mac = MacAddress::from_str(&payload.mac)?; | 60 | let mac = MacAddress::from_str(&payload.mac)?; |
112 | let device = sqlx::query_as!( | 61 | let device = Device { |
113 | Device, | 62 | id: payload.id, |
114 | r#" | ||
115 | INSERT INTO devices (id, mac, broadcast_addr, ip) | ||
116 | VALUES ($1, $2, $3, $4) | ||
117 | RETURNING id, mac, broadcast_addr, ip, times; | ||
118 | "#, | ||
119 | payload.id, | ||
120 | mac, | 63 | mac, |
121 | payload.broadcast_addr, | 64 | broadcast_addr: payload.broadcast_addr, |
122 | ip | 65 | ip, |
123 | ) | 66 | times: None, |
124 | .fetch_one(&state.db) | 67 | }; |
125 | .await?; | 68 | device.write()?; |
126 | 69 | ||
127 | Ok(Json(json!(device))) | 70 | Ok(Json(json!(device))) |
128 | } | 71 | } |
@@ -130,15 +73,14 @@ pub async fn put( | |||
130 | #[utoipa::path( | 73 | #[utoipa::path( |
131 | post, | 74 | post, |
132 | path = "/device", | 75 | path = "/device", |
133 | request_body = DevicePayload, | 76 | request_body = DPayload, |
134 | responses( | 77 | responses( |
135 | (status = 200, description = "update device in storage", body = [DeviceSchema]) | 78 | (status = 200, description = "update device in storage", body = [DeviceSchema]) |
136 | ), | 79 | ), |
137 | security((), ("api_key" = [])) | 80 | security((), ("api_key" = [])) |
138 | )] | 81 | )] |
139 | pub async fn post( | 82 | pub async fn post( |
140 | State(state): State<Arc<crate::AppState>>, | 83 | Json(payload): Json<DPayload>, |
141 | Json(payload): Json<DevicePayload>, | ||
142 | ) -> Result<Json<Value>, Error> { | 84 | ) -> Result<Json<Value>, Error> { |
143 | info!( | 85 | info!( |
144 | "edit device {} ({}, {}, {})", | 86 | "edit device {} ({}, {}, {})", |
@@ -146,20 +88,16 @@ pub async fn post( | |||
146 | ); | 88 | ); |
147 | let ip = IpNetwork::from_str(&payload.ip)?; | 89 | let ip = IpNetwork::from_str(&payload.ip)?; |
148 | let mac = MacAddress::from_str(&payload.mac)?; | 90 | let mac = MacAddress::from_str(&payload.mac)?; |
149 | let device = sqlx::query_as!( | 91 | let times = Device::read(&payload.id)?.times; |
150 | Device, | 92 | |
151 | r#" | 93 | let device = Device { |
152 | UPDATE devices | 94 | id: payload.id, |
153 | SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 | ||
154 | RETURNING id, mac, broadcast_addr, ip, times; | ||
155 | "#, | ||
156 | mac, | 95 | mac, |
157 | payload.broadcast_addr, | 96 | broadcast_addr: payload.broadcast_addr, |
158 | ip, | 97 | ip, |
159 | payload.id | 98 | times, |
160 | ) | 99 | }; |
161 | .fetch_one(&state.db) | 100 | device.write()?; |
162 | .await?; | ||
163 | 101 | ||
164 | Ok(Json(json!(device))) | 102 | Ok(Json(json!(device))) |
165 | } | 103 | } |