Переглянути джерело

Add route to create new home

Lee Morgan 2 місяців тому
батько
коміт
0ad28869c7

+ 2 - 0
Cargo.lock

@@ -412,6 +412,7 @@ dependencies = [
  "iana-time-zone",
  "js-sys",
  "num-traits",
+ "serde",
  "wasm-bindgen",
  "windows-link",
 ]
@@ -2461,6 +2462,7 @@ checksum = "ddd74a9687298c6858e9b88ec8935ec45d22e8fd5e6394fa1bd4e99a87789c76"
 dependencies = [
  "getrandom 0.4.2",
  "js-sys",
+ "serde_core",
  "wasm-bindgen",
 ]
 

+ 2 - 2
Cargo.toml

@@ -7,7 +7,7 @@ edition = "2024"
 actix-cors = "0.7.1"
 actix-web = "4.13.0"
 argon2 = "0.5.3"
-chrono = "0.4.44"
+chrono = { version="0.4.44", features=["serde"] }
 email_address = "0.2.9"
 serde = "1.0.228"
 serde_json = "1.0.149"
@@ -19,4 +19,4 @@ sqlx = { version = "0.8.6", features = [
     "uuid"
 ]}
 thiserror = "2.0.18"
-uuid = { version="1.23.1", features=["v4", "v7"] }
+uuid = { version="1.23.1", features=["v4", "v7", "serde"] }

+ 25 - 0
src/controllers/home/create.rs

@@ -0,0 +1,25 @@
+use actix_web::{HttpResponse, HttpRequest, web, post};
+use serde::Deserialize;
+use sqlx::PgPool;
+use crate::{
+    app_error::AppError,
+    auth::user_auth,
+    models::home::Home
+};
+
+#[derive(Deserialize)]
+struct Body {
+    name: String
+}
+
+#[post("/home")]
+pub async fn route(
+    db: web::Data<PgPool>,
+    body: web::Json<Body>,
+    req: HttpRequest
+) -> Result<HttpResponse, AppError> {
+    user_auth(&db, &req).await?;
+    let home = Home::new(body.into_inner().name);
+    home.insert(&db).await?;
+    Ok(HttpResponse::Ok().json(home))
+}

+ 1 - 0
src/controllers/home/mod.rs

@@ -0,0 +1 @@
+pub mod create;

+ 1 - 0
src/controllers/mod.rs

@@ -1 +1,2 @@
 pub mod user;
+pub mod home;

+ 1 - 0
src/main.rs

@@ -40,6 +40,7 @@ async fn main() -> Result<(), AppError> {
                 })
             )
             .configure(routes::user::config)
+            .configure(routes::home::config)
     })
         .bind(("0.0.0.0", 8000))
         .expect("Failed to bind to port")

+ 28 - 0
src/models/home.rs

@@ -1,8 +1,36 @@
 use uuid::Uuid;
 use chrono::{DateTime, Utc};
+use sqlx::PgPool;
+use serde::Serialize;
+use crate::app_error::AppError;
 
+#[derive(Serialize)]
 pub struct Home {
     pub id: Uuid,
     pub name: String,
     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(())
+    }
+}

+ 8 - 0
src/routes/home.rs

@@ -0,0 +1,8 @@
+use actix_web::web;
+use crate::controllers::home::{
+    create
+};
+
+pub fn config(cfg: &mut web::ServiceConfig) {
+    cfg.service(create::route);
+}

+ 1 - 0
src/routes/mod.rs

@@ -1 +1,2 @@
 pub mod user;
+pub mod home;