Skip to content

Commit

Permalink
✨ Feat: 컬렉션 update field optional하게 변경
Browse files Browse the repository at this point in the history
#
  • Loading branch information
ks1ksi committed Sep 12, 2023
1 parent bdb60d7 commit d494af7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
54 changes: 36 additions & 18 deletions src/collection/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions src/collection/dto/request/update-collection.dto.ts
Original file line number Diff line number Diff line change
@@ -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 })
Expand Down

0 comments on commit d494af7

Please sign in to comment.