Skip to content

Commit

Permalink
[#91] feat: [모임] 전체 모임의 게시글을 조회하도록 변경 및 모임 정보도 함께 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rdd9223 committed Dec 2, 2023
1 parent 0228cde commit 658d563
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsNumber } from 'class-validator';
import { IsNumber, IsOptional } from 'class-validator';
import { PageOptionsDto } from 'src/common/pagination/dto/page-options.dto';

/**
Expand All @@ -7,7 +7,7 @@ import { PageOptionsDto } from 'src/common/pagination/dto/page-options.dto';
*/
export class PostV1GetPostsQueryDto extends PageOptionsDto {
/** 모임 id */
@IsNotEmpty()
@IsOptional()
@IsNumber()
meetingId: number;
meetingId?: number | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
IsArray,
IsBoolean,
IsDate,
IsEnum,
IsInstance,
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { MeetingCategory } from 'src/entity/meeting/enum/meeting-category.enum';

/**
* 작성자 정보
Expand All @@ -36,6 +38,20 @@ class PostV1GetPostsResponsePostUserDto {
profileImage: string | null;
}

/** 게시글이 속한 모임 정보 */
class PostV1GetPostsResponsePostMeetingDto {
@IsNotEmpty()
@IsNumber()
id: number;

@IsNotEmpty()
@IsString()
title: string;

@IsEnum(MeetingCategory)
category: MeetingCategory;
}

export class PostV1GetPostsResponsePostDto {
/** 게시글 고유 ID */
@IsNotEmpty()
Expand Down Expand Up @@ -88,4 +104,10 @@ export class PostV1GetPostsResponsePostDto {
@IsNotEmpty({ each: true })
@IsString({ each: true })
commenterThumbnails: string[];

@IsNotEmpty()
@IsInstance(PostV1GetPostsResponsePostMeetingDto)
@ValidateNested()
@Type(() => PostV1GetPostsResponsePostMeetingDto)
meeting: PostV1GetPostsResponsePostMeetingDto;
}
35 changes: 28 additions & 7 deletions server/src/post/v1/post-v1.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { PostV1ReportPostResponseDto } from './dto/report-post/post-v1-report-po
import { PostV1GetPostCountQueryDto } from './dto/get-meeting-post-count/post-v1-get-post-count-query.dto';
import { PostV1GetPostCountResponseDto } from './dto/get-meeting-post-count/post-v1-get-post-count-response.dto';
import { PostV1UpdatePostBodyDto } from './dto/update-meeting-post/post-v1-update-post-body.dto';
import { FindManyOptions } from 'typeorm';
import { Post } from 'src/entity/post/post.entity';

@Injectable()
export class PostV1Service {
Expand Down Expand Up @@ -53,13 +55,27 @@ export class PostV1Service {
query: PostV1GetPostsQueryDto;
user: User;
}): Promise<PostV1GetPostsResponseDto> {
const [posts, postAmount] = await this.postRepository.findAndCount({
where: { meetingId: query.meetingId },
relations: ['meeting', 'user', 'comments', 'comments.user', 'likes'],
order: { id: 'DESC' },
skip: query.skip,
take: query.take,
});
const queryOptions: FindManyOptions<Post> = (() => {
const baseOptions: FindManyOptions<Post> = {
relations: ['meeting', 'user', 'comments', 'comments.user', 'likes'],
order: { id: 'DESC' },
skip: query.skip,
take: query.take,
};

if (query.meetingId === undefined) {
return baseOptions;
}

return {
...baseOptions,
where: { meetingId: query.meetingId },
};
})();

const [posts, postAmount] = await this.postRepository.findAndCount(
queryOptions,
);

const pageOptions: PageOptionsDto = {
page: query.page,
Expand Down Expand Up @@ -99,6 +115,11 @@ export class PostV1Service {
viewCount: post.viewCount,
commentCount: post.commentCount,
commenterThumbnails,
meeting: {
id: post.meeting.id,
title: post.meeting.title,
category: post.meeting.category,
},
};
}),
meta: pageMeta,
Expand Down

0 comments on commit 658d563

Please sign in to comment.