From 79f2ca0785b49248f748e11b592756ce95bd4d6b Mon Sep 17 00:00:00 2001 From: hye-on Date: Sun, 21 Jul 2024 00:31:09 +0900 Subject: [PATCH] =?UTF-8?q?Chore=20:=20=EC=9D=BD=EC=9D=8C=EC=97=AC?= =?UTF-8?q?=EB=B6=80=20=ED=95=84=ED=84=B0=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../folders/responses/post.response.ts | 4 ++ src/modules/posts/dto/find-in-folder.dto.ts | 8 +-- src/modules/posts/dto/list-post.dto.ts | 9 ++++ src/modules/posts/posts.repository.ts | 52 +++++++++++++++++-- src/modules/posts/posts.service.ts | 13 ++++- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/modules/folders/responses/post.response.ts b/src/modules/folders/responses/post.response.ts index 432d054..1e2ef32 100644 --- a/src/modules/folders/responses/post.response.ts +++ b/src/modules/folders/responses/post.response.ts @@ -40,6 +40,9 @@ export class PostResponse { @ApiProperty() isFavorite: boolean; + @ApiProperty() + readAt: Date; + @ApiProperty() createdAt: Date; @@ -54,6 +57,7 @@ export class PostResponse { this.title = data.title; this.description = data.description; this.isFavorite = data.isFavorite; + this.readAt = data.readAt; this.createdAt = data.createdAt; } } diff --git a/src/modules/posts/dto/find-in-folder.dto.ts b/src/modules/posts/dto/find-in-folder.dto.ts index b8b9873..0426ea6 100644 --- a/src/modules/posts/dto/find-in-folder.dto.ts +++ b/src/modules/posts/dto/find-in-folder.dto.ts @@ -5,11 +5,11 @@ import { Transform } from 'class-transformer'; export class GetPostQueryDto extends PaginationQuery { @ApiProperty({ - description: '읽지 않음 여부', required: false, + description: '읽음 필터링 여부', }) - @IsOptional() - @IsBoolean() @Transform(({ value }) => (value === 'true' ? true : false)) - unread?: boolean; + @IsBoolean() + @IsOptional() + isRead: boolean; } diff --git a/src/modules/posts/dto/list-post.dto.ts b/src/modules/posts/dto/list-post.dto.ts index 2755fc5..5b6ec9c 100644 --- a/src/modules/posts/dto/list-post.dto.ts +++ b/src/modules/posts/dto/list-post.dto.ts @@ -12,4 +12,13 @@ export class ListPostQueryDto extends PaginationQuery { @IsBoolean() @IsOptional() favorite: boolean; + + @ApiProperty({ + required: false, + description: '읽음 필터링 여부', + }) + @Transform(({ value }) => (value === 'true' ? true : false)) + @IsBoolean() + @IsOptional() + isRead: boolean; } diff --git a/src/modules/posts/posts.repository.ts b/src/modules/posts/posts.repository.ts index c744440..9a9a7b5 100644 --- a/src/modules/posts/posts.repository.ts +++ b/src/modules/posts/posts.repository.ts @@ -19,13 +19,24 @@ export class PostsRepository { private readonly aiClassificationModel: Model, ) {} - async getUserPostCount(userId: string, isFavorite?: boolean) { + async getUserPostCount( + userId: string, + isFavorite?: boolean, + isRead?: boolean, + ) { const queryFilter: FilterQuery = { userId: userId, }; if (isFavorite) { queryFilter['isFavorite'] = true; } + + if (isRead) { + queryFilter['readAt'] = { $ne: null }; + } else if (isRead === false) { + queryFilter['readAt'] = null; + } + const userPostCount = await this.postModel.countDocuments(queryFilter); return userPostCount; } @@ -36,6 +47,7 @@ export class PostsRepository { limit: number, isFavorite?: boolean, order = OrderType.desc, + isRead?: boolean, ) { // Skip Query const skipQuery = (page - 1) * limit; @@ -46,6 +58,12 @@ export class PostsRepository { if (isFavorite) { queryFilter['isFavorite'] = true; } + + if (isRead) { + queryFilter['readAt'] = { $ne: null }; + } else if (isRead === false) { + queryFilter['readAt'] = null; + } const posts = await this.postModel .find(queryFilter) .sort([['createdAt', order === OrderType.desc ? -1 : 1]]) @@ -181,15 +199,39 @@ export class PostsRepository { .exec(); } - async getCountByFolderId(folderId: string) { - const count = await this.postModel.countDocuments({ folderId }); + async getCountByFolderId(folderId: string, isRead?: boolean) { + const queryFilter: FilterQuery = { + folderId: folderId, + }; + + if (isRead) { + queryFilter['readAt'] = { $ne: null }; + } else if (isRead === false) { + queryFilter['readAt'] = null; + } + const count = await this.postModel.countDocuments(queryFilter); return count; } - async findByFolderId(folderId: string, offset: number, limit: number) { + async findByFolderId( + folderId: string, + offset: number, + limit: number, + isRead?: boolean, + ) { + const queryFilter: FilterQuery = { + folderId: folderId, + }; + + if (isRead) { + queryFilter['readAt'] = { $ne: null }; + } else if (isRead === false) { + queryFilter['readAt'] = null; + } + const folders = await this.postModel - .find({ folderId }) + .find(queryFilter) .skip(offset) .limit(limit); diff --git a/src/modules/posts/posts.service.ts b/src/modules/posts/posts.service.ts index 5f3045c..ae62ac1 100644 --- a/src/modules/posts/posts.service.ts +++ b/src/modules/posts/posts.service.ts @@ -20,13 +20,18 @@ export class PostsService { async listPost(userId: string, query: ListPostQueryDto) { const [count, posts] = await Promise.all([ - this.postRepository.getUserPostCount(userId, query.favorite), + this.postRepository.getUserPostCount( + userId, + query.favorite, + query.isRead, + ), this.postRepository.listPost( userId, query.page, query.limit, query.favorite, query.order, + query.isRead, ), ]); return { @@ -87,11 +92,15 @@ export class PostsService { }); const offset = (query.page - 1) * query.limit; - const count = await this.postRepository.getCountByFolderId(folderId); + const count = await this.postRepository.getCountByFolderId( + folderId, + query.isRead, + ); const posts = await this.postRepository.findByFolderId( folderId, offset, query.limit, + query.isRead, ); return { count, posts };