-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Application Server Search API 구현
검색에 대한 정보 제공을 위한 Application Server의 API를 추가하였습니다. 사용방법: [GET] api.funch.site/search?query=검색어
- Loading branch information
Showing
4 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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(' ', ''); | ||
const searchResult = { | ||
lives: [], | ||
members: [], | ||
}; | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
const metadata = [...live.tags, live.contentCategory, live.moodCategory, live.title, live.userName] | ||
.join('') | ||
.replace(' ', ''); | ||
if (metadata.match(new RegExp(keyword))) list.push(live); | ||
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; | ||
}); | ||
} | ||
} |