diff --git a/src/api/public/notes/notes.controller.ts b/src/api/public/notes/notes.controller.ts index 8405b1007..1df997ab4 100644 --- a/src/api/public/notes/notes.controller.ts +++ b/src/api/public/notes/notes.controller.ts @@ -48,7 +48,7 @@ export class NotesController { @Get(':noteIdOrAlias') getNote(@Param('noteIdOrAlias') noteIdOrAlias: string) { - return this.noteService.getNoteByIdOrAlias(noteIdOrAlias); + return this.noteService.getNoteDtoByIdOrAlias(noteIdOrAlias); } @Post(':noteAlias') diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index f3d08f836..11d4f1394 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -2,6 +2,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Revision } from '../revisions/revision.entity'; +import { RevisionsService } from '../revisions/revisions.service'; import { User } from '../users/user.entity'; import { UsersService } from '../users/users.service'; import { NoteMetadataDto } from './note-metadata.dto'; @@ -20,6 +21,7 @@ export class NotesService { constructor( @InjectRepository(Note) private noteRepository: Repository, @Inject(UsersService) private usersService: UsersService, + @Inject(RevisionsService) private revisionsService: RevisionsService, ) {} getUserNotes(username: string): NoteMetadataDto[] { @@ -121,39 +123,26 @@ export class NotesService { }; } - getNoteByIdOrAlias(noteIdOrAlias: string) { - this.logger.warn('Using hardcoded data!'); - return { - content: 'noteContent', - metadata: { - alias: null, - createTime: new Date(), - description: 'Very descriptive text.', - editedBy: [], - id: noteIdOrAlias, - permission: { - owner: { - displayName: 'foo', - userName: 'fooUser', - email: 'foo@example.com', - photo: '', - }, - sharedToUsers: [], - sharedToGroups: [], - }, - tags: [], - title: 'Title!', - updateTime: new Date(), - updateUser: { - displayName: 'foo', - userName: 'fooUser', - email: 'foo@example.com', - photo: '', - }, - viewCount: 42, - }, - editedByAtPosition: [], - }; + async getNoteByIdOrAlias(noteIdOrAlias: string): Promise { + const note = await this.noteRepository.findOne({ + where: [{ id: noteIdOrAlias }, { alias: noteIdOrAlias }], + relations: [ + 'authorColors', + 'owner', + 'groupPermissions', + 'userPermissions', + ], + }); + if (note === undefined) { + //TODO: Improve error handling + throw new Error('Note not found'); + } + return note; + } + + async getNoteDtoByIdOrAlias(noteIdOrAlias: string): Promise { + const note = await this.getNoteByIdOrAlias(noteIdOrAlias); + return this.toNoteDto(note); } deleteNoteByIdOrAlias(noteIdOrAlias: string) { @@ -248,4 +237,12 @@ export class NotesService { this.logger.warn('Using hardcoded data!'); return '# Markdown'; } + + async toNoteDto(note: Note): Promise { + return { + content: await this.getCurrentContent(note), + metadata: await this.getMetadata(note), + editedByAtPosition: [], + }; + } }