|
|
@@ -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?;
|
|
|
|