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 <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-09-22 21:44:53 +02:00
parent 446d6dec06
commit bc1c8448df
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 13 additions and 9 deletions

View file

@ -39,7 +39,7 @@ export class NotesController {
let bodyText: string = await getRawBody(req, 'utf-8'); let bodyText: string = await getRawBody(req, 'utf-8');
bodyText = bodyText.trim(); bodyText = bodyText.trim();
this.logger.debug('Got raw markdown:\n' + bodyText); this.logger.debug('Got raw markdown:\n' + bodyText);
return this.noteService.createNote(bodyText); return this.noteService.createNoteDto(bodyText);
} else { } else {
// TODO: Better error message // TODO: Better error message
throw new BadRequestException('Invalid body'); throw new BadRequestException('Invalid body');
@ -62,7 +62,7 @@ export class NotesController {
let bodyText: string = await getRawBody(req, 'utf-8'); let bodyText: string = await getRawBody(req, 'utf-8');
bodyText = bodyText.trim(); bodyText = bodyText.trim();
this.logger.debug('Got raw markdown:\n' + bodyText); this.logger.debug('Got raw markdown:\n' + bodyText);
return this.noteService.createNote(bodyText, noteAlias); return this.noteService.createNoteDto(bodyText, noteAlias);
} else { } else {
// TODO: Better error message // TODO: Better error message
throw new BadRequestException('Invalid body'); throw new BadRequestException('Invalid body');

View file

@ -58,11 +58,20 @@ export class NotesService {
]; ];
} }
async createNote( async createNoteDto(
noteContent: string, noteContent: string,
alias?: NoteMetadataDto['alias'], alias?: NoteMetadataDto['alias'],
owner?: User, owner?: User,
): Promise<NoteDto> { ): Promise<NoteDto> {
const note = await this.createNote(noteContent, alias, owner);
return this.toNoteDto(note);
}
async createNote(
noteContent: string,
alias?: NoteMetadataDto['alias'],
owner?: User,
): Promise<Note> {
const newNote = Note.create(); const newNote = Note.create();
newNote.revisions = Promise.resolve([ newNote.revisions = Promise.resolve([
Revision.create(noteContent, noteContent), Revision.create(noteContent, noteContent),
@ -73,12 +82,7 @@ export class NotesService {
if (owner) { if (owner) {
newNote.owner = owner; newNote.owner = owner;
} }
const savedNote = await this.noteRepository.save(newNote); return this.noteRepository.save(newNote);
return {
content: await this.getCurrentContent(savedNote),
metadata: await this.getMetadata(savedNote),
editedByAtPosition: [],
};
} }
async getCurrentContent(note: Note) { async getCurrentContent(note: Note) {