NotesController: Get text from request body when updating and deleting a note.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-09-22 20:06:56 +02:00
parent 99dccc0567
commit e43008c627
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -70,16 +70,29 @@ export class NotesController {
} }
@Delete(':noteIdOrAlias') @Delete(':noteIdOrAlias')
deleteNote(@Param('noteIdOrAlias') noteIdOrAlias: string) { async deleteNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
return this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias); this.logger.debug('Deleting note: ' + noteIdOrAlias);
await this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias);
this.logger.debug('Successfully deleted ' + noteIdOrAlias);
return;
} }
@Put(':noteIdOrAlias') @Put(':noteIdOrAlias')
updateNote( async updateNote(
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias') noteIdOrAlias: string,
@Body() noteContent: string, @Req() req: Request,
) { ) {
return this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, noteContent); // we have to check req.readable because of raw-body issue #57
// https://github.com/stream-utils/raw-body/issues/57
if (req.readable) {
let bodyText: string = await getRawBody(req, 'utf-8');
bodyText = bodyText.trim();
this.logger.debug('Got raw markdown:\n' + bodyText);
return this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, bodyText);
} else {
// TODO: Better error message
throw new BadRequestException('Invalid body');
}
} }
@Get(':noteIdOrAlias/content') @Get(':noteIdOrAlias/content')