From 3f65d3eb486f699f2f7a14d96c8310a92a504894 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sun, 22 Aug 2021 09:25:00 +0800 Subject: [PATCH] enable sqlx offline --- backend/Cargo.toml | 1 + backend/Dockerfile | 2 +- backend/doc/database_setup.md | 3 +-- backend/scripts/database/database.mk | 2 +- backend/sqlx-data.json | 19 +++++++++++++++++++ backend/src/routers/user.rs | 2 +- backend/src/user_service/auth.rs | 24 +++++++++++++++++++++--- 7 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 backend/sqlx-data.json diff --git a/backend/Cargo.toml b/backend/Cargo.toml index f9e05582dc..e9c70c8944 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -28,6 +28,7 @@ derive_more = {version = "0.99", features = ["display"]} protobuf = {version = "2.20.0"} uuid = { version = "0.8", features = ["serde", "v4"] } config = { version = "0.10.1", default-features = false, features = ["yaml"] } +chrono = "0.4.19" flowy-log = { path = "../rust-lib/flowy-log" } flowy-user = { path = "../rust-lib/flowy-user" } diff --git a/backend/Dockerfile b/backend/Dockerfile index b572ff6108..b642fe993f 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -8,7 +8,7 @@ WORKDIR /app COPY . . # Let's build our binary! -# We'll use the release profile to make it faaaast +# We'll use the release profile to make it fast WORKDIR /app/backend RUN cargo build --release # When `docker run` is executed, launch the binary! diff --git a/backend/doc/database_setup.md b/backend/doc/database_setup.md index 86c25a06c5..83c82cbde0 100644 --- a/backend/doc/database_setup.md +++ b/backend/doc/database_setup.md @@ -13,8 +13,7 @@ bfcdd6369e89 postgres "docker-entrypoint.s…" 19 minutes ago Up 19 minu ``` 4. run `make init_database`. It will create the database on the remote specified by DATABASE_URL. You can connect you database using -pgAdmin. - +pgAdmin. ![img_2.png](img_2.png) diff --git a/backend/scripts/database/database.mk b/backend/scripts/database/database.mk index 0c29b65d32..1385783b22 100644 --- a/backend/scripts/database/database.mk +++ b/backend/scripts/database/database.mk @@ -10,7 +10,7 @@ init_docker: ${ROOT}/docker.sh init_database: - ${ROOT}/init.sh + ${ROOT}/db_init.sh reset_db: sqlx database reset diff --git a/backend/sqlx-data.json b/backend/sqlx-data.json new file mode 100644 index 0000000000..12665e96ba --- /dev/null +++ b/backend/sqlx-data.json @@ -0,0 +1,19 @@ +{ + "db": "PostgreSQL", + "e8c487b4314c267f6da2667b95f6c8003fabc2461c10df2d6d39d081e74e167f": { + "query": "\n INSERT INTO user_table (id, email, name, create_time, password)\n VALUES ($1, $2, $3, $4, $5)\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Uuid", + "Text", + "Text", + "Timestamptz", + "Text" + ] + }, + "nullable": [] + } + } +} \ No newline at end of file diff --git a/backend/src/routers/user.rs b/backend/src/routers/user.rs index 7c2f1210b7..f9b90a8710 100644 --- a/backend/src/routers/user.rs +++ b/backend/src/routers/user.rs @@ -16,7 +16,7 @@ pub async fn register( auth: Data>, ) -> Result { let params: SignUpParams = parse_from_payload(payload).await?; - let _ = auth.sign_up(params)?; + let _ = auth.sign_up(params).await?; let resp = FlowyResponse::success(); diff --git a/backend/src/user_service/auth.rs b/backend/src/user_service/auth.rs index 23f7d8346a..3e590cfc00 100644 --- a/backend/src/user_service/auth.rs +++ b/backend/src/user_service/auth.rs @@ -1,3 +1,4 @@ +use chrono::Utc; use flowy_net::response::{ServerCode, ServerError}; use flowy_user::{entities::SignUpResponse, protobuf::SignUpParams}; use sqlx::PgPool; @@ -10,12 +11,29 @@ pub struct Auth { impl Auth { pub fn new(db_pool: Arc) -> Self { Self { db_pool } } - pub fn sign_up(&self, params: SignUpParams) -> Result { + pub async fn sign_up(&self, params: SignUpParams) -> Result { // email exist? - // generate user id + let result = sqlx::query!( + r#" + INSERT INTO user_table (id, email, name, create_time, password) + VALUES ($1, $2, $3, $4, $5) + "#, + uuid::Uuid::new_v4(), + params.email, + params.name, + Utc::now(), + "123".to_string() + ) + .execute(self.db_pool.as_ref()) + .await; - unimplemented!() + let response = SignUpResponse { + uid: "".to_string(), + name: "".to_string(), + email: "".to_string(), + }; + Ok(response) } pub fn is_email_exist(&self, email: &str) -> bool { true }