From 2414cd810ee88217a2422f75591e6e4bc5b4b131 Mon Sep 17 00:00:00 2001 From: Dmitriy Borzenko Date: Wed, 2 Aug 2023 20:24:36 +0300 Subject: [PATCH] Moved the finding of the ID to a separate method. --- .../dataset-create/dataset-create.service.ts | 73 +++++++++---------- 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/src/app/dataset-create/dataset-create.service.ts b/src/app/dataset-create/dataset-create.service.ts index bfca94f21..36f005900 100644 --- a/src/app/dataset-create/dataset-create.service.ts +++ b/src/app/dataset-create/dataset-create.service.ts @@ -5,7 +5,7 @@ import { DatasetByAccountAndDatasetNameQuery, UpdateReadmeMutation, } from "./../api/kamu.graphql.interface"; -import { Observable, Subject } from "rxjs"; +import { Observable, Subject, of } from "rxjs"; import { DatasetApi } from "src/app/api/dataset.api"; import { Injectable } from "@angular/core"; import { DatasetKind } from "../api/kamu.graphql.interface"; @@ -112,31 +112,16 @@ export class AppDatasetCreateService { datasetName: string, event: string, ): Observable { - const key = `${accountName}${datasetName}`; - let observable: Observable< - CommitEventToDatasetMutation | null | undefined - >; - if (this.cache.has(key)) { - observable = this.datasetApi.commitEvent({ - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - datasetId: this.cache.get(key)!, - event, - }); - } else { - observable = this.datasetApi - .getDatasetInfoByAccountAndDatasetName(accountName, datasetName) - .pipe( - switchMap((x: DatasetByAccountAndDatasetNameQuery) => { - const id = x.datasets.byOwnerAndName?.id as string; - this.cache.set(key, id); - return this.datasetApi.commitEvent({ - datasetId: id, - event, - }); - }), - ); - } - return observable.pipe( + return this.getIdByAccountNameAndDatasetName( + accountName, + datasetName, + ).pipe( + switchMap((id: string) => + this.datasetApi.commitEvent({ + datasetId: id, + event, + }), + ), map((data: CommitEventToDatasetMutation | undefined | null) => { if ( data?.datasets.byId?.metadata.chain.commitEvent @@ -154,31 +139,39 @@ export class AppDatasetCreateService { ); } - public updateReadme( + public getIdByAccountNameAndDatasetName( accountName: string, datasetName: string, - content: string, - ): Observable { + ): Observable { const key = `${accountName}${datasetName}`; - let observable: Observable; if (this.cache.has(key)) { - observable = this.datasetApi.updateReadme( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.cache.get(key)!, - content, - ); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return of(this.cache.get(key)!); } else { - observable = this.datasetApi + return this.datasetApi .getDatasetInfoByAccountAndDatasetName(accountName, datasetName) .pipe( - switchMap((x: DatasetByAccountAndDatasetNameQuery) => { - const id = x.datasets.byOwnerAndName?.id as string; + map((data: DatasetByAccountAndDatasetNameQuery) => { + const id = data.datasets.byOwnerAndName?.id as string; this.cache.set(key, id); - return this.datasetApi.updateReadme(id, content); + return id; }), ); } - return observable.pipe( + } + + public updateReadme( + accountName: string, + datasetName: string, + content: string, + ): Observable { + return this.getIdByAccountNameAndDatasetName( + accountName, + datasetName, + ).pipe( + switchMap((id: string) => + this.datasetApi.updateReadme(id, content), + ), map((data: UpdateReadmeMutation | null | undefined) => { if ( data?.datasets.byId?.metadata.updateReadme.__typename ===