Skip to content

Commit

Permalink
merge #270: 게시글 검색 시 offset, limit 제대로 동작 X 이슈 & 제목 검색 시 500번 에러 반환 이…
Browse files Browse the repository at this point in the history
…슈 해결

[fix] 게시글 검색 시 offset, limit 제대로 동작 X 이슈 & 제목 검색 시 500번 에러 반환 이슈 해결
  • Loading branch information
yaongmeow authored Dec 6, 2023
2 parents c60fbda + 0a6b4e0 commit 57665e1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion BE/src/postings/postings.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class PostingsController {
@ApiOkResponse({ schema: { example: searchByWord_OK } })
async searchByKeyWord(
@Query('keyword', new DefaultValuePipe('')) keyword: string
): Promise<string[]> {
) {
return this.postingsService.findAllBytitle(keyword);
}

Expand Down
9 changes: 1 addition & 8 deletions BE/src/postings/postings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,7 @@ export class PostingsService {

async findAllBytitle(keyword: string) {
const titles = await this.postingsRepository.findAllByTitle(keyword);

return [
...new Set(
titles
.filter((e) => e.reports.length <= BLOCKING_LIMIT)
.map((e) => e.title)
),
];
return titles.map((e) => e.title);
}

async findAllByWriter(userId: string) {
Expand Down
30 changes: 18 additions & 12 deletions BE/src/postings/repositories/postings.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { POSTINGS_REPOSITORY } from '../postings.constants';
import { BLOCKING_LIMIT, POSTINGS_REPOSITORY } from '../postings.constants';
import { Posting } from '../entities/posting.entity';
import { Inject, Injectable } from '@nestjs/common';
import { Like, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import {
Budget,
Headcount,
Expand Down Expand Up @@ -51,13 +51,15 @@ export class PostingsRepository {
) {
const qb = this.postingsRepository
.createQueryBuilder('p')
.leftJoinAndSelect('p.writer', 'u')
.where('p.title LIKE :keyword', { keyword: `%${keyword}%` })
.leftJoin('p.likeds', 'l', 'l.isDeleted = :isDeleted', {
isDeleted: false,
})
.leftJoinAndSelect('p.writer', 'u')
.where('p.title LIKE :keyword', { keyword: `%${keyword}%` })
.groupBy('p.id')
.addSelect('COUNT(l.posting)', 'likeds');
.addSelect('COUNT(l.posting)', 'likeds')
.leftJoin('p.reports', 'r')
.having('COUNT(r.posting) <= :BLOCKING_LIMIT', { BLOCKING_LIMIT })
.groupBy('p.id');

if (budget) {
qb.where('p.budget = :budget', { budget });
Expand Down Expand Up @@ -138,16 +140,20 @@ export class PostingsRepository {
}

return qb
.skip((offset - 1) * limit)
.take(limit)
.offset((offset - 1) * limit)
.limit(limit)
.getRawMany();
}

async findAllByTitle(keyword: string) {
return this.postingsRepository.find({
where: { title: Like(`${keyword}%`) },
select: ['title'],
});
return this.postingsRepository
.createQueryBuilder('p')
.select('DISTINCT p.title', 'title')
.where('p.title LIKE :keyword', { keyword: `%${keyword}%` })
.leftJoin('p.reports', 'r')
.groupBy('p.id')
.having('COUNT(r.posting) <= :BLOCKING_LIMIT', { BLOCKING_LIMIT })
.getRawMany();
}

async findAllByWriter(userId: string) {
Expand Down

0 comments on commit 57665e1

Please sign in to comment.