فهرست منبع

Add api key to home struct

Lee Morgan 2 ماه پیش
والد
کامیت
b4ac84c5b6
1فایلهای تغییر یافته به همراه9 افزوده شده و 7 حذف شده
  1. 9 7
      src/models/home.rs

+ 9 - 7
src/models/home.rs

@@ -1,12 +1,13 @@
 use uuid::Uuid;
 use chrono::{DateTime, Utc};
-use sqlx::PgPool;
+use sqlx::{PgPool, FromRow};
 use serde::Serialize;
 use crate::app_error::AppError;
 
-#[derive(Serialize)]
+#[derive(Serialize, FromRow)]
 pub struct Home {
     pub id: Uuid,
+    pub api_key: Uuid,
     pub name: String,
     pub created_at: DateTime<Utc>,
     pub is_admin: Option<bool>
@@ -16,6 +17,7 @@ impl Home {
     pub fn new(name: String) -> Self {
         Self {
             id: Uuid::now_v7(),
+            api_key: Uuid::new_v4(),
             name: name,
             created_at: Utc::now(),
             is_admin: Some(true)
@@ -63,13 +65,13 @@ impl Home {
     }
 
     pub async fn get_many(db: &PgPool, user_id: Uuid) -> Result<Vec<Home>, AppError> {
-        let homes = sqlx::query_as!(
-            Home,
-            "SELECT homes.*, users_homes.is_admin FROM homes
+        let homes = sqlx::query_as::<_, Home>(
+            "SELECT homes.*, users_homes.is_admin
+            FROM homes
             JOIN users_homes ON users_homes.home_id = homes.id
-            WHERE users_homes.user_id = $1",
-            user_id
+            WHERE users_homes.user_id = $1"
         )
+            .bind(user_id)
             .fetch_all(db)
             .await?;