diff --git a/applicationServer/src/app.module.ts b/applicationServer/src/app.module.ts index 1652422c..a17ae97f 100644 --- a/applicationServer/src/app.module.ts +++ b/applicationServer/src/app.module.ts @@ -6,13 +6,23 @@ import { LiveModule } from '@live/live.module'; import { GithubAuthModule } from '@github/github.module'; import { AuthModule } from '@auth/auth.module'; import { FollowModule } from '@follow/follow.module'; +import { SearchModule } from './search/search.module'; import { NaverAuthModule } from '@naver/naver.module'; import { GoogleAuthModule } from '@google/google.module'; dotenv.config(); @Module({ - imports: [MemberModule, GithubAuthModule, AuthModule, LiveModule, FollowModule, NaverAuthModule, GoogleAuthModule], + imports: [ + MemberModule, + GithubAuthModule, + AuthModule, + LiveModule, + SearchModule, + FollowModule, + NaverAuthModule, + GoogleAuthModule, + ], }) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { diff --git a/applicationServer/src/search/search.controller.ts b/applicationServer/src/search/search.controller.ts new file mode 100644 index 00000000..d46f3ede --- /dev/null +++ b/applicationServer/src/search/search.controller.ts @@ -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; + } +} diff --git a/applicationServer/src/search/search.module.ts b/applicationServer/src/search/search.module.ts new file mode 100644 index 00000000..a98a5a28 --- /dev/null +++ b/applicationServer/src/search/search.module.ts @@ -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 {} diff --git a/applicationServer/src/search/search.service.ts b/applicationServer/src/search/search.service.ts new file mode 100644 index 00000000..3387b403 --- /dev/null +++ b/applicationServer/src/search/search.service.ts @@ -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; + }); + } +}