|
@@ -1,8 +1,36 @@
|
|
|
use uuid::Uuid;
|
|
use uuid::Uuid;
|
|
|
use chrono::{DateTime, Utc};
|
|
use chrono::{DateTime, Utc};
|
|
|
|
|
+use sqlx::PgPool;
|
|
|
|
|
+use serde::Serialize;
|
|
|
|
|
+use crate::app_error::AppError;
|
|
|
|
|
|
|
|
|
|
+#[derive(Serialize)]
|
|
|
pub struct Home {
|
|
pub struct Home {
|
|
|
pub id: Uuid,
|
|
pub id: Uuid,
|
|
|
pub name: String,
|
|
pub name: String,
|
|
|
pub created_at: DateTime<Utc>
|
|
pub created_at: DateTime<Utc>
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+impl Home {
|
|
|
|
|
+ pub fn new(name: String) -> Self {
|
|
|
|
|
+ Self {
|
|
|
|
|
+ id: Uuid::now_v7(),
|
|
|
|
|
+ name: name,
|
|
|
|
|
+ created_at: Utc::now()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pub async fn insert(&self, db: &PgPool) -> Result<(), AppError> {
|
|
|
|
|
+ sqlx::query!(
|
|
|
|
|
+ "INSERT INTO homes (id, name, created_at)
|
|
|
|
|
+ VALUES($1, $2, $3)",
|
|
|
|
|
+ self.id,
|
|
|
|
|
+ self.name,
|
|
|
|
|
+ self.created_at
|
|
|
|
|
+ )
|
|
|
|
|
+ .execute(db)
|
|
|
|
|
+ .await?;
|
|
|
|
|
+
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+}
|