From 9abd20acd2c7e87c18bb6b9f11392cca834da5ff Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:26:20 +0900 Subject: [PATCH 01/12] =?UTF-8?q?usecase/user=20->=20get=5Fuser=5Fby=5Fid?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 見つからなかった場合(404)とDBでエラーが発生した場合(Internal server error)を別で扱いたいのでResultからResult>に変更 これに伴いテストの軽い修正(テストしたい振る舞い自体は変わらないうえ軽い修正なので一緒にやった) --- backend/src/usecase/user.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/src/usecase/user.rs b/backend/src/usecase/user.rs index 156596d..cb00209 100644 --- a/backend/src/usecase/user.rs +++ b/backend/src/usecase/user.rs @@ -1,7 +1,20 @@ +use sea_orm::EntityTrait; + +use crate::generate::entities::user; use crate::{context::Context, models::user::User}; -pub async fn get_user_by_id(_ctx: &Context, _id: &str) -> Result { - todo!(); +pub async fn get_user_by_id(ctx: &Context, id: &str) -> Result, ()> { + let db = &ctx.db; + + let Ok(user) = user::Entity::find_by_id(id).one(db).await else { + return Err(()); + }; + + Ok(user.map(|user| User { + id: user.id, + name: user.user_name, + display_name: user.display_name, + })) } #[cfg(test)] @@ -43,11 +56,11 @@ pub mod test { // Assert assert_eq!( result, - Ok(User { + Ok(Some(User { id: id.to_string(), name: "aiueo".to_string(), display_name: "あいうえお".to_string(), - }) + })) ); } @@ -77,6 +90,6 @@ pub mod test { let result = get_user_by_id(&ctx, "存在しないID").await; // Assert - assert_eq!(result, Err(())); + assert_eq!(result, Ok(None)); } } From 89f4360a9f05ef64819a59504fba58dd580ddb68 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:28:47 +0900 Subject: [PATCH 02/12] =?UTF-8?q?handler/user=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/handler.rs | 1 + backend/src/handler/query.rs | 4 +++- backend/src/handler/user.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 backend/src/handler/user.rs diff --git a/backend/src/handler.rs b/backend/src/handler.rs index 827aa39..facb74c 100644 --- a/backend/src/handler.rs +++ b/backend/src/handler.rs @@ -1,3 +1,4 @@ pub mod mutation; pub mod query; mod test_handler; +mod user; diff --git a/backend/src/handler/query.rs b/backend/src/handler/query.rs index afed9b5..dc056aa 100644 --- a/backend/src/handler/query.rs +++ b/backend/src/handler/query.rs @@ -1,5 +1,7 @@ use crate::handler::test_handler::TestQuery; use async_graphql::MergedObject; +use crate::handler::user::UserQuery; + #[derive(MergedObject, Default)] -pub struct Query(TestQuery); +pub struct Query(TestQuery, UserQuery); diff --git a/backend/src/handler/user.rs b/backend/src/handler/user.rs new file mode 100644 index 0000000..e778595 --- /dev/null +++ b/backend/src/handler/user.rs @@ -0,0 +1,27 @@ +use async_graphql::Error; +use async_graphql::Object; +use async_graphql::Result; + +use crate::context::Context; +use crate::models; +use crate::usecase; + +#[derive(Default)] +pub struct UserQuery; + +#[Object] +impl UserQuery { + async fn user<'ctx>( + &self, + ctx: &async_graphql::Context<'ctx>, + id: String, + ) -> Result { + let ctx = ctx.data_unchecked::(); + + match usecase::user::get_user_by_id(ctx, id.as_str()).await { + Ok(Some(user)) => Ok(user), + Ok(None) => Err(Error::new("Not found")), + Err(_) => Err(Error::new("Internal server error")), + } + } +} From 9249897cb2239d6f21c83523d89fef023eecec51 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:14:47 +0900 Subject: [PATCH 03/12] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit モックの場合find_by_idで渡したidと一致しなくても最初の要素が返ってきてしまうためからのVectorに変更した --- backend/src/usecase/user.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/backend/src/usecase/user.rs b/backend/src/usecase/user.rs index cb00209..a5d6197 100644 --- a/backend/src/usecase/user.rs +++ b/backend/src/usecase/user.rs @@ -65,20 +65,12 @@ pub mod test { } #[tokio::test] - async fn 指定IDのユーザが存在しない場合失敗() { + async fn 指定IDのユーザが存在しない場合None() { // Arrange let id = "4e36eb58-49a5-43aa-935f-5a5cccb77a90"; let db: DatabaseConnection = MockDatabase::new(sea_orm::DatabaseBackend::Postgres) - .append_query_results([vec![user::Model { - id: id.to_string(), - user_name: "aiueo".to_string(), - display_name: "あいうえお".to_string(), - created_at: DateTime::parse_from_str("2024-08-08 00:00:00", "%Y-%m-%d %H:%M:%S") - .unwrap(), - updated_at: DateTime::parse_from_str("2024-08-08 00:00:00", "%Y-%m-%d %H:%M:%S") - .unwrap(), - }]]) + .append_query_results(vec![Vec::::new()]) .into_connection(); let ctx = Context { From dee3f483df7db3434b8b910b7ec962a59080c29a Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Sun, 8 Sep 2024 17:20:36 +0900 Subject: [PATCH 04/12] =?UTF-8?q?User=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E3=82=B9=E3=82=AD=E3=83=BC=E3=83=9E=E3=82=92=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updated_atをオプショナルにした --- backend/db-schema/schema.prisma | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/db-schema/schema.prisma b/backend/db-schema/schema.prisma index 126fcb8..5b6d6e3 100644 --- a/backend/db-schema/schema.prisma +++ b/backend/db-schema/schema.prisma @@ -23,7 +23,7 @@ model User { user_name String @unique display_name String @db.VarChar(255) created_at DateTime @default(now()) - updated_at DateTime @updatedAt + updated_at DateTime? @updatedAt channel Channel[] message Message[] channel_user ChannelUser[] From b4bd285b8719ce18ff30a6b9c97adc8c5e8226c1 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Sun, 8 Sep 2024 17:21:49 +0900 Subject: [PATCH 05/12] migrate db for dee3f483df7db3434b8b910b7ec962a59080c29a --- backend/db-schema/migrations/20240908082102_/migration.sql | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 backend/db-schema/migrations/20240908082102_/migration.sql diff --git a/backend/db-schema/migrations/20240908082102_/migration.sql b/backend/db-schema/migrations/20240908082102_/migration.sql new file mode 100644 index 0000000..fdbd511 --- /dev/null +++ b/backend/db-schema/migrations/20240908082102_/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "User" ALTER COLUMN "updated_at" DROP NOT NULL; From 1361aa6937833168751580d18a42790f8205abdb Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Sun, 8 Sep 2024 17:22:36 +0900 Subject: [PATCH 06/12] generate entities --- backend/src/generate/entities/user.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/generate/entities/user.rs b/backend/src/generate/entities/user.rs index a1b67da..63a5377 100644 --- a/backend/src/generate/entities/user.rs +++ b/backend/src/generate/entities/user.rs @@ -11,7 +11,7 @@ pub struct Model { pub user_name: String, pub display_name: String, pub created_at: DateTime, - pub updated_at: DateTime, + pub updated_at: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] From ba63e02ffd9bebec660ad88bc5cd90a1831299b2 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Sun, 8 Sep 2024 17:23:53 +0900 Subject: [PATCH 07/12] fix test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit user方が変更されたので修正 --- backend/src/usecase/user.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/src/usecase/user.rs b/backend/src/usecase/user.rs index a5d6197..f34a5f4 100644 --- a/backend/src/usecase/user.rs +++ b/backend/src/usecase/user.rs @@ -40,8 +40,7 @@ pub mod test { display_name: "あいうえお".to_string(), created_at: DateTime::parse_from_str("2024-08-08 00:00:00", "%Y-%m-%d %H:%M:%S") .unwrap(), - updated_at: DateTime::parse_from_str("2024-08-08 00:00:00", "%Y-%m-%d %H:%M:%S") - .unwrap(), + updated_at: None, }]]) .into_connection(); From 17143613535507451c4b099b63dd2f9a71f5c89a Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Tue, 10 Sep 2024 22:41:51 +0900 Subject: [PATCH 08/12] add ci test --- .github/workflows/ci-backend.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci-backend.yaml b/.github/workflows/ci-backend.yaml index 0c96067..26d6b40 100644 --- a/.github/workflows/ci-backend.yaml +++ b/.github/workflows/ci-backend.yaml @@ -36,3 +36,8 @@ jobs: - name: format check run: | cargo fmt --check + + - name: test + run: | + cargo test + From 6daeb099cc5c42c49f49fefbb3c32ef1152ecec2 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Tue, 10 Sep 2024 22:52:31 +0900 Subject: [PATCH 09/12] =?UTF-8?q?ci=E3=81=AEjobs=E3=82=92=E5=88=86?= =?UTF-8?q?=E3=81=91=E3=81=A6=E3=81=BF=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci-backend.yaml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-backend.yaml b/.github/workflows/ci-backend.yaml index 26d6b40..87c34f9 100644 --- a/.github/workflows/ci-backend.yaml +++ b/.github/workflows/ci-backend.yaml @@ -3,12 +3,12 @@ name: CI on: push: paths: - - "backend/**" + - "./**" workflow_dispatch: jobs: - build: + setup: runs-on: ubuntu-latest defaults: run: @@ -25,10 +25,14 @@ jobs: with: workspaces: backend + build: + steps: - name: Build project run: | cargo build + lint: + steps: - name: lint check run: | cargo clippy @@ -37,6 +41,8 @@ jobs: run: | cargo fmt --check + test: + steps: - name: test run: | cargo test From 4f10ea3080f1e43fdd6f6cfed548476af1ca9257 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Tue, 10 Sep 2024 22:54:11 +0900 Subject: [PATCH 10/12] =?UTF-8?q?ci=E3=82=92=E7=9B=B4=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=81=AE=E3=81=A8=E3=80=81=E6=A4=9C=E8=A8=BC=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E9=81=A9=E5=BD=93=E3=81=AA=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci-backend.yaml | 2 +- backend/src/usecase/user.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci-backend.yaml b/.github/workflows/ci-backend.yaml index 87c34f9..5d49335 100644 --- a/.github/workflows/ci-backend.yaml +++ b/.github/workflows/ci-backend.yaml @@ -3,7 +3,7 @@ name: CI on: push: paths: - - "./**" + - "backend/**" workflow_dispatch: diff --git a/backend/src/usecase/user.rs b/backend/src/usecase/user.rs index f34a5f4..28201c1 100644 --- a/backend/src/usecase/user.rs +++ b/backend/src/usecase/user.rs @@ -4,6 +4,7 @@ use crate::generate::entities::user; use crate::{context::Context, models::user::User}; pub async fn get_user_by_id(ctx: &Context, id: &str) -> Result, ()> { + // hoge let db = &ctx.db; let Ok(user) = user::Entity::find_by_id(id).one(db).await else { From 67ca071ff672673e1b221211604be111a3c0f009 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Tue, 10 Sep 2024 22:57:36 +0900 Subject: [PATCH 11/12] fix ci --- .github/workflows/ci-backend.yaml | 34 ++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci-backend.yaml b/.github/workflows/ci-backend.yaml index 5d49335..d8551c0 100644 --- a/.github/workflows/ci-backend.yaml +++ b/.github/workflows/ci-backend.yaml @@ -8,7 +8,7 @@ on: jobs: - setup: + build: runs-on: ubuntu-latest defaults: run: @@ -25,14 +25,27 @@ jobs: with: workspaces: backend - build: - steps: - name: Build project run: | cargo build lint: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./backend steps: + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Checkout + uses: actions/checkout@v4 + + - name: Restore Cache + uses: Swatinem/rust-cache@v2 + with: + workspaces: backend + - name: lint check run: | cargo clippy @@ -42,7 +55,22 @@ jobs: cargo fmt --check test: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./backend steps: + - name: Setup Rust + uses: dtolnay/rust-toolchain@stable + + - name: Checkout + uses: actions/checkout@v4 + + - name: Restore Cache + uses: Swatinem/rust-cache@v2 + with: + workspaces: backend + - name: test run: | cargo test From e8d1c485a8de03896ddffd78be2c2402b441f5d0 Mon Sep 17 00:00:00 2001 From: eraser5th <83107074+eraser5th@users.noreply.github.com> Date: Tue, 10 Sep 2024 22:58:50 +0900 Subject: [PATCH 12/12] =?UTF-8?q?ci=E3=82=92=E7=99=BA=E7=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=81=9F=E3=82=81=E3=81=AB=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/usecase/user.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/usecase/user.rs b/backend/src/usecase/user.rs index 28201c1..f34a5f4 100644 --- a/backend/src/usecase/user.rs +++ b/backend/src/usecase/user.rs @@ -4,7 +4,6 @@ use crate::generate::entities::user; use crate::{context::Context, models::user::User}; pub async fn get_user_by_id(ctx: &Context, id: &str) -> Result, ()> { - // hoge let db = &ctx.db; let Ok(user) = user::Entity::find_by_id(id).one(db).await else {