Skip to content

Commit

Permalink
Merge pull request #330 from boostcampwm2023/BE-Search-#322
Browse files Browse the repository at this point in the history
[BE/#322] 검색 구현 및 페이지네이션 수정
  • Loading branch information
namewhat99 authored Dec 5, 2023
2 parents 425f55d + 9a72b25 commit 58aaa8e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions BE/src/post/post.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { HttpException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { PostEntity } from '../entities/post.entity';
import { Repository } from 'typeorm';
import { LessThan, Like, Repository } from 'typeorm';
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';
import { BlockUserEntity } from '../entities/blockUser.entity';
import { BlockPostEntity } from '../entities/blockPost.entity';
import { FindOperator } from 'typeorm/find-options/FindOperator';

interface WhereOption {
id: FindOperator<number>;
is_request?: boolean;
user_hash?: string;
title?: FindOperator<string>;
}
@Injectable()
export class PostService {
Expand All @@ -30,13 +33,18 @@ export class PostService {
private s3Handler: S3Handler,
) {}
makeWhereOption(query: PostListDto): WhereOption {
const where: WhereOption = {};
const cursor: FindOperator<number> =
query.page === undefined ? undefined : LessThan(query.page);
const where: WhereOption = { id: cursor };
if (query.requestFilter !== undefined) {
where.is_request = query.requestFilter !== 0;
}
if (query.writer !== undefined) {
where.user_hash = query.writer;
}
if (query.searchKeyword !== undefined) {
where.title = Like(`%${query.searchKeyword}%`);
}
return where;
}

Expand Down Expand Up @@ -80,13 +88,10 @@ export class PostService {
}

async findPosts(query: PostListDto, userId: string) {
const page: number = query.page === undefined ? 1 : query.page;
const limit: number = 20;
const offset: number = limit * (page - 1);

const posts = await this.postRepository.find({
take: limit,
skip: offset,
where: this.makeWhereOption(query),
relations: ['post_images', 'user'],
order: {
Expand Down

0 comments on commit 58aaa8e

Please sign in to comment.