Skip to content

Commit

Permalink
Moved the finding of the ID to a separate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Borzenko committed Aug 2, 2023
1 parent 64affb5 commit 2414cd8
Showing 1 changed file with 33 additions and 40 deletions.
73 changes: 33 additions & 40 deletions src/app/dataset-create/dataset-create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -112,31 +112,16 @@ export class AppDatasetCreateService {
datasetName: string,
event: string,
): Observable<void> {
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
Expand All @@ -154,31 +139,39 @@ export class AppDatasetCreateService {
);
}

public updateReadme(
public getIdByAccountNameAndDatasetName(
accountName: string,
datasetName: string,
content: string,
): Observable<void> {
): Observable<string> {
const key = `${accountName}${datasetName}`;
let observable: Observable<UpdateReadmeMutation | null | undefined>;
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<void> {
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 ===
Expand Down

0 comments on commit 2414cd8

Please sign in to comment.