diff --git a/src/notes/note-metadata.dto.ts b/src/notes/note-metadata.dto.ts index 2b26a37bb..fea43be93 100644 --- a/src/notes/note-metadata.dto.ts +++ b/src/notes/note-metadata.dto.ts @@ -34,3 +34,13 @@ export class NoteMetadataDto { @ValidateNested() permissions: NotePermissionsDto; } + +export class NoteMetadataUpdateDto { + @IsString() + title: string; + @IsString() + description: string; + @IsArray() + @IsString({ each: true }) + tags: string[]; +} diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index 807c16d36..092ad4339 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -7,20 +7,21 @@ import { Revision } from '../revisions/revision.entity'; import { RevisionsService } from '../revisions/revisions.service'; import { User } from '../users/user.entity'; import { UsersService } from '../users/users.service'; -import { NoteMetadataDto } from './note-metadata.dto'; +import { NoteMetadataDto, NoteMetadataUpdateDto } from './note-metadata.dto'; import { NotePermissionsDto, NotePermissionsUpdateDto, } from './note-permissions.dto'; import { NoteDto } from './note.dto'; import { Note } from './note.entity'; -import { NoteUtils } from './note.utils'; +import { Tag } from './tag.entity'; @Injectable() export class NotesService { constructor( private readonly logger: ConsoleLoggerService, @InjectRepository(Note) private noteRepository: Repository, + @InjectRepository(Tag) private tagRepository: Repository, @Inject(UsersService) private usersService: UsersService, @Inject(forwardRef(() => RevisionsService)) private revisionsService: RevisionsService, @@ -219,4 +220,23 @@ export class NotesService { editedByAtPosition: [], }; } + + async updateNoteMetadata( + noteIdOrAlias: string, + updateDto: NoteMetadataUpdateDto, + ) { + const note = await this.getNoteByIdOrAlias(noteIdOrAlias); + note.title = updateDto.title; + note.description = updateDto.description; + note.tags = await Promise.all( + updateDto.tags.map(async tag => { + let dbTag = await this.tagRepository.findOne({ where: { name: tag } }); + if (!dbTag) { + dbTag = await this.tagRepository.create({ name: tag }); + } + return dbTag; + }), + ); + await this.noteRepository.save(note); + } }