diff options
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r-- | src/routes/device.rs | 113 |
1 files changed, 45 insertions, 68 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs index 2f0093d..d39d98e 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -1,8 +1,6 @@ | |||
1 | use crate::auth::auth; | ||
2 | use crate::db::Device; | 1 | use crate::db::Device; |
3 | use crate::error::Error; | 2 | use crate::error::Error; |
4 | use axum::extract::State; | 3 | use axum::extract::State; |
5 | use axum::http::HeaderMap; | ||
6 | use axum::Json; | 4 | use axum::Json; |
7 | use mac_address::MacAddress; | 5 | use mac_address::MacAddress; |
8 | use serde::{Deserialize, Serialize}; | 6 | use serde::{Deserialize, Serialize}; |
@@ -13,31 +11,24 @@ use tracing::{debug, info}; | |||
13 | 11 | ||
14 | pub async fn get( | 12 | pub async fn get( |
15 | State(state): State<Arc<crate::AppState>>, | 13 | State(state): State<Arc<crate::AppState>>, |
16 | headers: HeaderMap, | ||
17 | Json(payload): Json<GetDevicePayload>, | 14 | Json(payload): Json<GetDevicePayload>, |
18 | ) -> Result<Json<Value>, Error> { | 15 | ) -> Result<Json<Value>, Error> { |
19 | info!("get device {}", payload.id); | 16 | info!("get device {}", payload.id); |
20 | let secret = headers.get("authorization"); | 17 | let device = sqlx::query_as!( |
21 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); | 18 | Device, |
22 | if authorized { | 19 | r#" |
23 | let device = sqlx::query_as!( | 20 | SELECT id, mac, broadcast_addr, ip, times |
24 | Device, | 21 | FROM devices |
25 | r#" | 22 | WHERE id = $1; |
26 | SELECT id, mac, broadcast_addr, ip, times | 23 | "#, |
27 | FROM devices | 24 | payload.id |
28 | WHERE id = $1; | 25 | ) |
29 | "#, | 26 | .fetch_one(&state.db) |
30 | payload.id | 27 | .await?; |
31 | ) | ||
32 | .fetch_one(&state.db) | ||
33 | .await?; | ||
34 | 28 | ||
35 | debug!("got device {:?}", device); | 29 | debug!("got device {:?}", device); |
36 | 30 | ||
37 | Ok(Json(json!(device))) | 31 | Ok(Json(json!(device))) |
38 | } else { | ||
39 | Err(Error::Generic) | ||
40 | } | ||
41 | } | 32 | } |
42 | 33 | ||
43 | #[derive(Deserialize)] | 34 | #[derive(Deserialize)] |
@@ -47,7 +38,6 @@ pub struct GetDevicePayload { | |||
47 | 38 | ||
48 | pub async fn put( | 39 | pub async fn put( |
49 | State(state): State<Arc<crate::AppState>>, | 40 | State(state): State<Arc<crate::AppState>>, |
50 | headers: HeaderMap, | ||
51 | Json(payload): Json<PutDevicePayload>, | 41 | Json(payload): Json<PutDevicePayload>, |
52 | ) -> Result<Json<Value>, Error> { | 42 | ) -> Result<Json<Value>, Error> { |
53 | info!( | 43 | info!( |
@@ -55,28 +45,22 @@ pub async fn put( | |||
55 | payload.id, payload.mac, payload.broadcast_addr, payload.ip | 45 | payload.id, payload.mac, payload.broadcast_addr, payload.ip |
56 | ); | 46 | ); |
57 | 47 | ||
58 | let secret = headers.get("authorization"); | 48 | let ip = IpNetwork::from_str(&payload.ip)?; |
59 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); | 49 | let mac = MacAddress::from_str(&payload.mac)?; |
60 | if authorized { | 50 | sqlx::query!( |
61 | let ip = IpNetwork::from_str(&payload.ip)?; | 51 | r#" |
62 | let mac = MacAddress::from_str(&payload.mac)?; | 52 | INSERT INTO devices (id, mac, broadcast_addr, ip) |
63 | sqlx::query!( | 53 | VALUES ($1, $2, $3, $4); |
64 | r#" | 54 | "#, |
65 | INSERT INTO devices (id, mac, broadcast_addr, ip) | 55 | payload.id, |
66 | VALUES ($1, $2, $3, $4); | 56 | mac, |
67 | "#, | 57 | payload.broadcast_addr, |
68 | payload.id, | 58 | ip |
69 | mac, | 59 | ) |
70 | payload.broadcast_addr, | 60 | .execute(&state.db) |
71 | ip | 61 | .await?; |
72 | ) | ||
73 | .execute(&state.db) | ||
74 | .await?; | ||
75 | 62 | ||
76 | Ok(Json(json!(PutDeviceResponse { success: true }))) | 63 | Ok(Json(json!(PutDeviceResponse { success: true }))) |
77 | } else { | ||
78 | Err(Error::Generic) | ||
79 | } | ||
80 | } | 64 | } |
81 | 65 | ||
82 | #[derive(Deserialize)] | 66 | #[derive(Deserialize)] |
@@ -94,37 +78,30 @@ pub struct PutDeviceResponse { | |||
94 | 78 | ||
95 | pub async fn post( | 79 | pub async fn post( |
96 | State(state): State<Arc<crate::AppState>>, | 80 | State(state): State<Arc<crate::AppState>>, |
97 | headers: HeaderMap, | ||
98 | Json(payload): Json<PostDevicePayload>, | 81 | Json(payload): Json<PostDevicePayload>, |
99 | ) -> Result<Json<Value>, Error> { | 82 | ) -> Result<Json<Value>, Error> { |
100 | info!( | 83 | info!( |
101 | "edit device {} ({}, {}, {})", | 84 | "edit device {} ({}, {}, {})", |
102 | payload.id, payload.mac, payload.broadcast_addr, payload.ip | 85 | payload.id, payload.mac, payload.broadcast_addr, payload.ip |
103 | ); | 86 | ); |
104 | let secret = headers.get("authorization"); | 87 | let ip = IpNetwork::from_str(&payload.ip)?; |
105 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); | 88 | let mac = MacAddress::from_str(&payload.mac)?; |
106 | if authorized { | 89 | let device = sqlx::query_as!( |
107 | let ip = IpNetwork::from_str(&payload.ip)?; | 90 | Device, |
108 | let mac = MacAddress::from_str(&payload.mac)?; | 91 | r#" |
109 | let device = sqlx::query_as!( | 92 | UPDATE devices |
110 | Device, | 93 | SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 |
111 | r#" | 94 | RETURNING id, mac, broadcast_addr, ip, times; |
112 | UPDATE devices | 95 | "#, |
113 | SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4 | 96 | mac, |
114 | RETURNING id, mac, broadcast_addr, ip, times; | 97 | payload.broadcast_addr, |
115 | "#, | 98 | ip, |
116 | mac, | 99 | payload.id |
117 | payload.broadcast_addr, | 100 | ) |
118 | ip, | 101 | .fetch_one(&state.db) |
119 | payload.id | 102 | .await?; |
120 | ) | ||
121 | .fetch_one(&state.db) | ||
122 | .await?; | ||
123 | 103 | ||
124 | Ok(Json(json!(device))) | 104 | Ok(Json(json!(device))) |
125 | } else { | ||
126 | Err(Error::Generic) | ||
127 | } | ||
128 | } | 105 | } |
129 | 106 | ||
130 | #[derive(Deserialize)] | 107 | #[derive(Deserialize)] |