mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-15 15:44:45 -04:00
NotesService: createNote()
now saves new notes to the database
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
2261b81139
commit
32a6396a50
1 changed files with 54 additions and 24 deletions
|
@ -1,15 +1,24 @@
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
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 { NoteMetadataDto } from './note-metadata.dto';
|
||||||
import {
|
import {
|
||||||
NotePermissionsDto,
|
NotePermissionsDto,
|
||||||
NotePermissionsUpdateDto,
|
NotePermissionsUpdateDto,
|
||||||
} from './note-permissions.dto';
|
} from './note-permissions.dto';
|
||||||
import { NoteDto } from './note.dto';
|
import { NoteDto } from './note.dto';
|
||||||
|
import { Note } from './note.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NotesService {
|
export class NotesService {
|
||||||
private readonly logger = new Logger(NotesService.name);
|
private readonly logger = new Logger(NotesService.name);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(Note) private noteRepository: Repository<Note>,
|
||||||
|
) {}
|
||||||
|
|
||||||
getUserNotes(username: string): NoteMetadataDto[] {
|
getUserNotes(username: string): NoteMetadataDto[] {
|
||||||
this.logger.warn('Using hardcoded data!');
|
this.logger.warn('Using hardcoded data!');
|
||||||
return [
|
return [
|
||||||
|
@ -43,16 +52,39 @@ 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!');
|
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 {
|
return {
|
||||||
content: noteContent,
|
content: this.getCurrentContent(savedNote),
|
||||||
metdata: {
|
metadata: this.getMetadata(savedNote),
|
||||||
alias: alias,
|
editedByAtPosition: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
getCurrentContent(note: Note) {
|
||||||
|
return note.revisions[note.revisions.length - 1].content;
|
||||||
|
}
|
||||||
|
|
||||||
|
getMetadata(note: Note) {
|
||||||
|
return {
|
||||||
|
alias: note.alias,
|
||||||
createTime: new Date(),
|
createTime: new Date(),
|
||||||
description: 'Very descriptive text.',
|
description: 'Very descriptive text.',
|
||||||
editedBy: [],
|
editedBy: [],
|
||||||
id: 'foobar-barfoo',
|
id: note.id,
|
||||||
permission: {
|
permission: {
|
||||||
owner: {
|
owner: {
|
||||||
displayName: 'foo',
|
displayName: 'foo',
|
||||||
|
@ -73,8 +105,6 @@ export class NotesService {
|
||||||
photo: '',
|
photo: '',
|
||||||
},
|
},
|
||||||
viewCount: 42,
|
viewCount: 42,
|
||||||
},
|
|
||||||
editedByAtPosition: [],
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue