Skip to content

Commit

Permalink
Merge pull request #111 from boostcampwm2023/BE-GetPostsPagination-#109
Browse files Browse the repository at this point in the history
[BE/#109] 게시글 목록 조회에 pagination, filter 기능 추가
  • Loading branch information
namewhat99 authored Nov 22, 2023
2 parents efdf87d + 6dcdd44 commit fd14f8d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
6 changes: 5 additions & 1 deletion BE/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ async function bootstrap() {
});

app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
app.useGlobalPipes(new ValidationPipe());
app.useGlobalPipes(
new ValidationPipe({
transform: true,
}),
);
setupSwagger(app);
await app.listen(3000);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsBoolean, IsNumber, IsString, ValidateIf } from 'class-validator';

export class CreatePostDto {
export class PostCreateDto {
@IsString()
title: string;

Expand Down
22 changes: 22 additions & 0 deletions BE/src/post/dto/postList.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IsNumber, IsOptional, IsString } from 'class-validator';
import { Type } from 'class-transformer';

export class PostListDto {
@IsNumber()
@IsOptional()
@Type(() => Number)
page: number;

@IsNumber()
@IsOptional()
@Type(() => Number)
requestFilter: number;

@IsString()
@IsOptional()
searchKeyword: string;

@IsString()
@IsOptional()
writer: string;
}
File renamed without changes.
18 changes: 9 additions & 9 deletions BE/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import {
HttpException,
Param,
Patch,
Req,
Res,
Post,
UploadedFiles,
UseInterceptors,
ValidationPipe,
Delete,
Query,
} from '@nestjs/common';
import { PostService } from './post.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { UpdatePostDto } from './postUpdateDto';
import { UpdatePostDto } from './dto/postUpdate.dto';
import { FilesInterceptor } from '@nestjs/platform-express';
import { CreatePostDto } from './createPost.dto';
import { PostCreateDto } from './dto/postCreate.dto';
import { MultiPartBody } from '../utils/multiPartBody.decorator';
import { PostListDto } from './dto/postList.dto';

@Controller('posts')
@ApiTags('posts')
export class PostController {
constructor(private readonly postService: PostService) {}

@Get()
async getPosts() {
const posts = await this.postService.getPosts();
async postsList(@Query() query: PostListDto) {
const posts = await this.postService.findPosts(query);
return posts;
}

Expand All @@ -40,14 +40,14 @@ export class PostController {
'profile_info',
new ValidationPipe({ validateCustomDecorators: true }),
)
createPostDto: CreatePostDto,
body: PostCreateDto,
) {
const userId: string = 'qwe';
let imageLocation: Array<string> = [];
if (createPostDto.is_request === false && files !== undefined) {
if (body.is_request === false && files !== undefined) {
imageLocation = await this.postService.uploadImages(files);
}
await this.postService.createPost(imageLocation, createPostDto, userId);
await this.postService.createPost(imageLocation, body, userId);
}

@Get('/:id')
Expand Down
23 changes: 17 additions & 6 deletions BE/src/post/post.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { HttpException, Injectable } from '@nestjs/common';
import { HttpException, Injectable, Query } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { PostEntity } from '../entities/post.entity';
import { Repository } from 'typeorm';
import { UpdatePostDto } from './postUpdateDto';
import { validate } from 'class-validator';
import { UpdatePostDto } from './dto/postUpdate.dto';
import { PostImageEntity } from 'src/entities/postImage.entity';
import { S3Handler } from '../utils/S3Handler';
import { UserEntity } from '../entities/user.entity';
import { PostListDto } from './dto/postList.dto';

@Injectable()
export class PostService {
Expand All @@ -19,8 +19,19 @@ export class PostService {
private postImageRepository: Repository<PostImageEntity>,
private s3Handler: S3Handler,
) {}
async getPosts() {
const res = await this.postRepository.find();
async findPosts(query: PostListDto) {
const page: number = query.page === undefined ? 1 : query.page;
const limit: number = 19;
const offset: number = limit * (page - 1) + 1;
const res = await this.postRepository.find({
take: limit,
skip: offset,
where: {
status: true,
is_request: query.requestFilter !== 0,
},
relations: ['post_images'],
});
const posts = [];
res.forEach((re) => {
const post = {
Expand All @@ -30,7 +41,7 @@ export class PostService {
post_id: re.id,
user_id: re.user_id,
is_request: re.is_request,
images: re.post_images,
images: re.post_images.map((post_image) => post_image.image_url),
start_date: re.start_date,
end_date: re.end_date,
};
Expand Down

0 comments on commit fd14f8d

Please sign in to comment.