MediaService: Add listUploadsByNote method

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-03-19 16:47:52 +01:00 committed by David Mehren
parent 53f5093630
commit 890de2dea1
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 54 additions and 0 deletions

View file

@ -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([]);
});
});
});
}); });

View file

@ -24,6 +24,7 @@ import { AzureBackend } from './backends/azure-backend';
import { ImgurBackend } from './backends/imgur-backend'; import { ImgurBackend } from './backends/imgur-backend';
import { User } from '../users/user.entity'; import { User } from '../users/user.entity';
import { MediaUploadDto } from './media-upload.dto'; import { MediaUploadDto } from './media-upload.dto';
import { Note } from '../notes/note.entity';
@Injectable() @Injectable()
export class MediaService { export class MediaService {
@ -175,6 +176,23 @@ export class MediaService {
return mediaUploads; 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<MediaUpload[]> {
const mediaUploads = await this.mediaUploadRepository.find({
where: { note: note },
relations: ['user', 'note'],
});
if (mediaUploads === undefined) {
return [];
}
return mediaUploads;
}
private chooseBackendType(): BackendType { private chooseBackendType(): BackendType {
switch (this.mediaConfig.backend.use) { switch (this.mediaConfig.backend.use) {
case 'filesystem': case 'filesystem':