Skip to content

Commit

Permalink
✨ Feat: 컬렉션에 포함된 문서 반환하는 기능 추가
Browse files Browse the repository at this point in the history
#
  • Loading branch information
ks1ksi committed Sep 11, 2023
1 parent 3647035 commit 03ea383
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
50 changes: 50 additions & 0 deletions src/document/document.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,56 @@ export class DocumentController {
return this.documentService.findByTag(uid, tagName, cursor, docId, limit);
}

@Get('search/collection')
@ApiQuery({
name: 'collectionName',
required: true,
type: String,
description: '컬렉션 이름',
example: 'collection0',
})
@ApiQuery({
name: 'cursor',
required: false,
type: Date,
description:
'이전에 받은 문서들 중 마지막 문서의 updatedAt. 이 값이 없으면 현재 시간으로.',
example: '2021-01-01T00:00:00.000Z',
})
@ApiQuery({
name: 'doc-id',
required: false,
type: String,
description:
'이전에 받은 문서들 중 마지막 문서의 docId. 이 값이 없으면 가장 큰 UUID 값으로.',
example: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
})
@ApiQuery({
name: 'limit',
required: false,
type: Number,
description: '한 번에 받을 문서의 개수. 최대 20개까지 가능. 기본값은 20.',
})
findByCollection(
@GetUid() uid: string,
@Query('collectionName') collectionName: string,
@Query('cursor') cursor?: Date,
@Query(
'doc-id',
new DefaultValuePipe('ffffffff-ffff-ffff-ffff-ffffffffffff'),
)
docId?: string,
@Query('limit', new DefaultValuePipe(20), ParseIntPipe) limit?: number,
): Promise<DocumentDto[]> {
return this.documentService.findByCollection(
uid,
collectionName,
cursor,
docId,
limit,
);
}

@Delete(':docId')
deleteOne(
@GetUid() uid: string,
Expand Down
58 changes: 55 additions & 3 deletions src/document/document.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,12 @@ export class DocumentService {

const user = await this.userService.findByUid(uid);

const tag = await this.prisma.tag.findFirst({
const tag = await this.prisma.tag.findUnique({
where: {
user: { id: user.id },
name: tagName,
name_userId: {
name: tagName,
userId: user.id,
},
},
});

Expand Down Expand Up @@ -385,6 +387,56 @@ export class DocumentService {
return documents.map((document) => new DocumentDto(document));
}

async findByCollection(
uid: string,
collectionName: string,
cursor: Date,
docId: string,
take: number,
): Promise<DocumentDto[]> {
cursor = cursor || new Date();
take = Math.min(take, 20);

const user = await this.userService.findByUid(uid);

const collection = await this.prisma.collection.findUnique({
where: {
name_userId: {
name: collectionName,
userId: user.id,
},
},
});

if (!collection) {
throw new NotFoundException(`Tag with name ${collectionName} not found`);
}

const documents = await this.prisma.document.findMany({
where: {
AND: [
{
collections: { some: { id: collection.id } },
},
{
OR: [
{ updatedAt: { lt: cursor } },
{
updatedAt: cursor,
docId: { lt: docId },
},
],
},
],
},
include: { tags: true },
orderBy: [{ updatedAt: 'desc' }],
take,
});

return documents.map((document) => new DocumentDto(document));
}

private async getDeleteTags(tagIds: bigint[]): Promise<Tag[]> {
return tagIds.length === 0
? []
Expand Down

0 comments on commit 03ea383

Please sign in to comment.