-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
검색 API 구현 #319
검색 API 구현 #319
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,19 +12,25 @@ class MemberService { | |
private memberRepository: Repository<Member>, | ||
) {} | ||
|
||
async findMembers() { | ||
findMembers() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. async를 빼신 이유가 있으실까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. await을 안써서 뺐습니다! |
||
return this.memberRepository.find(); | ||
} | ||
|
||
async findMembersWithFollowTable(id) { | ||
findMembersWithCondition(condition) { | ||
return this.memberRepository.find({ | ||
where: condition, | ||
}); | ||
} | ||
|
||
findMembersWithFollowTable(id) { | ||
return this.memberRepository | ||
.createQueryBuilder('member') | ||
.innerJoin('follow', 'f', 'f.following = member.id') | ||
.where('f.follower = :id', { id }) | ||
.getMany(); | ||
} | ||
|
||
async findOneMemberWithCondition(condition: { [key: string]: string }) { | ||
findOneMemberWithCondition(condition: { [key: string]: string }) { | ||
return this.memberRepository.findOne({ where: condition }); | ||
} | ||
|
||
|
@@ -37,7 +43,7 @@ class MemberService { | |
return member; | ||
} | ||
|
||
async register(id: string, image_url?: string): Promise<Member> { | ||
register(id: string, image_url?: string): Promise<Member> { | ||
const name = this.generateUniqueName(); | ||
const user = { | ||
id, | ||
|
@@ -59,7 +65,7 @@ class MemberService { | |
return name; | ||
} | ||
|
||
async refreshStreamKey(id) { | ||
refreshStreamKey(id) { | ||
const newStreamKey = crypto.randomUUID(); | ||
this.memberRepository.update(id, { stream_key: newStreamKey }); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Controller, Get, Query } from '@nestjs/common'; | ||
import { SearchService } from '@search/search.service'; | ||
import { LiveService } from '@src/live/live.service'; | ||
|
||
@Controller('search') | ||
export class SearchController { | ||
constructor( | ||
private readonly searchService: SearchService, | ||
private readonly liveService: LiveService, | ||
) {} | ||
|
||
@Get('/') | ||
async getSearchData(@Query('query') query: string) { | ||
const keyword = query.replace(' ', ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 쿼리에서 공백을 없애는 이유가 있으신가요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 검색 효율성을 위해 공백을 제거한 문자열 매칭으로 탐색하기 때문에 검색어에서도 공백을 제거했습니다. |
||
const searchResult = { | ||
lives: [], | ||
members: [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이것도 on/off 같이 변수명을 더 직관적이게 만들면 좋을 것 같아요. |
||
}; | ||
await Promise.all([ | ||
(searchResult.lives = this.searchService.getLiveListWithKeyword(keyword)), | ||
(searchResult.members = (await this.searchService.getMemberListWithKeyword(keyword)).reduce( | ||
(offline, thisUser) => { | ||
if (!this.liveService.live.data.has(thisUser.broadcast_id)) offline.push(thisUser); | ||
return offline; | ||
}, | ||
[], | ||
)), | ||
]); | ||
|
||
return searchResult; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LiveModule } from '@live/live.module'; | ||
import { MemberModule } from '@src/member/member.module'; | ||
import { SearchController } from '@search/search.controller'; | ||
import { SearchService } from '@search/search.service'; | ||
|
||
@Module({ | ||
imports: [MemberModule, LiveModule], | ||
controllers: [SearchController], | ||
providers: [SearchService], | ||
}) | ||
export class SearchModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { MemberService } from '@src/member/member.service'; | ||
import { LiveService } from '@src/live/live.service'; | ||
import { Like } from 'typeorm'; | ||
import { User } from '@src/types'; | ||
|
||
@Injectable() | ||
export class SearchService { | ||
constructor( | ||
private readonly memberService: MemberService, | ||
private readonly liveService: LiveService, | ||
) {} | ||
|
||
getLiveListWithKeyword(keyword) { | ||
const currentLiveList = this.liveService.getLiveList(0); | ||
const filteredLiveList = currentLiveList.reduce((list, live) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 진영님 정말 reduce를 좋아하시고 또 잘 쓰시네요..!! |
||
const metadata = [...live.tags, live.contentCategory, live.moodCategory, live.title, live.userName] | ||
.join('') | ||
.replace(' ', ''); | ||
if (metadata.match(new RegExp(keyword))) list.push(live); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아까 개발일지를 봤었는데, kmp 알고리즘과 정규표현식의 비교를 통해 정규표현식으로 결정하셨네요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사실 kmp가 당연히 더 빠를 줄 알았는데 느려서 조금 당황스럽긴 하네요.. |
||
return list; | ||
}, []); | ||
|
||
return filteredLiveList; | ||
} | ||
|
||
async getMemberListWithKeyword(keyword) { | ||
const condition = { name: Like(`%${keyword}%`) }; | ||
const searchMemberList = await this.memberService.findMembersWithCondition(condition); | ||
|
||
return searchMemberList.map((member) => { | ||
return { | ||
name: member.name, | ||
profile_image: member.profile_image, | ||
broadcast_id: member.broadcast_id, | ||
follower_count: member.follower_count, | ||
} as User; | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
점점 늘어가는 모듈들,,,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ㅠㅠ