Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock api 実装リファクタリング part.2 #11

Merged
merged 2 commits into from
Mar 17, 2024

Conversation

gurezo
Copy link
Contributor

@gurezo gurezo commented Mar 17, 2024

実装を簡素にする為に以下のリファクタリングします。

メモ一覧画面用サービス関数戻り値型変更 3c350dd

  • moemoList$ の使用の取りやめ
  • getAll 関数自体の戻り値を使用する
diff --git a/frontend/src/app/todo-list/todo-list.service.ts b/frontend/src/app/todo-list/todo-list.service.ts
index 6cae5fe..93cc40a 100644
--- a/frontend/src/app/todo-list/todo-list.service.ts
+++ b/frontend/src/app/todo-list/todo-list.service.ts
@@ -1,5 +1,5 @@
 import { Injectable, inject } from '@angular/core';
-import { BehaviorSubject, firstValueFrom, map } from 'rxjs';
+import { firstValueFrom, map } from 'rxjs';
 import { Memo, MemoUi } from '../shared/models';
 import { ApiService } from '../shared/service/api.service';

@@ -9,19 +9,14 @@ import { ApiService } from '../shared/service/api.service';
 export class TodoListService {
   api = inject(ApiService);

// moemoList$ の使用の取りやめ
-  moemoList$ = new BehaviorSubject<MemoUi[]>([]);
-
// getAll 関数自体の戻り値を使用する
-  async getAll() {
-    const memoList = await firstValueFrom(
-      // Memo[] => MemoUi[] に変換
+  async getAll(): Promise<MemoUi[]> {
+    // Memo[] => MemoUi[] に変換して返却
+    return await firstValueFrom(
       this.api.getAll().pipe(
         map((memos: Memo[]) => {
           return memos.map((item: Memo) => ({ ...item, checked: false }));
         })
       )
     );
-
-    // MemoUi[] をストリームに流す
-    this.moemoList$.next(memoList);
   }
 }

メモ作成画面用サービス関数戻り値型変更 5014d16

  • memoData$ の使用の取りやめ
  • get 関数自体の戻り値を使用する
diff --git a/frontend/src/app/todo/todo.service.ts b/frontend/src/app/todo/todo.service.ts
index ec99f29..c62666a 100644
--- a/frontend/src/app/todo/todo.service.ts
+++ b/frontend/src/app/todo/todo.service.ts
@@ -1,5 +1,5 @@
 import { Injectable, inject } from '@angular/core';
-import { BehaviorSubject, firstValueFrom, map } from 'rxjs';
+import { firstValueFrom, map } from 'rxjs';
 import { Memo, MemoUi } from '../shared/models';
 import { ApiService } from '../shared/service/api.service';

@@ -9,20 +9,10 @@ import { ApiService } from '../shared/service/api.service';
 export class TodoService {
   api = inject(ApiService);

// memoData$ の使用の取りやめ
-  memoData$ = new BehaviorSubject<MemoUi>({
-    checked: false,
-    id: 0,
-    title: '',
-    memo: '',
-  });
-
// get 関数自体の戻り値を使用する
-  async get(id: string) {
-    const memoData = await firstValueFrom(
-      // Memo => MemoUi に変換
+  async get(id: string): Promise<MemoUi> {
+    // Memo => MemoUi に変換して返却
+    return await firstValueFrom(
       this.api.get(id).pipe(map((item: Memo) => ({ ...item, checked: true })))
     );
-
-    // MemoUi をストリームに流す
-    this.memoData$.next(memoData);
   }
 }

まとめ

  • 上記手順がこのプルリクエストの作業になります。

@gurezo gurezo force-pushed the frontend/mock-api-refactoring branch from 06b9404 to 5014d16 Compare March 17, 2024 06:55
@ic-lifewood ic-lifewood self-assigned this Mar 17, 2024
@ic-lifewood ic-lifewood merged commit 839c3eb into ic-lifewood:main Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants