From 05f25b92aab39b629e410b003bd4e6ae1d23a430 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Tue, 22 Sep 2020 20:06:56 +0200 Subject: [PATCH] NotesController: Get text from request body when updating and deleting a note. Signed-off-by: David Mehren --- src/api/public/notes/notes.controller.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/api/public/notes/notes.controller.ts b/src/api/public/notes/notes.controller.ts index 800e2e734..7a59d4a0f 100644 --- a/src/api/public/notes/notes.controller.ts +++ b/src/api/public/notes/notes.controller.ts @@ -70,16 +70,29 @@ export class NotesController { } @Delete(':noteIdOrAlias') - deleteNote(@Param('noteIdOrAlias') noteIdOrAlias: string) { - return this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias); + async deleteNote(@Param('noteIdOrAlias') noteIdOrAlias: string) { + this.logger.debug('Deleting note: ' + noteIdOrAlias); + await this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias); + this.logger.debug('Successfully deleted ' + noteIdOrAlias); + return; } @Put(':noteIdOrAlias') - updateNote( + async updateNote( @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')