NotesService: Implement updateNoteMetadata

Signed-off-by: David Mehren <git@herrmehren.de>
Co-authored-by: Yannick Bungers <git@innay.de>
This commit is contained in:
David Mehren 2020-10-03 15:52:49 +02:00
parent 6541cfda4e
commit 08fd070bcb
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 32 additions and 2 deletions

View file

@ -34,3 +34,13 @@ export class NoteMetadataDto {
@ValidateNested() @ValidateNested()
permissions: NotePermissionsDto; permissions: NotePermissionsDto;
} }
export class NoteMetadataUpdateDto {
@IsString()
title: string;
@IsString()
description: string;
@IsArray()
@IsString({ each: true })
tags: string[];
}

View file

@ -7,20 +7,21 @@ import { Revision } from '../revisions/revision.entity';
import { RevisionsService } from '../revisions/revisions.service'; 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, NoteMetadataUpdateDto } 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'; import { Note } from './note.entity';
import { NoteUtils } from './note.utils'; import { Tag } from './tag.entity';
@Injectable() @Injectable()
export class NotesService { export class NotesService {
constructor( constructor(
private readonly logger: ConsoleLoggerService, private readonly logger: ConsoleLoggerService,
@InjectRepository(Note) private noteRepository: Repository<Note>, @InjectRepository(Note) private noteRepository: Repository<Note>,
@InjectRepository(Tag) private tagRepository: Repository<Tag>,
@Inject(UsersService) private usersService: UsersService, @Inject(UsersService) private usersService: UsersService,
@Inject(forwardRef(() => RevisionsService)) @Inject(forwardRef(() => RevisionsService))
private revisionsService: RevisionsService, private revisionsService: RevisionsService,
@ -219,4 +220,23 @@ export class NotesService {
editedByAtPosition: [], 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);
}
} }