From 05a62b31ee3da8ed46541880cc678fde12c62bf0 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Tue, 22 Sep 2020 21:09:14 +0200 Subject: [PATCH] RevisionService: Implement `getNoteRevisionMetadatas` Signed-off-by: David Mehren --- src/notes/notes.module.ts | 4 ++-- src/notes/notes.service.ts | 5 +++-- src/revisions/revisions.module.ts | 8 ++++++-- src/revisions/revisions.service.ts | 29 ++++++++++++++++++++--------- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/notes/notes.module.ts b/src/notes/notes.module.ts index a06575138..0d2470ba3 100644 --- a/src/notes/notes.module.ts +++ b/src/notes/notes.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { forwardRef, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { RevisionsModule } from '../revisions/revisions.module'; import { UsersModule } from '../users/users.module'; @@ -9,7 +9,7 @@ import { NotesService } from './notes.service'; @Module({ imports: [ TypeOrmModule.forFeature([Note, AuthorColor]), - RevisionsModule, + forwardRef(() => RevisionsModule), UsersModule, ], controllers: [], diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index c3b622cdf..c82ecf471 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, Logger } from '@nestjs/common'; +import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Revision } from '../revisions/revision.entity'; @@ -21,7 +21,8 @@ export class NotesService { constructor( @InjectRepository(Note) private noteRepository: Repository, @Inject(UsersService) private usersService: UsersService, - @Inject(RevisionsService) private revisionsService: RevisionsService, + @Inject(forwardRef(() => RevisionsService)) + private revisionsService: RevisionsService, ) {} getUserNotes(username: string): NoteMetadataDto[] { diff --git a/src/revisions/revisions.module.ts b/src/revisions/revisions.module.ts index 6b1bc365f..a959f703a 100644 --- a/src/revisions/revisions.module.ts +++ b/src/revisions/revisions.module.ts @@ -1,11 +1,15 @@ -import { Module } from '@nestjs/common'; +import { forwardRef, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { NotesModule } from '../notes/notes.module'; import { Authorship } from './authorship.entity'; import { Revision } from './revision.entity'; import { RevisionsService } from './revisions.service'; @Module({ - imports: [TypeOrmModule.forFeature([Revision, Authorship])], + imports: [ + TypeOrmModule.forFeature([Revision, Authorship]), + forwardRef(() => NotesModule), + ], providers: [RevisionsService], exports: [RevisionsService], }) diff --git a/src/revisions/revisions.service.ts b/src/revisions/revisions.service.ts index 167274fc4..26beaa01e 100644 --- a/src/revisions/revisions.service.ts +++ b/src/revisions/revisions.service.ts @@ -1,6 +1,7 @@ -import { Injectable, Logger } from '@nestjs/common'; +import { Inject, Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; +import { NotesService } from '../notes/notes.service'; import { RevisionMetadataDto } from './revision-metadata.dto'; import { RevisionDto } from './revision.dto'; import { Revision } from './revision.entity'; @@ -10,17 +11,19 @@ export class RevisionsService { constructor( @InjectRepository(Revision) private revisionRepository: Repository, + @Inject(NotesService) private notesService: NotesService, ) {} private readonly logger = new Logger(RevisionsService.name); - getNoteRevisionMetadatas(noteIdOrAlias: string): RevisionMetadataDto[] { - this.logger.warn('Using hardcoded data!'); - return [ - { - id: 42, - updatedAt: new Date(), - length: 42, + async getNoteRevisionMetadatas( + noteIdOrAlias: string, + ): Promise { + const note = await this.notesService.getNoteByIdOrAlias(noteIdOrAlias); + const revisions = await this.revisionRepository.find({ + where: { + note: note.id, }, - ]; + }); + return revisions.map(revision => this.toMetadataDto(revision)); } getNoteRevision(noteIdOrAlias: string, revisionId: number): RevisionDto { @@ -44,6 +47,14 @@ export class RevisionsService { }); } + toMetadataDto(revision: Revision): RevisionMetadataDto { + return { + id: revision.id, + length: revision.length, + createdAt: revision.createdAt, + }; + } + createRevision(content: string) { // TODO: Add previous revision // TODO: Calculate patch