From 890de2dea19892537962ef4f989bab2170f41ce0 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Fri, 19 Mar 2021 16:47:52 +0100 Subject: [PATCH] MediaService: Add listUploadsByNote method Signed-off-by: Philip Molares --- src/media/media.service.spec.ts | 36 +++++++++++++++++++++++++++++++++ src/media/media.service.ts | 18 +++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/media/media.service.spec.ts b/src/media/media.service.spec.ts index a71464e81..bab440fdc 100644 --- a/src/media/media.service.spec.ts +++ b/src/media/media.service.spec.ts @@ -264,4 +264,40 @@ describe('MediaService', () => { }); }); }); + + describe('listUploadsByNote', () => { + describe('works', () => { + it('with one upload to note', async () => { + const mockMediaUploadEntry = { + id: 'testMediaUpload', + backendData: 'testBackendData', + note: { + id: '123', + } as Note, + } as MediaUpload; + jest + .spyOn(mediaRepo, 'find') + .mockResolvedValueOnce([mockMediaUploadEntry]); + const mediaList = await service.listUploadsByNote({ + id: '123', + } as Note); + expect(mediaList).toEqual([mockMediaUploadEntry]); + }); + + it('without uploads to note', async () => { + jest.spyOn(mediaRepo, 'find').mockResolvedValueOnce([]); + const mediaList = await service.listUploadsByNote({ + id: '123', + } as Note); + expect(mediaList).toEqual([]); + }); + it('with error (undefined as return value of find)', async () => { + jest.spyOn(mediaRepo, 'find').mockResolvedValueOnce(undefined); + const mediaList = await service.listUploadsByNote({ + id: '123', + } as Note); + expect(mediaList).toEqual([]); + }); + }); + }); }); diff --git a/src/media/media.service.ts b/src/media/media.service.ts index 90a72049b..8a71f2678 100644 --- a/src/media/media.service.ts +++ b/src/media/media.service.ts @@ -24,6 +24,7 @@ import { AzureBackend } from './backends/azure-backend'; import { ImgurBackend } from './backends/imgur-backend'; import { User } from '../users/user.entity'; import { MediaUploadDto } from './media-upload.dto'; +import { Note } from '../notes/note.entity'; @Injectable() export class MediaService { @@ -175,6 +176,23 @@ export class MediaService { return mediaUploads; } + /** + * @async + * List all uploads by a specific note + * @param {Note} note - the specific user + * @return {MediaUpload[]} arary of media uploads owned by the user + */ + async listUploadsByNote(note: Note): Promise { + const mediaUploads = await this.mediaUploadRepository.find({ + where: { note: note }, + relations: ['user', 'note'], + }); + if (mediaUploads === undefined) { + return []; + } + return mediaUploads; + } + private chooseBackendType(): BackendType { switch (this.mediaConfig.backend.use) { case 'filesystem':