Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix : 읽음여부 필터 적용 #72

Merged
merged 2 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/modules/folders/responses/post.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class PostResponse {
@ApiProperty()
isFavorite: boolean;

@ApiProperty()
readAt: Date;

@ApiProperty()
createdAt: Date;

Expand All @@ -53,6 +56,7 @@ export class PostResponse {
this.title = data.title;
this.description = data.description;
this.isFavorite = data.isFavorite;
this.readAt = data.readAt;
this.createdAt = data.createdAt;
}
}
8 changes: 4 additions & 4 deletions src/modules/posts/dto/find-in-folder.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions src/modules/posts/dto/list-post.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
52 changes: 47 additions & 5 deletions src/modules/posts/posts.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ export class PostsRepository {
private readonly aiClassificationModel: Model<AIClassification>,
) {}

async getUserPostCount(userId: string, isFavorite?: boolean) {
async getUserPostCount(
userId: string,
isFavorite?: boolean,
isRead?: boolean,
) {
const queryFilter: FilterQuery<Post> = {
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;
}
Expand All @@ -36,6 +47,7 @@ export class PostsRepository {
limit: number,
isFavorite?: boolean,
order = OrderType.desc,
isRead?: boolean,
) {
// Skip Query
const skipQuery = (page - 1) * limit;
Expand All @@ -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]])
Expand Down Expand Up @@ -175,15 +193,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<Post> = {
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<Post> = {
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);

Expand Down
13 changes: 11 additions & 2 deletions src/modules/posts/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,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 {
Expand Down Expand Up @@ -89,11 +94,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 };
Expand Down