From 8b9a45b738e08c411e3422914c0c5d30aa5eb11c Mon Sep 17 00:00:00 2001 From: David Mehren Date: Tue, 22 Sep 2020 21:44:53 +0200 Subject: [PATCH] NotesService: Let `createNote` create an actual `Note` and introduce `createNoteDto` to create & convert in one step. It might be handy to have access to the original `Note` after creating one, so the creation and conversion to a `NoteDto` is now split. Signed-off-by: David Mehren --- src/api/public/notes/notes.controller.ts | 4 ++-- src/notes/notes.service.ts | 18 +++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/api/public/notes/notes.controller.ts b/src/api/public/notes/notes.controller.ts index 00c6c4ee9..e0f15d551 100644 --- a/src/api/public/notes/notes.controller.ts +++ b/src/api/public/notes/notes.controller.ts @@ -39,7 +39,7 @@ export class NotesController { let bodyText: string = await getRawBody(req, 'utf-8'); bodyText = bodyText.trim(); this.logger.debug('Got raw markdown:\n' + bodyText); - return this.noteService.createNote(bodyText); + return this.noteService.createNoteDto(bodyText); } else { // TODO: Better error message throw new BadRequestException('Invalid body'); @@ -62,7 +62,7 @@ export class NotesController { let bodyText: string = await getRawBody(req, 'utf-8'); bodyText = bodyText.trim(); this.logger.debug('Got raw markdown:\n' + bodyText); - return this.noteService.createNote(bodyText, noteAlias); + return this.noteService.createNoteDto(bodyText, noteAlias); } else { // TODO: Better error message throw new BadRequestException('Invalid body'); diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index c82ecf471..ec76150ec 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -58,11 +58,20 @@ export class NotesService { ]; } - async createNote( + async createNoteDto( noteContent: string, alias?: NoteMetadataDto['alias'], owner?: User, ): Promise { + const note = await this.createNote(noteContent, alias, owner); + return this.toNoteDto(note); + } + + async createNote( + noteContent: string, + alias?: NoteMetadataDto['alias'], + owner?: User, + ): Promise { const newNote = Note.create(); newNote.revisions = Promise.resolve([ Revision.create(noteContent, noteContent), @@ -73,12 +82,7 @@ export class NotesService { if (owner) { newNote.owner = owner; } - const savedNote = await this.noteRepository.save(newNote); - return { - content: await this.getCurrentContent(savedNote), - metadata: await this.getMetadata(savedNote), - editedByAtPosition: [], - }; + return this.noteRepository.save(newNote); } async getCurrentContent(note: Note) {