Skip to content

Commit

Permalink
Merge pull request #422 from boostcampwm2023/BE-AutoComplete-#420
Browse files Browse the repository at this point in the history
[BE/#422] 제목 자동완성 API 구현
  • Loading branch information
koomin1227 authored Dec 9, 2023
2 parents 625c139 + b8b751e commit ac331d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions BE/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
Get,
HttpCode,
HttpException,
MaxFileSizeValidator,
Param,
ParseFilePipe,
Patch,
Post,
Query,
Expand Down Expand Up @@ -38,6 +36,11 @@ export class PostController {
return posts;
}

@Get('/titles')
async postsTitlesList(@Query('searchKeyword') searchKeyword) {
return await this.postService.findPostsTitles(searchKeyword);
}

@Post()
@UseInterceptors(FilesInterceptor('image', 12))
async postsCreate(
Expand Down
12 changes: 11 additions & 1 deletion BE/src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ export class PostService {
post.end_date = createPostDto.end_date;
post.user_hash = userHash;
post.thumbnail = imageLocations.length > 0 ? imageLocations[0] : null;
// 이미지 추가
const res = await this.postRepository.save(post);
if (res.is_request === false) {
await this.createImages(imageLocations, res.id);
Expand All @@ -249,4 +248,15 @@ export class PostService {
await this.blockPostRepository.softDelete({ blocked_post: postId });
await this.postRepository.softDelete({ id: postId });
}

async findPostsTitles(searchKeyword: string) {
const posts: PostEntity[] = await this.postRepository.find({
where: { title: Like(`%${searchKeyword}%`) },
order: {
create_date: 'desc',
},
});
const titles: string[] = posts.map((post) => post.title);
return titles.slice(0, 5);
}
}

0 comments on commit ac331d7

Please sign in to comment.