RevisionsService: Get note revision from database

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-09-22 21:42:17 +02:00
parent a3ba5bccf7
commit 881263f2a4
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 27 additions and 9 deletions

View file

@ -1,4 +1,4 @@
import { IsNumber, IsString } from 'class-validator';
import { IsDate, IsNumber, IsString } from 'class-validator';
import { Revision } from './revision.entity';
export class RevisionDto {
@ -8,4 +8,6 @@ export class RevisionDto {
content: string;
@IsString()
patch: string;
@IsDate()
createdAt: Date;
}

View file

@ -8,12 +8,14 @@ import { Revision } from './revision.entity';
@Injectable()
export class RevisionsService {
private readonly logger = new Logger(RevisionsService.name);
constructor(
@InjectRepository(Revision)
private revisionRepository: Repository<Revision>,
@Inject(NotesService) private notesService: NotesService,
) {}
private readonly logger = new Logger(RevisionsService.name);
async getNoteRevisionMetadatas(
noteIdOrAlias: string,
): Promise<RevisionMetadataDto[]> {
@ -26,13 +28,18 @@ export class RevisionsService {
return revisions.map(revision => this.toMetadataDto(revision));
}
getNoteRevision(noteIdOrAlias: string, revisionId: number): RevisionDto {
this.logger.warn('Using hardcoded data!');
return {
id: revisionId,
content: 'Foobar',
patch: 'barfoo',
};
async getNoteRevision(
noteIdOrAlias: string,
revisionId: number,
): Promise<RevisionDto> {
const note = await this.notesService.getNoteByIdOrAlias(noteIdOrAlias);
const revision = await this.revisionRepository.findOne({
where: {
id: revisionId,
note: note,
},
});
return this.toDto(revision);
}
getLatestRevision(noteId: string): Promise<Revision> {
@ -55,6 +62,15 @@ export class RevisionsService {
};
}
toDto(revision: Revision): RevisionDto {
return {
id: revision.id,
content: revision.content,
createdAt: revision.createdAt,
patch: revision.patch,
};
}
createRevision(content: string) {
// TODO: Add previous revision
// TODO: Calculate patch