Procházet zdrojové kódy

Update the creation route for device_type

Lee Morgan před 2 měsíci
rodič
revize
6f47303042

+ 1 - 1
docs/openapi.yaml

@@ -11,7 +11,7 @@ servers:
 tags:
   - name: User
   - name: Home
-  - name: Devices
+  - name: Device
 
 components:
   schemas:

+ 7 - 0
docs/paths/device/create.yaml

@@ -20,6 +20,7 @@ requestBody:
         type: object
         required:
           - name
+          - device_type
         properties:
           name:
             type: string
@@ -29,6 +30,12 @@ requestBody:
             type: string
             description: Short user created description of the device
             example: North Bedroom Light
+          device_type:
+            type: string
+            description: Type of the device to add
+            example: on_off_switch
+            enum:
+              - on_off_switch
 
 responses:
   "200":

+ 3 - 2
src/controllers/device/create.rs

@@ -11,7 +11,8 @@ use crate::{
 #[derive(Deserialize)]
 struct Body{
     name: String,
-    description: Option<String>
+    description: Option<String>,
+    device_type: String
 }
 
 #[post("/home/{home_id}/device")]
@@ -24,7 +25,7 @@ pub async fn route(
     let user = user_auth(&db, &req).await?;
     let home_id = Uuid::parse_str(&path.into_inner())?;
     let body = body.into_inner();
-    let device = Device::new(home_id, body.name, body.description);
+    let device = Device::new(home_id, body.name, body.description, body.device_type)?;
     device.insert(&db, user.id, home_id).await?;
     Ok(HttpResponse::Ok().json(device))
 }

+ 25 - 8
src/models/device.rs

@@ -8,17 +8,33 @@ pub struct Device {
     id: Uuid,
     home_id: Uuid,
     name: String,
-    description: Option<String>
+    description: Option<String>,
+    device_type: DeviceType
+}
+
+#[derive(Serialize, sqlx::Type)]
+#[sqlx(type_name = "device_type", rename_all = "snake_case")]
+enum DeviceType {
+    OnOffSwitch
 }
 
 impl Device {
-    pub fn new(home_id: Uuid, name: String, description: Option<String>) -> Self {
-        Self {
+    pub fn new(
+        home_id: Uuid,
+        name: String,
+        description: Option<String>,
+        device_type: String
+    ) -> Result<Self, AppError> {
+        Ok(Self {
             id: Uuid::now_v7(),
             home_id: home_id,
             name: name,
-            description: description
-        }
+            description: description,
+            device_type: match device_type.as_str() {
+                "on_off_switch" => DeviceType::OnOffSwitch,
+                _ => {return Err(AppError::invalid_input("Invalid device type"))}
+            }
+        })
     }
 
     pub async fn insert(
@@ -28,12 +44,12 @@ impl Device {
         home_id: Uuid
     ) -> Result<(), AppError> {
         let result: PgQueryResult = sqlx::query!(
-            "INSERT INTO devices (id, home_id, name, description)
-            SELECT $1, $2, $3, $4
+            "INSERT INTO devices (id, home_id, name, description, device_type)
+            SELECT $1, $2, $3, $4, $5
             WHERE EXISTS (
                 SELECT 1
                 FROM users_homes
-                WHERE user_id = $5
+                WHERE user_id = $6
                     AND home_id = $2
                     AND is_admin = true
             )",
@@ -41,6 +57,7 @@ impl Device {
             home_id,
             self.name,
             self.description,
+            &self.device_type as &DeviceType,
             user_id
         )
             .execute(db)