From 53d4a5268b1f65f91493cc9815eeb85c357c637e Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Fri, 5 Jul 2024 15:10:11 -0400 Subject: [PATCH] refactor(server): remove has asset (#10891) --- server/src/interfaces/album.interface.ts | 11 ------- server/src/queries/album.repository.sql | 31 ------------------- server/src/repositories/album.repository.ts | 17 +--------- server/src/services/album.service.spec.ts | 4 +-- server/src/services/album.service.ts | 4 +-- .../repositories/album.repository.mock.ts | 1 - 6 files changed, 5 insertions(+), 63 deletions(-) diff --git a/server/src/interfaces/album.interface.ts b/server/src/interfaces/album.interface.ts index 2d48f4da7f..9963612fd6 100644 --- a/server/src/interfaces/album.interface.ts +++ b/server/src/interfaces/album.interface.ts @@ -14,21 +14,10 @@ export interface AlbumInfoOptions { withAssets: boolean; } -export interface AlbumAsset { - albumId: string; - assetId: string; -} - -export interface AlbumAssets { - albumId: string; - assetIds: string[]; -} - export interface IAlbumRepository extends IBulkAsset { getById(id: string, options: AlbumInfoOptions): Promise; getByIds(ids: string[]): Promise; getByAssetId(ownerId: string, assetId: string): Promise; - hasAsset(asset: AlbumAsset): Promise; removeAsset(assetId: string): Promise; getMetadataForIds(ids: string[]): Promise; getInvalidThumbnail(): Promise; diff --git a/server/src/queries/album.repository.sql b/server/src/queries/album.repository.sql index 44097a5a8c..729f7c7f20 100644 --- a/server/src/queries/album.repository.sql +++ b/server/src/queries/album.repository.sql @@ -585,37 +585,6 @@ WHERE "albums_assets"."albumsId" = $1 AND "albums_assets"."assetsId" IN ($2) --- AlbumRepository.hasAsset -SELECT - 1 AS "row_exists" -FROM - ( - SELECT - 1 AS dummy_column - ) "dummy_table" -WHERE - EXISTS ( - SELECT - 1 - FROM - "albums" "AlbumEntity" - LEFT JOIN "albums_assets_assets" "AlbumEntity_AlbumEntity__AlbumEntity_assets" ON "AlbumEntity_AlbumEntity__AlbumEntity_assets"."albumsId" = "AlbumEntity"."id" - LEFT JOIN "assets" "AlbumEntity__AlbumEntity_assets" ON "AlbumEntity__AlbumEntity_assets"."id" = "AlbumEntity_AlbumEntity__AlbumEntity_assets"."assetsId" - AND ( - "AlbumEntity__AlbumEntity_assets"."deletedAt" IS NULL - ) - WHERE - ( - ( - ("AlbumEntity"."id" = $1) - AND ((("AlbumEntity__AlbumEntity_assets"."id" = $2))) - ) - ) - AND ("AlbumEntity"."deletedAt" IS NULL) - ) -LIMIT - 1 - -- AlbumRepository.addAssetIds INSERT INTO "albums_assets_assets" ("albumsId", "assetsId") diff --git a/server/src/repositories/album.repository.ts b/server/src/repositories/album.repository.ts index 845694f389..a44b8dc071 100644 --- a/server/src/repositories/album.repository.ts +++ b/server/src/repositories/album.repository.ts @@ -3,7 +3,7 @@ import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; import { Chunked, ChunkedArray, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators'; import { AlbumEntity } from 'src/entities/album.entity'; import { AssetEntity } from 'src/entities/asset.entity'; -import { AlbumAsset, AlbumAssetCount, AlbumInfoOptions, IAlbumRepository } from 'src/interfaces/album.interface'; +import { AlbumAssetCount, AlbumInfoOptions, IAlbumRepository } from 'src/interfaces/album.interface'; import { Instrumentation } from 'src/utils/instrumentation'; import { DataSource, FindOptionsOrder, FindOptionsRelations, In, IsNull, Not, Repository } from 'typeorm'; @@ -253,21 +253,6 @@ export class AlbumRepository implements IAlbumRepository { return new Set(results.map(({ assetId }) => assetId)); } - @GenerateSql({ params: [{ albumId: DummyValue.UUID, assetId: DummyValue.UUID }] }) - hasAsset(asset: AlbumAsset): Promise { - return this.repository.exist({ - where: { - id: asset.albumId, - assets: { - id: asset.assetId, - }, - }, - relations: { - assets: true, - }, - }); - } - @GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] }) async addAssetIds(albumId: string, assetIds: string[]): Promise { await this.dataSource diff --git a/server/src/services/album.service.spec.ts b/server/src/services/album.service.spec.ts index d111e0c6c4..97549d89f1 100644 --- a/server/src/services/album.service.spec.ts +++ b/server/src/services/album.service.spec.ts @@ -270,7 +270,7 @@ describe(AlbumService.name, () => { accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-4'])); albumMock.getById.mockResolvedValue(albumStub.oneAsset); albumMock.update.mockResolvedValue(albumStub.oneAsset); - albumMock.hasAsset.mockResolvedValue(false); + albumMock.getAssetIds.mockResolvedValue(new Set()); await expect( sut.update(authStub.admin, albumStub.oneAsset.id, { @@ -278,7 +278,7 @@ describe(AlbumService.name, () => { }), ).rejects.toBeInstanceOf(BadRequestException); - expect(albumMock.hasAsset).toHaveBeenCalledWith({ albumId: 'album-4', assetId: 'not-in-album' }); + expect(albumMock.getAssetIds).toHaveBeenCalledWith('album-4', ['not-in-album']); expect(albumMock.update).not.toHaveBeenCalled(); }); diff --git a/server/src/services/album.service.ts b/server/src/services/album.service.ts index 76fc5f182c..7c86d74d45 100644 --- a/server/src/services/album.service.ts +++ b/server/src/services/album.service.ts @@ -146,8 +146,8 @@ export class AlbumService { const album = await this.findOrFail(id, { withAssets: true }); if (dto.albumThumbnailAssetId) { - const valid = await this.albumRepository.hasAsset({ albumId: id, assetId: dto.albumThumbnailAssetId }); - if (!valid) { + const results = await this.albumRepository.getAssetIds(id, [dto.albumThumbnailAssetId]); + if (results.size === 0) { throw new BadRequestException('Invalid album thumbnail'); } } diff --git a/server/test/repositories/album.repository.mock.ts b/server/test/repositories/album.repository.mock.ts index 9b852d7393..af267dd49c 100644 --- a/server/test/repositories/album.repository.mock.ts +++ b/server/test/repositories/album.repository.mock.ts @@ -19,7 +19,6 @@ export const newAlbumRepositoryMock = (): Mocked => { removeAsset: vitest.fn(), removeAssetIds: vitest.fn(), getAssetIds: vitest.fn(), - hasAsset: vitest.fn(), create: vitest.fn(), update: vitest.fn(), delete: vitest.fn(),