summaryrefslogtreecommitdiff
path: root/src/routes/device.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-10-09 17:26:59 +0200
committerfx <[email protected]>2023-10-09 17:26:59 +0200
commit3e6a72428824c5a50a873a4284b86d0a9e47a778 (patch)
tree7f3594f4068a8009210039bc33e0205a672828f7 /src/routes/device.rs
parent732c487d3dab4af9fc561527591d3d56299e39f2 (diff)
downloadwebol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar
webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.tar.gz
webol-3e6a72428824c5a50a873a4284b86d0a9e47a778.zip
db int for api
Diffstat (limited to 'src/routes/device.rs')
-rw-r--r--src/routes/device.rs92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
new file mode 100644
index 0000000..d5d7144
--- /dev/null
+++ b/src/routes/device.rs
@@ -0,0 +1,92 @@
1use std::sync::Arc;
2use axum::extract::State;
3use axum::headers::HeaderMap;
4use axum::Json;
5use serde::{Deserialize, Serialize};
6use serde_json::{json, Value};
7use crate::auth::auth;
8use crate::db::Device;
9use crate::error::WebolError;
10
11pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, WebolError> {
12 let secret = headers.get("authorization");
13 if auth(secret).map_err(WebolError::Auth)? {
14 let device = sqlx::query_as!(
15 Device,
16 r#"
17 SELECT id, mac, broadcast_addr
18 FROM devices
19 WHERE id = $1;
20 "#,
21 payload.id
22 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?;
23
24 Ok(Json(json!(device)))
25 } else {
26 Err(WebolError::Generic)
27 }
28}
29
30#[derive(Deserialize)]
31pub struct GetDevicePayload {
32 id: String,
33}
34
35pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, WebolError> {
36 let secret = headers.get("authorization");
37 if auth(secret).map_err(WebolError::Auth)? {
38 sqlx::query!(
39 r#"
40 INSERT INTO devices (id, mac, broadcast_addr)
41 VALUES ($1, $2, $3);
42 "#,
43 payload.id,
44 payload.mac,
45 payload.broadcast_addr
46 ).execute(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?;
47
48 Ok(Json(json!(PutDeviceResponse { success: true })))
49 } else {
50 Err(WebolError::Generic)
51 }
52}
53
54#[derive(Deserialize)]
55pub struct PutDevicePayload {
56 id: String,
57 mac: String,
58 broadcast_addr: String,
59}
60
61#[derive(Serialize)]
62pub struct PutDeviceResponse {
63 success: bool
64}
65
66pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, WebolError> {
67 let secret = headers.get("authorization");
68 if auth(secret).map_err(WebolError::Auth)? {
69 let device = sqlx::query_as!(
70 Device,
71 r#"
72 UPDATE devices
73 SET mac = $1, broadcast_addr = $2 WHERE id = $3
74 RETURNING id, mac, broadcast_addr;
75 "#,
76 payload.mac,
77 payload.broadcast_addr,
78 payload.id
79 ).fetch_one(&state.db).await.map_err(|err| WebolError::Server(Box::new(err)))?;
80
81 Ok(Json(json!(device)))
82 } else {
83 Err(WebolError::Generic)
84 }
85}
86
87#[derive(Deserialize)]
88pub struct PostDevicePayload {
89 id: String,
90 mac: String,
91 broadcast_addr: String,
92} \ No newline at end of file