mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-01-02 16:01:55 +00:00
NotesService: createNote()
now saves new notes to the database
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
39410ab9c8
commit
2ab90917bc
1 changed files with 54 additions and 24 deletions
|
@ -1,15 +1,24 @@
|
|||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Revision } from '../revisions/revision.entity';
|
||||
import { User } from '../users/user.entity';
|
||||
import { NoteMetadataDto } from './note-metadata.dto';
|
||||
import {
|
||||
NotePermissionsDto,
|
||||
NotePermissionsUpdateDto,
|
||||
} from './note-permissions.dto';
|
||||
import { NoteDto } from './note.dto';
|
||||
import { Note } from './note.entity';
|
||||
|
||||
@Injectable()
|
||||
export class NotesService {
|
||||
private readonly logger = new Logger(NotesService.name);
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Note) private noteRepository: Repository<Note>,
|
||||
) {}
|
||||
|
||||
getUserNotes(username: string): NoteMetadataDto[] {
|
||||
this.logger.warn('Using hardcoded data!');
|
||||
return [
|
||||
|
@ -43,38 +52,59 @@ export class NotesService {
|
|||
];
|
||||
}
|
||||
|
||||
createNote(noteContent: string, alias?: NoteMetadataDto['alias']): NoteDto {
|
||||
async createNote(
|
||||
noteContent: string,
|
||||
alias?: NoteMetadataDto['alias'],
|
||||
owner?: User,
|
||||
): Promise<NoteDto> {
|
||||
this.logger.warn('Using hardcoded data!');
|
||||
const newNote = Note.create();
|
||||
newNote.revisions = [Revision.create(noteContent, noteContent)];
|
||||
if (alias) {
|
||||
newNote.alias = alias;
|
||||
}
|
||||
if (owner) {
|
||||
newNote.owner = owner;
|
||||
}
|
||||
const savedNote = await this.noteRepository.save(newNote);
|
||||
return {
|
||||
content: noteContent,
|
||||
metdata: {
|
||||
alias: alias,
|
||||
createTime: new Date(),
|
||||
description: 'Very descriptive text.',
|
||||
editedBy: [],
|
||||
id: 'foobar-barfoo',
|
||||
permission: {
|
||||
owner: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
sharedToUsers: [],
|
||||
sharedToGroups: [],
|
||||
},
|
||||
tags: [],
|
||||
title: 'Title!',
|
||||
updateTime: new Date(),
|
||||
updateUser: {
|
||||
content: this.getCurrentContent(savedNote),
|
||||
metadata: this.getMetadata(savedNote),
|
||||
editedByAtPosition: [],
|
||||
};
|
||||
}
|
||||
|
||||
getCurrentContent(note: Note) {
|
||||
return note.revisions[note.revisions.length - 1].content;
|
||||
}
|
||||
|
||||
getMetadata(note: Note) {
|
||||
return {
|
||||
alias: note.alias,
|
||||
createTime: new Date(),
|
||||
description: 'Very descriptive text.',
|
||||
editedBy: [],
|
||||
id: note.id,
|
||||
permission: {
|
||||
owner: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
viewCount: 42,
|
||||
sharedToUsers: [],
|
||||
sharedToGroups: [],
|
||||
},
|
||||
editedByAtPosition: [],
|
||||
tags: [],
|
||||
title: 'Title!',
|
||||
updateTime: new Date(),
|
||||
updateUser: {
|
||||
displayName: 'foo',
|
||||
userName: 'fooUser',
|
||||
email: 'foo@example.com',
|
||||
photo: '',
|
||||
},
|
||||
viewCount: 42,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue