diff --git a/src/collection/collection.controller.ts b/src/collection/collection.controller.ts index 8b966f9..cb7ee32 100644 --- a/src/collection/collection.controller.ts +++ b/src/collection/collection.controller.ts @@ -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, diff --git a/src/collection/collection.service.ts b/src/collection/collection.service.ts index efd1696..ed18c43 100644 --- a/src/collection/collection.service.ts +++ b/src/collection/collection.service.ts @@ -175,4 +175,36 @@ export class CollectionService { count: updatedCollection._count.documents, }; } + + async addDocuments( + uid: string, + name: string, + docIds: string[], + ): Promise { + 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 })), + }, + }, + }); + } }