feat(revision): include edits in dto

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-02-13 19:07:52 +01:00
parent 50c8f39c0c
commit 7050c9f13b
7 changed files with 48 additions and 6 deletions

View file

@ -172,7 +172,7 @@ export class NotesController {
@RequestNote() note: Note, @RequestNote() note: Note,
@Param('revisionId') revisionId: number, @Param('revisionId') revisionId: number,
): Promise<RevisionDto> { ): Promise<RevisionDto> {
return this.revisionsService.toRevisionDto( return await this.revisionsService.toRevisionDto(
await this.revisionsService.getRevision(note, revisionId), await this.revisionsService.getRevision(note, revisionId),
); );
} }

View file

@ -264,7 +264,7 @@ export class NotesController {
@RequestNote() note: Note, @RequestNote() note: Note,
@Param('revisionId') revisionId: number, @Param('revisionId') revisionId: number,
): Promise<RevisionDto> { ): Promise<RevisionDto> {
return this.revisionsService.toRevisionDto( return await this.revisionsService.toRevisionDto(
await this.revisionsService.getRevision(note, revisionId), await this.revisionsService.getRevision(note, revisionId),
); );
} }

View file

@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable } from '@nestjs/common';
import { EditDto } from './edit.dto';
import { Edit } from './edit.entity';
@Injectable()
export class EditService {
async toEditDto(edit: Edit): Promise<EditDto> {
const authorUser = await (await edit.author).user;
return {
username: authorUser ? authorUser.username : null,
startPos: edit.startPos,
endPos: edit.endPos,
createdAt: edit.createdAt,
updatedAt: edit.updatedAt,
};
}
}

View file

@ -5,9 +5,10 @@
*/ */
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer'; import { Type } from 'class-transformer';
import { IsDate, IsNumber, IsString } from 'class-validator'; import { IsDate, IsNumber, IsString, ValidateNested } from 'class-validator';
import { BaseDto } from '../utils/base.dto.'; import { BaseDto } from '../utils/base.dto.';
import { EditDto } from './edit.dto';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
export class RevisionDto extends BaseDto { export class RevisionDto extends BaseDto {
@ -42,4 +43,11 @@ export class RevisionDto extends BaseDto {
@Type(() => Date) @Type(() => Date)
@ApiProperty() @ApiProperty()
createdAt: Date; createdAt: Date;
/**
* All edit objects which are used in the revision.
*/
@ValidateNested()
@ApiProperty()
edits: EditDto[];
} }

View file

@ -11,6 +11,7 @@ import { AuthorsModule } from '../authors/authors.module';
import { LoggerModule } from '../logger/logger.module'; import { LoggerModule } from '../logger/logger.module';
import { NotesModule } from '../notes/notes.module'; import { NotesModule } from '../notes/notes.module';
import { Edit } from './edit.entity'; import { Edit } from './edit.entity';
import { EditService } from './edit.service';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
import { RevisionsService } from './revisions.service'; import { RevisionsService } from './revisions.service';
@ -22,7 +23,7 @@ import { RevisionsService } from './revisions.service';
ConfigModule, ConfigModule,
AuthorsModule, AuthorsModule,
], ],
providers: [RevisionsService], providers: [RevisionsService, EditService],
exports: [RevisionsService], exports: [RevisionsService, EditService],
}) })
export class RevisionsModule {} export class RevisionsModule {}

View file

@ -25,6 +25,7 @@ import { NoteUserPermission } from '../permissions/note-user-permission.entity';
import { Session } from '../users/session.entity'; import { Session } from '../users/session.entity';
import { User } from '../users/user.entity'; import { User } from '../users/user.entity';
import { Edit } from './edit.entity'; import { Edit } from './edit.entity';
import { EditService } from './edit.service';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
import { RevisionsService } from './revisions.service'; import { RevisionsService } from './revisions.service';
@ -36,6 +37,7 @@ describe('RevisionsService', () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [ providers: [
RevisionsService, RevisionsService,
EditService,
{ {
provide: getRepositoryToken(Revision), provide: getRepositoryToken(Revision),
useClass: Repository, useClass: Repository,

View file

@ -11,6 +11,7 @@ import { NotInDBError } from '../errors/errors';
import { ConsoleLoggerService } from '../logger/console-logger.service'; import { ConsoleLoggerService } from '../logger/console-logger.service';
import { Note } from '../notes/note.entity'; import { Note } from '../notes/note.entity';
import { NotesService } from '../notes/notes.service'; import { NotesService } from '../notes/notes.service';
import { EditService } from './edit.service';
import { RevisionMetadataDto } from './revision-metadata.dto'; import { RevisionMetadataDto } from './revision-metadata.dto';
import { RevisionDto } from './revision.dto'; import { RevisionDto } from './revision.dto';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
@ -22,6 +23,7 @@ export class RevisionsService {
@InjectRepository(Revision) @InjectRepository(Revision)
private revisionRepository: Repository<Revision>, private revisionRepository: Repository<Revision>,
@Inject(forwardRef(() => NotesService)) private notesService: NotesService, @Inject(forwardRef(() => NotesService)) private notesService: NotesService,
private editService: EditService,
) { ) {
this.logger.setContext(RevisionsService.name); this.logger.setContext(RevisionsService.name);
} }
@ -109,12 +111,17 @@ export class RevisionsService {
}; };
} }
toRevisionDto(revision: Revision): RevisionDto { async toRevisionDto(revision: Revision): Promise<RevisionDto> {
return { return {
id: revision.id, id: revision.id,
content: revision.content, content: revision.content,
createdAt: revision.createdAt, createdAt: revision.createdAt,
patch: revision.patch, patch: revision.patch,
edits: await Promise.all(
(
await revision.edits
).map(async (edit) => await this.editService.toEditDto(edit)),
),
}; };
} }