From 360fc6b6bb8ca810d2cdb8df6842199b4a85aa98 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Sun, 17 May 2026 16:59:26 +0300 Subject: [PATCH] =?UTF-8?q?smart-house-web:=20=D0=B2=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smart-house-web/backend/src/house.rs | 18 ++++++++++++- smart-house-web/backend/src/server.rs | 4 ++- smart-house-web/backend/src/server/rooms.rs | 29 ++++++++++++++++----- smart-house-web/backend/test.http | 13 +++++++-- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/smart-house-web/backend/src/house.rs b/smart-house-web/backend/src/house.rs index 1d9a4d3..2ba164f 100644 --- a/smart-house-web/backend/src/house.rs +++ b/smart-house-web/backend/src/house.rs @@ -79,7 +79,7 @@ impl From for Device { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct Room { devices: HashMap, } @@ -99,4 +99,20 @@ impl House { pub fn new(rooms: HashMap) -> Self { Self { rooms } } + + pub fn rooms(&self) -> Vec { + let mut output = Vec::with_capacity(self.rooms.len()); + for (room, _) in self.rooms.iter() { + output.push(room.into()); + } + return output; + } + + pub fn add_room(&mut self, name: String) { + self.rooms.insert(name, Room::default()); + } + + pub fn drop_room(&mut self, name: &str) { + self.rooms.remove(name); + } } diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs index a8f5182..5ca00f2 100644 --- a/smart-house-web/backend/src/server.rs +++ b/smart-house-web/backend/src/server.rs @@ -1,4 +1,4 @@ -use axum::routing::get; +use axum::routing::{delete, get, put}; use std::{ collections::HashMap, process::exit, @@ -45,6 +45,8 @@ async fn server_main() { let app = axum::Router::new() .route("/debug", get(debug::debug)) .route("/rooms", get(rooms::get_rooms)) + .route("/room", put(rooms::add_room)) + .route("/room/{name}", delete(rooms::remove_room)) // TODO .with_state(state) .fallback(fallback); diff --git a/smart-house-web/backend/src/server/rooms.rs b/smart-house-web/backend/src/server/rooms.rs index a3d7faa..26ae96e 100644 --- a/smart-house-web/backend/src/server/rooms.rs +++ b/smart-house-web/backend/src/server/rooms.rs @@ -1,8 +1,25 @@ -use axum::{Json, extract::State}; +use axum::{ + Json, + extract::{Path, State}, + http::StatusCode, +}; -use crate::Room; - -pub async fn get_rooms(State(server_state): State) -> Json> { - let _x = server_state.read().await.get_rooms(); - todo!() +pub async fn get_rooms(State(server_state): State) -> Json> { + server_state.read().await.rooms().into() +} + +pub async fn add_room( + State(server_state): State, + Json(name): Json, +) -> StatusCode { + server_state.write().await.add_room(name); + StatusCode::CREATED +} + +pub async fn remove_room( + State(server_state): State, + Path(name): Path, +) -> StatusCode { + server_state.write().await.drop_room(&name); + StatusCode::ACCEPTED } diff --git a/smart-house-web/backend/test.http b/smart-house-web/backend/test.http index 49bb354..ba4d403 100644 --- a/smart-house-web/backend/test.http +++ b/smart-house-web/backend/test.http @@ -1,5 +1,14 @@ -### +### DEBUG GET http://localhost:8080/debug + +### list rooms +GET http://localhost:8080/rooms + +### add room +PUT http://localhost:8080/room Content-Type: application/json -{} +"ROOM" + +### drop room +DELETE http://localhost:8080/room/ROOM