From d494af7d8695785284460f2aa05e7d4b57a8af5a Mon Sep 17 00:00:00 2001 From: Seungil Kim Date: Tue, 12 Sep 2023 21:02:06 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20=EC=BB=AC=EB=A0=89=EC=85=98?= =?UTF-8?q?=20update=20field=20optional=ED=95=98=EA=B2=8C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # --- src/collection/collection.service.ts | 54 ++++++++++++------- .../dto/request/update-collection.dto.ts | 9 ++-- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/collection/collection.service.ts b/src/collection/collection.service.ts index ed18c43..a41f457 100644 --- a/src/collection/collection.service.ts +++ b/src/collection/collection.service.ts @@ -136,7 +136,7 @@ export class CollectionService { throw new BadRequestException('Collection does not exist'); } - if (collection.user.uid !== uid) { + if (collection.user.id !== user.id) { throw new UnauthorizedException('Unauthorized'); } @@ -156,24 +156,42 @@ export class CollectionService { }, }); - const updatedCollection = await this.prisma.collection.update({ - where: { id: collection.id }, - include: { _count: { select: { documents: true } } }, - data: { - name: updateCollectionDto.name, - description: updateCollectionDto.description, - documents: { - connect: addedDocuments.map((doc) => ({ id: doc.id })), - disconnect: removedDocuments.map((doc) => ({ id: doc.id })), - }, - }, - }); + const updateData: any = {}; + if (updateCollectionDto.newName) { + updateData.name = updateCollectionDto.newName; + } + if (updateCollectionDto.description) { + updateData.description = updateCollectionDto.description; + } + if (addedDocuments.length > 0) { + updateData.documents = { + connect: addedDocuments.map((doc) => ({ id: doc.id })), + }; + } + if (removedDocuments.length > 0) { + updateData.documents = { + disconnect: removedDocuments.map((doc) => ({ id: doc.id })), + }; + } - return { - name: updatedCollection.name, - description: updatedCollection.description, - count: updatedCollection._count.documents, - }; + try { + const updatedCollection = await this.prisma.collection.update({ + where: { id: collection.id }, + include: { _count: { select: { documents: true } } }, + data: { ...updateData }, + }); + + return { + name: updatedCollection.name, + description: updatedCollection.description, + count: updatedCollection._count.documents, + }; + } catch (e) { + if (e.code === 'P2002') { + // "Unique constraint failed on the {constraint}" 에러 + throw new BadRequestException('Collection already exists'); + } + } } async addDocuments( diff --git a/src/collection/dto/request/update-collection.dto.ts b/src/collection/dto/request/update-collection.dto.ts index 7752eb6..2369f48 100644 --- a/src/collection/dto/request/update-collection.dto.ts +++ b/src/collection/dto/request/update-collection.dto.ts @@ -1,13 +1,14 @@ -import { IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { IsOptional, IsString } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; export class UpdateCollectionDto { + @IsOptional() @IsString() - @IsNotEmpty() - name: string; + newName?: string; + @IsOptional() @IsString() - description: string; + description?: string; @IsOptional() @IsString({ each: true })