mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-17 16:44:49 -04:00
NotesService: Find note by ID or alias in database
This commit also introduces the `getNoteDtoByIdOrAlias` method, that converts a `Note` entity to a `NoteDto` Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
21f4cda27c
commit
5d1cc6c339
2 changed files with 31 additions and 34 deletions
|
@ -48,7 +48,7 @@ export class NotesController {
|
||||||
|
|
||||||
@Get(':noteIdOrAlias')
|
@Get(':noteIdOrAlias')
|
||||||
getNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
getNote(@Param('noteIdOrAlias') noteIdOrAlias: string) {
|
||||||
return this.noteService.getNoteByIdOrAlias(noteIdOrAlias);
|
return this.noteService.getNoteDtoByIdOrAlias(noteIdOrAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post(':noteAlias')
|
@Post(':noteAlias')
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Inject, Injectable, Logger } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { Revision } from '../revisions/revision.entity';
|
import { Revision } from '../revisions/revision.entity';
|
||||||
|
import { RevisionsService } from '../revisions/revisions.service';
|
||||||
import { User } from '../users/user.entity';
|
import { User } from '../users/user.entity';
|
||||||
import { UsersService } from '../users/users.service';
|
import { UsersService } from '../users/users.service';
|
||||||
import { NoteMetadataDto } from './note-metadata.dto';
|
import { NoteMetadataDto } from './note-metadata.dto';
|
||||||
|
@ -20,6 +21,7 @@ export class NotesService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Note) private noteRepository: Repository<Note>,
|
@InjectRepository(Note) private noteRepository: Repository<Note>,
|
||||||
@Inject(UsersService) private usersService: UsersService,
|
@Inject(UsersService) private usersService: UsersService,
|
||||||
|
@Inject(RevisionsService) private revisionsService: RevisionsService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
getUserNotes(username: string): NoteMetadataDto[] {
|
getUserNotes(username: string): NoteMetadataDto[] {
|
||||||
|
@ -121,39 +123,26 @@ export class NotesService {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getNoteByIdOrAlias(noteIdOrAlias: string) {
|
async getNoteByIdOrAlias(noteIdOrAlias: string): Promise<Note> {
|
||||||
this.logger.warn('Using hardcoded data!');
|
const note = await this.noteRepository.findOne({
|
||||||
return {
|
where: [{ id: noteIdOrAlias }, { alias: noteIdOrAlias }],
|
||||||
content: 'noteContent',
|
relations: [
|
||||||
metadata: {
|
'authorColors',
|
||||||
alias: null,
|
'owner',
|
||||||
createTime: new Date(),
|
'groupPermissions',
|
||||||
description: 'Very descriptive text.',
|
'userPermissions',
|
||||||
editedBy: [],
|
],
|
||||||
id: noteIdOrAlias,
|
});
|
||||||
permission: {
|
if (note === undefined) {
|
||||||
owner: {
|
//TODO: Improve error handling
|
||||||
displayName: 'foo',
|
throw new Error('Note not found');
|
||||||
userName: 'fooUser',
|
}
|
||||||
email: 'foo@example.com',
|
return note;
|
||||||
photo: '',
|
}
|
||||||
},
|
|
||||||
sharedToUsers: [],
|
async getNoteDtoByIdOrAlias(noteIdOrAlias: string): Promise<NoteDto> {
|
||||||
sharedToGroups: [],
|
const note = await this.getNoteByIdOrAlias(noteIdOrAlias);
|
||||||
},
|
return this.toNoteDto(note);
|
||||||
tags: [],
|
|
||||||
title: 'Title!',
|
|
||||||
updateTime: new Date(),
|
|
||||||
updateUser: {
|
|
||||||
displayName: 'foo',
|
|
||||||
userName: 'fooUser',
|
|
||||||
email: 'foo@example.com',
|
|
||||||
photo: '',
|
|
||||||
},
|
|
||||||
viewCount: 42,
|
|
||||||
},
|
|
||||||
editedByAtPosition: [],
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteNoteByIdOrAlias(noteIdOrAlias: string) {
|
deleteNoteByIdOrAlias(noteIdOrAlias: string) {
|
||||||
|
@ -248,4 +237,12 @@ export class NotesService {
|
||||||
this.logger.warn('Using hardcoded data!');
|
this.logger.warn('Using hardcoded data!');
|
||||||
return '# Markdown';
|
return '# Markdown';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async toNoteDto(note: Note): Promise<NoteDto> {
|
||||||
|
return {
|
||||||
|
content: await this.getCurrentContent(note),
|
||||||
|
metadata: await this.getMetadata(note),
|
||||||
|
editedByAtPosition: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue