summaryrefslogtreecommitdiff
path: root/src/routes/device.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2023-11-06 11:09:34 +0100
committerGitHub <[email protected]>2023-11-06 11:09:34 +0100
commit1cd2a8e4aecfaad2a8385a6bea61580209b86398 (patch)
treec357bcaca0681caf9a6742c857bb494dc4315900 /src/routes/device.rs
parentd9d7b125e4fcaa3aedd7b57a69e6880e012ccf33 (diff)
parent32561060a8dc6fc6118498da06bdd8f5b4c3f0fd (diff)
downloadwebol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar
webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.tar.gz
webol-1cd2a8e4aecfaad2a8385a6bea61580209b86398.zip
Merge pull request #6 from FxQnLr/ping
Ping
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r--src/routes/device.rs34
1 files changed, 20 insertions, 14 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
index 025c7d0..1eeff0b 100644
--- a/src/routes/device.rs
+++ b/src/routes/device.rs
@@ -4,24 +4,26 @@ use axum::headers::HeaderMap;
4use axum::Json; 4use axum::Json;
5use serde::{Deserialize, Serialize}; 5use serde::{Deserialize, Serialize};
6use serde_json::{json, Value}; 6use serde_json::{json, Value};
7use tracing::info; 7use tracing::{debug, info};
8use crate::auth::auth; 8use crate::auth::auth;
9use crate::db::Device; 9use crate::db::Device;
10use crate::error::WebolError; 10use crate::error::WebolError;
11 11
12pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, WebolError> { 12pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, WebolError> {
13 info!("GET request"); 13 info!("add device {}", payload.id);
14 let secret = headers.get("authorization"); 14 let secret = headers.get("authorization");
15 if auth(secret).map_err(WebolError::Auth)? { 15 if auth(secret).map_err(WebolError::Auth)? {
16 let device = sqlx::query_as!( 16 let device = sqlx::query_as!(
17 Device, 17 Device,
18 r#" 18 r#"
19 SELECT id, mac, broadcast_addr 19 SELECT id, mac, broadcast_addr, ip
20 FROM devices 20 FROM devices
21 WHERE id = $1; 21 WHERE id = $1;
22 "#, 22 "#,
23 payload.id 23 payload.id
24 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 24 ).fetch_one(&state.db).await.map_err(WebolError::DB)?;
25
26 debug!("got device {:?}", device);
25 27
26 Ok(Json(json!(device))) 28 Ok(Json(json!(device)))
27 } else { 29 } else {
@@ -35,18 +37,19 @@ pub struct GetDevicePayload {
35} 37}
36 38
37pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, WebolError> { 39pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, WebolError> {
38 info!("PUT request"); 40 info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip);
39 let secret = headers.get("authorization"); 41 let secret = headers.get("authorization");
40 if auth(secret).map_err(WebolError::Auth)? { 42 if auth(secret).map_err(WebolError::Auth)? {
41 sqlx::query!( 43 sqlx::query!(
42 r#" 44 r#"
43 INSERT INTO devices (id, mac, broadcast_addr) 45 INSERT INTO devices (id, mac, broadcast_addr, ip)
44 VALUES ($1, $2, $3); 46 VALUES ($1, $2, $3, $4);
45 "#, 47 "#,
46 payload.id, 48 payload.id,
47 payload.mac, 49 payload.mac,
48 payload.broadcast_addr 50 payload.broadcast_addr,
49 ).execute(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 51 payload.ip
52 ).execute(&state.db).await.map_err(WebolError::DB)?;
50 53
51 Ok(Json(json!(PutDeviceResponse { success: true }))) 54 Ok(Json(json!(PutDeviceResponse { success: true })))
52 } else { 55 } else {
@@ -59,6 +62,7 @@ pub struct PutDevicePayload {
59 id: String, 62 id: String,
60 mac: String, 63 mac: String,
61 broadcast_addr: String, 64 broadcast_addr: String,
65 ip: String
62} 66}
63 67
64#[derive(Serialize)] 68#[derive(Serialize)]
@@ -67,20 +71,21 @@ pub struct PutDeviceResponse {
67} 71}
68 72
69pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, WebolError> { 73pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, WebolError> {
70 info!("POST request"); 74 info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip);
71 let secret = headers.get("authorization"); 75 let secret = headers.get("authorization");
72 if auth(secret).map_err(WebolError::Auth)? { 76 if auth(secret).map_err(WebolError::Auth)? {
73 let device = sqlx::query_as!( 77 let device = sqlx::query_as!(
74 Device, 78 Device,
75 r#" 79 r#"
76 UPDATE devices 80 UPDATE devices
77 SET mac = $1, broadcast_addr = $2 WHERE id = $3 81 SET mac = $1, broadcast_addr = $2, ip = $3 WHERE id = $4
78 RETURNING id, mac, broadcast_addr; 82 RETURNING id, mac, broadcast_addr, ip;
79 "#, 83 "#,
80 payload.mac, 84 payload.mac,
81 payload.broadcast_addr, 85 payload.broadcast_addr,
86 payload.ip,
82 payload.id 87 payload.id
83 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?; 88 ).fetch_one(&state.db).await.map_err(WebolError::DB)?;
84 89
85 Ok(Json(json!(device))) 90 Ok(Json(json!(device)))
86 } else { 91 } else {
@@ -93,4 +98,5 @@ pub struct PostDevicePayload {
93 id: String, 98 id: String,
94 mac: String, 99 mac: String,
95 broadcast_addr: String, 100 broadcast_addr: String,
96} \ No newline at end of file 101 ip: String,
102}