mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 19:26:31 -05:00
Implement GetNotePipe
This pipe transforms a note ID or alias to a Note object by loading it from the database. It also performs error handling Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
3265c72515
commit
73be10e606
1 changed files with 43 additions and 0 deletions
43
src/notes/get-note.pipe.ts
Normal file
43
src/notes/get-note.pipe.ts
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
import {
|
||||||
|
ArgumentMetadata,
|
||||||
|
BadRequestException,
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
PipeTransform,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
import { ForbiddenIdError, NotInDBError } from '../errors/errors';
|
||||||
|
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||||
|
import { Note } from './note.entity';
|
||||||
|
import { NotesService } from './notes.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class GetNotePipe implements PipeTransform<string, Promise<Note>> {
|
||||||
|
constructor(
|
||||||
|
private readonly logger: ConsoleLoggerService,
|
||||||
|
private noteService: NotesService,
|
||||||
|
) {
|
||||||
|
this.logger.setContext(GetNotePipe.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
async transform(noteIdOrAlias: string, _: ArgumentMetadata): Promise<Note> {
|
||||||
|
let note: Note;
|
||||||
|
try {
|
||||||
|
note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof NotInDBError) {
|
||||||
|
throw new NotFoundException(e.message);
|
||||||
|
}
|
||||||
|
if (e instanceof ForbiddenIdError) {
|
||||||
|
throw new BadRequestException(e.message);
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue