Skip to content

Commit 652dae2

Browse files
authored
Merge pull request #21 from oodd-team/OD-38
fix: 게시글 리스트 조회 기능 수정
2 parents 3407eea + 9d04319 commit 652dae2

9 files changed

+244
-60
lines changed

src/post-comment/post-comment.controller.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class PostCommentController {
4242
@Body() createCommentDto: CreateCommentDto,
4343
@Req() req: Request,
4444
): Promise<BaseResponse<any>> {
45-
const currentUserId = req.user.userId;
45+
const currentUserId = req.user.id;
4646

4747
await this.postService.validatePost(postId);
4848

@@ -61,7 +61,7 @@ export class PostCommentController {
6161
@Query('postId') postId: number,
6262
@Req() req: Request,
6363
): Promise<BaseResponse<GetCommentsDto>> {
64-
const currentUserId = req.user.userId;
64+
const currentUserId = req.user.id;
6565

6666
const comments = await this.postCommentService.getPostComments(postId);
6767

@@ -88,7 +88,7 @@ export class PostCommentController {
8888
commentId: number,
8989
@Req() req: Request,
9090
): Promise<BaseResponse<PostComment>> {
91-
const currentUserId = req.user.userId;
91+
const currentUserId = req.user.id;
9292

9393
await this.postCommentService.validateUser(commentId, currentUserId);
9494

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { PageOptionsDto } from './page-options.dto';
2+
3+
export interface PageMetaDtoParameters {
4+
pageOptionsDto: PageOptionsDto;
5+
total: number;
6+
}

src/post/dtos/page-meta.dto.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { IsInt, IsPositive } from 'class-validator';
2+
import { PageMetaDtoParameters } from './meta-dto-parameter.interface';
3+
import { ApiProperty } from '@nestjs/swagger';
4+
5+
export class PageMetaDto {
6+
@ApiProperty({ description: '총 게시물 수', example: 100 })
7+
@IsInt()
8+
@IsPositive()
9+
readonly total: number;
10+
@ApiProperty({ description: '현재 페이지 번호', example: 1 })
11+
@IsInt()
12+
@IsPositive()
13+
readonly page: number;
14+
@ApiProperty({ description: '페이지당 게시물 수', example: 10 })
15+
@IsInt()
16+
@IsPositive()
17+
readonly take: number;
18+
@ApiProperty({ description: '마지막 페이지 번호', example: 10 })
19+
@IsInt()
20+
@IsPositive()
21+
readonly last_page: number;
22+
@ApiProperty({ description: '이전 페이지 존재 여부', example: true })
23+
readonly hasPreviousPage: boolean;
24+
@ApiProperty({ description: '다음 페이지 존재 여부', example: true })
25+
readonly hasNextPage: boolean;
26+
27+
constructor({ pageOptionsDto, total }: PageMetaDtoParameters) {
28+
this.page = pageOptionsDto.page <= 0 ? 1 : pageOptionsDto.page; // 페이지가 0 이하일 경우 1로 설정
29+
this.take = pageOptionsDto.take;
30+
this.total = total;
31+
this.last_page = Math.ceil(this.total / this.take); // 마지막 페이지 계산
32+
this.hasPreviousPage = this.page > 1; // 이전 페이지 여부
33+
this.hasNextPage = this.page < this.last_page; // 다음 페이지 여부
34+
}
35+
}

src/post/dtos/page-options.dto.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { Type } from 'class-transformer';
3+
import { IsInt, IsOptional, Min } from 'class-validator';
4+
5+
export class PageOptionsDto {
6+
@ApiProperty({
7+
example: '1',
8+
description: '페이지 번호',
9+
})
10+
@Type(() => Number)
11+
@IsInt()
12+
@IsOptional()
13+
@Min(1) // 페이지는 1 이상
14+
page?: number;
15+
16+
@ApiProperty({
17+
example: '10',
18+
description: '한 페이지에 불러올 데이터 개수',
19+
})
20+
@Type(() => Number)
21+
@IsInt()
22+
@IsOptional()
23+
take?: number;
24+
25+
// 기본값 설정
26+
constructor() {
27+
this.page = this.page || 1;
28+
this.take = this.take || 10;
29+
}
30+
}

src/post/dtos/page.dto.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { IsArray } from 'class-validator';
2+
import { PageMetaDto } from './page-meta.dto';
3+
import { ApiProperty } from '@nestjs/swagger';
4+
import { GetPostsResponse } from './total-postsResponse.dto';
5+
import {
6+
GetMyPostsResponse,
7+
GetOtherPostsResponse,
8+
} from './user-postsResponse.dto';
9+
10+
export class PageDto<T> {
11+
@ApiProperty({
12+
description: '실제 데이터',
13+
type: [GetPostsResponse, GetMyPostsResponse, GetOtherPostsResponse],
14+
})
15+
@IsArray()
16+
readonly data: T[]; // 실제 데이터
17+
18+
@ApiProperty({ description: '페이지 메타 정보', type: PageMetaDto })
19+
readonly meta: PageMetaDto; // 메타 정보
20+
21+
constructor(data: T[], meta: PageMetaDto) {
22+
this.data = data;
23+
this.meta = meta;
24+
}
25+
}

src/post/dtos/total-postsResponse.dto.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ApiProperty } from '@nestjs/swagger';
2+
23
class PostImageDto {
34
@ApiProperty({
45
example: 'http://postimageurl.example',

src/post/post.controller.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ import {
2323
PatchIsRepresentativeSwagger,
2424
PatchPostSwagger,
2525
} from './post.swagger';
26-
import { ApiBearerAuth, ApiParam, ApiTags } from '@nestjs/swagger';
26+
import { ApiBearerAuth, ApiQuery, ApiTags } from '@nestjs/swagger';
2727
import { CreatePostDto } from './dtos/create-post.dto';
2828
import { BaseResponse } from 'src/common/response/dto';
2929
import { AuthGuard } from 'src/auth/guards/jwt.auth.guard';
3030
import { Request } from 'express';
3131
import { GetPostResponse } from './dtos/get-post.dto';
3232
import { PatchPostDto } from './dtos/patch-Post.dto';
33+
import { PageOptionsDto } from './dtos/page-options.dto';
34+
import { PageDto } from './dtos/page.dto';
3335

3436
@Controller('post')
3537
@ApiBearerAuth()
@@ -40,21 +42,48 @@ export class PostController {
4042

4143
@Get('/')
4244
@GetPostsSwagger('게시글 리스트 조회 API')
43-
@ApiParam({ name: 'userId', required: false, description: 'User ID' })
45+
@ApiQuery({
46+
name: 'userId',
47+
required: false,
48+
description: 'User ID',
49+
type: Number,
50+
})
51+
@ApiQuery({
52+
name: 'page',
53+
required: false,
54+
description: '페이지 번호',
55+
type: Number,
56+
})
57+
@ApiQuery({
58+
name: 'take',
59+
required: false,
60+
description: '한 페이지에 불러올 데이터 개수',
61+
})
4462
async getPosts(
4563
@Req() req: Request,
64+
@Query() pageOptionsDto?: PageOptionsDto,
4665
@Query('userId') userId?: number,
4766
): Promise<
48-
BaseResponse<GetPostsResponse | GetMyPostsResponse | GetOtherPostsResponse>
67+
BaseResponse<
68+
PageDto<GetPostsResponse | GetMyPostsResponse | GetOtherPostsResponse>
69+
>
4970
> {
5071
const currentUserId = req.user.id;
5172

73+
const options = pageOptionsDto
74+
? {
75+
...new PageOptionsDto(),
76+
...pageOptionsDto, // Dto에 전달된 기본값
77+
}
78+
: new PageOptionsDto();
79+
5280
const postsResponse = await this.postService.getPosts(
81+
options,
5382
userId,
5483
currentUserId,
5584
);
5685

57-
return new BaseResponse(true, 'SUCCESS', postsResponse);
86+
return new BaseResponse(true, '게시글 리스트 조회 성공', postsResponse);
5887
}
5988

6089
@Get(':postId')

0 commit comments

Comments
 (0)