aboutsummaryrefslogtreecommitdiff
path: root/src/routes/device.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-03-05 13:16:18 +0100
committerGitHub <[email protected]>2024-03-05 13:16:18 +0100
commit8dca7e83519b6c3531653cdedf60b2a14e1035b7 (patch)
tree0180a452dd354b067470618e6dbdc330649287cb /src/routes/device.rs
parentf0dc13f907a72ffef44f89b5e197567db129b020 (diff)
parent85c63c2ca8448428e2db93cb9d4f284a4e314ed7 (diff)
downloadwebol-8dca7e83519b6c3531653cdedf60b2a14e1035b7.tar
webol-8dca7e83519b6c3531653cdedf60b2a14e1035b7.tar.gz
webol-8dca7e83519b6c3531653cdedf60b2a14e1035b7.zip
Merge pull request #22 from FxQnLr/0.3.3
0.3.3
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r--src/routes/device.rs91
1 files changed, 75 insertions, 16 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
index d39d98e..d01d9f0 100644
--- a/src/routes/device.rs
+++ b/src/routes/device.rs
@@ -1,14 +1,25 @@
1use crate::db::Device; 1use crate::db::Device;
2use crate::error::Error; 2use crate::error::Error;
3use axum::extract::State; 3use axum::extract::{Path, State};
4use axum::Json; 4use axum::Json;
5use mac_address::MacAddress; 5use mac_address::MacAddress;
6use serde::{Deserialize, Serialize}; 6use serde::Deserialize;
7use serde_json::{json, Value}; 7use serde_json::{json, Value};
8use sqlx::types::ipnetwork::IpNetwork; 8use sqlx::types::ipnetwork::IpNetwork;
9use std::{sync::Arc, str::FromStr}; 9use std::{str::FromStr, sync::Arc};
10use tracing::{debug, info}; 10use tracing::{debug, info};
11use utoipa::ToSchema;
11 12
13#[utoipa::path(
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]
12pub async fn get( 23pub async fn get(
13 State(state): State<Arc<crate::AppState>>, 24 State(state): State<Arc<crate::AppState>>,
14 Json(payload): Json<GetDevicePayload>, 25 Json(payload): Json<GetDevicePayload>,
@@ -31,11 +42,53 @@ pub async fn get(
31 Ok(Json(json!(device))) 42 Ok(Json(json!(device)))
32} 43}
33 44
34#[derive(Deserialize)] 45#[utoipa::path(
46 get,
47 path = "/device/{id}",
48 responses(
49 (status = 200, description = "Get `Device` information", body = [Device])
50 ),
51 params(
52 ("id" = String, Path, description = "Device id")
53 ),
54 security(("api_key" = []))
55)]
56pub async fn get_path(
57 State(state): State<Arc<crate::AppState>>,
58 Path(path): Path<String>,
59) -> Result<Json<Value>, Error> {
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
73 debug!("got device {:?}", device);
74
75 Ok(Json(json!(device)))
76}
77
78#[derive(Deserialize, ToSchema)]
35pub struct GetDevicePayload { 79pub struct GetDevicePayload {
36 id: String, 80 id: String,
37} 81}
38 82
83#[utoipa::path(
84 put,
85 path = "/device",
86 request_body = PutDevicePayload,
87 responses(
88 (status = 200, description = "List matching todos by query", body = [DeviceSchema])
89 ),
90 security(("api_key" = []))
91)]
39pub async fn put( 92pub async fn put(
40 State(state): State<Arc<crate::AppState>>, 93 State(state): State<Arc<crate::AppState>>,
41 Json(payload): Json<PutDevicePayload>, 94 Json(payload): Json<PutDevicePayload>,
@@ -44,26 +97,28 @@ pub async fn put(
44 "add device {} ({}, {}, {})", 97 "add device {} ({}, {}, {})",
45 payload.id, payload.mac, payload.broadcast_addr, payload.ip 98 payload.id, payload.mac, payload.broadcast_addr, payload.ip
46 ); 99 );
47 100
48 let ip = IpNetwork::from_str(&payload.ip)?; 101 let ip = IpNetwork::from_str(&payload.ip)?;
49 let mac = MacAddress::from_str(&payload.mac)?; 102 let mac = MacAddress::from_str(&payload.mac)?;
50 sqlx::query!( 103 let device = sqlx::query_as!(
104 Device,
51 r#" 105 r#"
52 INSERT INTO devices (id, mac, broadcast_addr, ip) 106 INSERT INTO devices (id, mac, broadcast_addr, ip)
53 VALUES ($1, $2, $3, $4); 107 VALUES ($1, $2, $3, $4)
108 RETURNING id, mac, broadcast_addr, ip, times;
54 "#, 109 "#,
55 payload.id, 110 payload.id,
56 mac, 111 mac,
57 payload.broadcast_addr, 112 payload.broadcast_addr,
58 ip 113 ip
59 ) 114 )
60 .execute(&state.db) 115 .fetch_one(&state.db)
61 .await?; 116 .await?;
62 117
63 Ok(Json(json!(PutDeviceResponse { success: true }))) 118 Ok(Json(json!(device)))
64} 119}
65 120
66#[derive(Deserialize)] 121#[derive(Deserialize, ToSchema)]
67pub struct PutDevicePayload { 122pub struct PutDevicePayload {
68 id: String, 123 id: String,
69 mac: String, 124 mac: String,
@@ -71,11 +126,15 @@ pub struct PutDevicePayload {
71 ip: String, 126 ip: String,
72} 127}
73 128
74#[derive(Serialize)] 129#[utoipa::path(
75pub struct PutDeviceResponse { 130 post,
76 success: bool, 131 path = "/device",
77} 132 request_body = PostDevicePayload,
78 133 responses(
134 (status = 200, description = "List matching todos by query", body = [DeviceSchema])
135 ),
136 security(("api_key" = []))
137)]
79pub async fn post( 138pub async fn post(
80 State(state): State<Arc<crate::AppState>>, 139 State(state): State<Arc<crate::AppState>>,
81 Json(payload): Json<PostDevicePayload>, 140 Json(payload): Json<PostDevicePayload>,
@@ -104,7 +163,7 @@ pub async fn post(
104 Ok(Json(json!(device))) 163 Ok(Json(json!(device)))
105} 164}
106 165
107#[derive(Deserialize)] 166#[derive(Deserialize, ToSchema)]
108pub struct PostDevicePayload { 167pub struct PostDevicePayload {
109 id: String, 168 id: String,
110 mac: String, 169 mac: String,