Public API: Use GetNotePipe

This replaces repeated calls to `noteService.getNoteByIdOrAlias`
and associated error handling with the `GetNotePipe`
in the `Param` decorator.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-08-29 17:28:14 +02:00
parent 1e3c08b3df
commit 9da8d0efb0
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -40,6 +40,7 @@ import { HistoryService } from '../../../history/history.service';
import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { MediaUploadDto } from '../../../media/media-upload.dto'; import { MediaUploadDto } from '../../../media/media-upload.dto';
import { MediaService } from '../../../media/media.service'; import { MediaService } from '../../../media/media.service';
import { GetNotePipe } from '../../../notes/get-note.pipe';
import { NoteMetadataDto } from '../../../notes/note-metadata.dto'; import { NoteMetadataDto } from '../../../notes/note-metadata.dto';
import { import {
NotePermissionsDto, NotePermissionsDto,
@ -106,20 +107,8 @@ export class NotesController {
@FullApi @FullApi
async getNote( async getNote(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<NoteDto> { ): Promise<NoteDto> {
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;
}
if (!this.permissionsService.mayRead(user, note)) { if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
@ -167,11 +156,9 @@ export class NotesController {
@FullApi @FullApi
async deleteNote( async deleteNote(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
@Body() noteMediaDeletionDto: NoteMediaDeletionDto, @Body() noteMediaDeletionDto: NoteMediaDeletionDto,
): Promise<void> { ): Promise<void> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.isOwner(user, note)) { if (!this.permissionsService.isOwner(user, note)) {
throw new UnauthorizedException('Deleting note denied!'); throw new UnauthorizedException('Deleting note denied!');
} }
@ -183,19 +170,10 @@ export class NotesController {
await this.mediaService.removeNoteFromMediaUpload(mediaUpload); await this.mediaService.removeNoteFromMediaUpload(mediaUpload);
} }
} }
this.logger.debug('Deleting note: ' + noteIdOrAlias, 'deleteNote'); this.logger.debug('Deleting note: ' + note.id, 'deleteNote');
await this.noteService.deleteNote(note); await this.noteService.deleteNote(note);
this.logger.debug('Successfully deleted ' + noteIdOrAlias, 'deleteNote'); this.logger.debug('Successfully deleted ' + note.id, 'deleteNote');
return; return;
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
}
} }
@UseGuards(TokenAuthGuard) @UseGuards(TokenAuthGuard)
@ -207,11 +185,9 @@ export class NotesController {
@FullApi @FullApi
async updateNote( async updateNote(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
@MarkdownBody() text: string, @MarkdownBody() text: string,
): Promise<NoteDto> { ): Promise<NoteDto> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.mayWrite(user, note)) { if (!this.permissionsService.mayWrite(user, note)) {
throw new UnauthorizedException('Updating note denied!'); throw new UnauthorizedException('Updating note denied!');
} }
@ -219,15 +195,6 @@ export class NotesController {
return await this.noteService.toNoteDto( return await this.noteService.toNoteDto(
await this.noteService.updateNote(note, text), await this.noteService.updateNote(note, text),
); );
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
}
} }
@UseGuards(TokenAuthGuard) @UseGuards(TokenAuthGuard)
@ -240,23 +207,12 @@ export class NotesController {
@Header('content-type', 'text/markdown') @Header('content-type', 'text/markdown')
async getNoteContent( async getNoteContent(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<string> { ): Promise<string> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.mayRead(user, note)) { if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
return await this.noteService.getNoteContent(note); return await this.noteService.getNoteContent(note);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
}
} }
@UseGuards(TokenAuthGuard) @UseGuards(TokenAuthGuard)
@ -268,24 +224,17 @@ export class NotesController {
@FullApi @FullApi
async getNoteMetadata( async getNoteMetadata(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<NoteMetadataDto> { ): Promise<NoteMetadataDto> {
try { try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.mayRead(user, note)) { if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
return await this.noteService.toNoteMetadataDto(note); return await this.noteService.toNoteMetadataDto(note);
} catch (e) { } catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof PermissionsUpdateInconsistentError) { if (e instanceof PermissionsUpdateInconsistentError) {
throw new BadRequestException(e.message); throw new BadRequestException(e.message);
} }
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e; throw e;
} }
} }
@ -299,26 +248,15 @@ export class NotesController {
@FullApi @FullApi
async updateNotePermissions( async updateNotePermissions(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
@Body() updateDto: NotePermissionsUpdateDto, @Body() updateDto: NotePermissionsUpdateDto,
): Promise<NotePermissionsDto> { ): Promise<NotePermissionsDto> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.isOwner(user, note)) { if (!this.permissionsService.isOwner(user, note)) {
throw new UnauthorizedException('Updating note denied!'); throw new UnauthorizedException('Updating note denied!');
} }
return this.noteService.toNotePermissionsDto( return this.noteService.toNotePermissionsDto(
await this.noteService.updateNotePermissions(note, updateDto), await this.noteService.updateNotePermissions(note, updateDto),
); );
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
}
} }
@UseGuards(TokenAuthGuard) @UseGuards(TokenAuthGuard)
@ -331,10 +269,8 @@ export class NotesController {
@FullApi @FullApi
async getNoteRevisions( async getNoteRevisions(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<RevisionMetadataDto[]> { ): Promise<RevisionMetadataDto[]> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.mayRead(user, note)) { if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
@ -344,15 +280,6 @@ export class NotesController {
this.revisionsService.toRevisionMetadataDto(revision), this.revisionsService.toRevisionMetadataDto(revision),
), ),
); );
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
}
} }
@UseGuards(TokenAuthGuard) @UseGuards(TokenAuthGuard)
@ -364,14 +291,13 @@ export class NotesController {
@FullApi @FullApi
async getNoteRevision( async getNoteRevision(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
@Param('revisionId') revisionId: number, @Param('revisionId') revisionId: number,
): Promise<RevisionDto> { ): Promise<RevisionDto> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.mayRead(user, note)) { if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
try {
return this.revisionsService.toRevisionDto( return this.revisionsService.toRevisionDto(
await this.revisionsService.getRevision(note, revisionId), await this.revisionsService.getRevision(note, revisionId),
); );
@ -379,9 +305,6 @@ export class NotesController {
if (e instanceof NotInDBError) { if (e instanceof NotInDBError) {
throw new NotFoundException(e.message); throw new NotFoundException(e.message);
} }
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e; throw e;
} }
} }
@ -396,20 +319,12 @@ export class NotesController {
@ApiUnauthorizedResponse({ description: unauthorizedDescription }) @ApiUnauthorizedResponse({ description: unauthorizedDescription })
async getNotesMedia( async getNotesMedia(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<MediaUploadDto[]> { ): Promise<MediaUploadDto[]> {
try {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
if (!this.permissionsService.mayRead(user, note)) { if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!'); throw new UnauthorizedException('Reading note denied!');
} }
const media = await this.mediaService.listUploadsByNote(note); const media = await this.mediaService.listUploadsByNote(note);
return media.map((media) => this.mediaService.toMediaUploadDto(media)); return media.map((media) => this.mediaService.toMediaUploadDto(media));
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
}
} }
} }