Skip to content

Commit

Permalink
Merge pull request #70 from Zli-UoA/ticket/#41-get-user-api
Browse files Browse the repository at this point in the history
特定ユーザ取得APIの実装
  • Loading branch information
noharu36 authored Sep 10, 2024
2 parents 9b9b835 + e8d1c48 commit c76ad8c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/ci-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,49 @@ jobs:
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
- name: format check
run: |
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
2 changes: 2 additions & 0 deletions backend/db-schema/migrations/20240908082102_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "updated_at" DROP NOT NULL;
2 changes: 1 addition & 1 deletion backend/db-schema/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
2 changes: 1 addition & 1 deletion backend/src/generate/entities/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateTime>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
1 change: 1 addition & 0 deletions backend/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod mutation;
pub mod query;
mod test_handler;
mod user;
4 changes: 3 additions & 1 deletion backend/src/handler/query.rs
Original file line number Diff line number Diff line change
@@ -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);
27 changes: 27 additions & 0 deletions backend/src/handler/user.rs
Original file line number Diff line number Diff line change
@@ -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<models::user::User> {
let ctx = ctx.data_unchecked::<Context>();

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")),
}
}
}
38 changes: 21 additions & 17 deletions backend/src/usecase/user.rs
Original file line number Diff line number Diff line change
@@ -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<User, ()> {
todo!();
pub async fn get_user_by_id(ctx: &Context, id: &str) -> Result<Option<User>, ()> {
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)]
Expand All @@ -27,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();

Expand All @@ -43,29 +55,21 @@ pub mod test {
// Assert
assert_eq!(
result,
Ok(User {
Ok(Some(User {
id: id.to_string(),
name: "aiueo".to_string(),
display_name: "あいうえお".to_string(),
})
}))
);
}

#[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::<user::Model>::new()])
.into_connection();

let ctx = Context {
Expand All @@ -77,6 +81,6 @@ pub mod test {
let result = get_user_by_id(&ctx, "存在しないID").await;

// Assert
assert_eq!(result, Err(()));
assert_eq!(result, Ok(None));
}
}

0 comments on commit c76ad8c

Please sign in to comment.