From 95d0f5d16f378f041ced067c4b14595ccd17734d Mon Sep 17 00:00:00 2001
From: Thomas <4159106+dtslvr@users.noreply.github.com>
Date: Wed, 24 May 2023 20:55:10 +0200
Subject: [PATCH] Refactoring
---
.../src/app/benchmark/benchmark.controller.ts | 24 +++++++++-------
.../src/app/benchmark/benchmark.service.ts | 11 ++++----
.../admin-market-data.component.ts | 28 ++-----------------
.../admin-market-data/admin-market-data.html | 19 -------------
.../asset-profile-dialog.component.ts | 23 +++++++++++++--
.../asset-profile-dialog.html | 7 +++++
libs/common/src/lib/exceptions/index.ts | 3 --
.../src/lib/exceptions/not-found-error.ts | 6 ----
8 files changed, 49 insertions(+), 72 deletions(-)
delete mode 100644 libs/common/src/lib/exceptions/index.ts
delete mode 100644 libs/common/src/lib/exceptions/not-found-error.ts
diff --git a/apps/api/src/app/benchmark/benchmark.controller.ts b/apps/api/src/app/benchmark/benchmark.controller.ts
index 473d82b74..862e8442d 100644
--- a/apps/api/src/app/benchmark/benchmark.controller.ts
+++ b/apps/api/src/app/benchmark/benchmark.controller.ts
@@ -23,7 +23,6 @@ import { BenchmarkService } from './benchmark.service';
import { REQUEST } from '@nestjs/core';
import { RequestWithUser } from '@ghostfolio/common/types';
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
-import { NotFoundError } from '@ghostfolio/common/exceptions';
@Controller('benchmark')
export class BenchmarkController {
@@ -60,7 +59,7 @@ export class BenchmarkController {
@Post()
@UseGuards(AuthGuard('jwt'))
- public async addBenchmark(@Body() benchmark: UniqueAsset) {
+ public async addBenchmark(@Body() { dataSource, symbol }: UniqueAsset) {
if (
!hasPermission(
this.request.user.permissions,
@@ -74,19 +73,24 @@ export class BenchmarkController {
}
try {
- return await this.benchmarkService.addBenchmark(benchmark);
- } catch (error) {
- if (error instanceof NotFoundError) {
+ const benchmark = await this.benchmarkService.addBenchmark({
+ dataSource,
+ symbol
+ });
+
+ if (!benchmark) {
throw new HttpException(
getReasonPhrase(StatusCodes.NOT_FOUND),
StatusCodes.NOT_FOUND
);
- } else {
- throw new HttpException(
- getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR),
- StatusCodes.INTERNAL_SERVER_ERROR
- );
}
+
+ return benchmark;
+ } catch {
+ throw new HttpException(
+ getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR),
+ StatusCodes.INTERNAL_SERVER_ERROR
+ );
}
}
}
diff --git a/apps/api/src/app/benchmark/benchmark.service.ts b/apps/api/src/app/benchmark/benchmark.service.ts
index 1065e3fcb..a01af1f1a 100644
--- a/apps/api/src/app/benchmark/benchmark.service.ts
+++ b/apps/api/src/app/benchmark/benchmark.service.ts
@@ -2,6 +2,7 @@ import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.s
import { SymbolService } from '@ghostfolio/api/app/symbol/symbol.service';
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
+import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service';
import {
@@ -21,8 +22,6 @@ import Big from 'big.js';
import { format } from 'date-fns';
import ms from 'ms';
import { uniqBy } from 'lodash';
-import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
-import { NotFoundError } from '@ghostfolio/common/exceptions';
@Injectable()
export class BenchmarkService {
@@ -221,21 +220,21 @@ export class BenchmarkService {
});
if (!symbolProfile) {
- throw new NotFoundError('Symbol profile not found');
+ return;
}
- const benchmarks =
+ let benchmarks =
((await this.propertyService.getByKey(
PROPERTY_BENCHMARKS
)) as BenchmarkProperty[]) ?? [];
benchmarks.push({ symbolProfileId: symbolProfile.id } as BenchmarkProperty);
- const newBenchmarks = uniqBy(benchmarks, 'symbolProfileId');
+ benchmarks = uniqBy(benchmarks, 'symbolProfileId');
await this.propertyService.put({
key: PROPERTY_BENCHMARKS,
- value: JSON.stringify(newBenchmarks)
+ value: JSON.stringify(benchmarks)
});
return {
diff --git a/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts b/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts
index 7abc79f96..26a235000 100644
--- a/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts
+++ b/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts
@@ -14,23 +14,13 @@ import { AdminService } from '@ghostfolio/client/services/admin.service';
import { DataService } from '@ghostfolio/client/services/data.service';
import { UserService } from '@ghostfolio/client/services/user/user.service';
import { getDateFormatString } from '@ghostfolio/common/helper';
-import {
- Filter,
- InfoItem,
- UniqueAsset,
- User
-} from '@ghostfolio/common/interfaces';
+import { Filter, UniqueAsset, User } from '@ghostfolio/common/interfaces';
import { AdminMarketDataItem } from '@ghostfolio/common/interfaces/admin-market-data.interface';
import { translate } from '@ghostfolio/ui/i18n';
import { AssetSubClass, DataSource, SymbolProfile } from '@prisma/client';
import { DeviceDetectorService } from 'ngx-device-detector';
import { Subject } from 'rxjs';
-import {
- distinctUntilChanged,
- switchMap,
- take,
- takeUntil
-} from 'rxjs/operators';
+import { distinctUntilChanged, switchMap, takeUntil } from 'rxjs/operators';
import { AssetProfileDialog } from './asset-profile-dialog/asset-profile-dialog.component';
import { AssetProfileDialogParams } from './asset-profile-dialog/interfaces/interfaces';
@@ -155,13 +145,6 @@ export class AdminMarketDataComponent implements OnDestroy, OnInit {
});
}
- public isBenchmark({ dataSource, symbol }: UniqueAsset) {
- return this.benchmarks.some(
- (benchmark) =>
- benchmark.dataSource === dataSource && benchmark.symbol === symbol
- );
- }
-
public onDeleteProfileData({ dataSource, symbol }: UniqueAsset) {
this.adminService
.deleteProfileData({ dataSource, symbol })
@@ -205,13 +188,6 @@ export class AdminMarketDataComponent implements OnDestroy, OnInit {
.subscribe(() => {});
}
- public onSetBenchmark(benchmark: UniqueAsset) {
- this.dataService
- .postBenchmark(benchmark)
- .pipe(takeUntil(this.unsubscribeSubject))
- .subscribe(() => {});
- }
-
public onGatherSymbol({ dataSource, symbol }: UniqueAsset) {
this.adminService
.gatherSymbol({ dataSource, symbol })
diff --git a/apps/client/src/app/components/admin-market-data/admin-market-data.html b/apps/client/src/app/components/admin-market-data/admin-market-data.html
index 7b3e80df4..bb9322a68 100644
--- a/apps/client/src/app/components/admin-market-data/admin-market-data.html
+++ b/apps/client/src/app/components/admin-market-data/admin-market-data.html
@@ -143,25 +143,6 @@
-
-
-
+
diff --git a/libs/common/src/lib/exceptions/index.ts b/libs/common/src/lib/exceptions/index.ts
deleted file mode 100644
index e5e85e1de..000000000
--- a/libs/common/src/lib/exceptions/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-import { NotFoundError } from './not-found-error';
-
-export { NotFoundError };
diff --git a/libs/common/src/lib/exceptions/not-found-error.ts b/libs/common/src/lib/exceptions/not-found-error.ts
deleted file mode 100644
index 84bdc80f6..000000000
--- a/libs/common/src/lib/exceptions/not-found-error.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export class NotFoundError extends Error {
- constructor(message: string) {
- super(message);
- this.name = 'NotFoundError';
- }
-}