Skip to content

Commit

Permalink
✨ Feat: 컬렉션에 문서 추가하는 기능
Browse files Browse the repository at this point in the history
#
  • Loading branch information
ks1ksi committed Sep 12, 2023
1 parent c99d506 commit bdb60d7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/collection/collection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export class CollectionController {
return this.collectionService.create(uid, createCollectionDto);
}

@Post('add/:name')
addDocuments(
@GetUid() uid: string,
@Param('name') name: string,
@Body() docIds: string[],
) {
return this.collectionService.addDocuments(uid, name, docIds);
}

@Get('find/:name')
findByName(
@GetUid() uid: string,
Expand Down
32 changes: 32 additions & 0 deletions src/collection/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,36 @@ export class CollectionService {
count: updatedCollection._count.documents,
};
}

async addDocuments(
uid: string,
name: string,
docIds: string[],
): Promise<void> {
const user = await this.userService.findByUid(uid);
const collection = await this.prisma.collection.findUnique({
where: { name_userId: { userId: user.id, name } },
});

if (!collection) {
throw new BadRequestException('Collection does not exist');
}

const documents = await this.prisma.document.findMany({
select: { id: true },
where: {
userId: user.id,
docId: { in: docIds },
},
});

await this.prisma.collection.update({
where: { id: collection.id },
data: {
documents: {
connect: documents.map((doc) => ({ id: doc.id })),
},
},
});
}
}

0 comments on commit bdb60d7

Please sign in to comment.