Skip to content

Commit

Permalink
feat: Application Server Search API 구현
Browse files Browse the repository at this point in the history
검색에 대한 정보 제공을 위한 Application Server의 API를 추가하였습니다.
사용방법: [GET] api.funch.site/search?query=검색어
  • Loading branch information
JYKIM317 committed Dec 1, 2024
1 parent 90854a1 commit 8cc0cd9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
12 changes: 11 additions & 1 deletion applicationServer/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions applicationServer/src/search/search.controller.ts
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;
}
}
12 changes: 12 additions & 0 deletions applicationServer/src/search/search.module.ts
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 {}
40 changes: 40 additions & 0 deletions applicationServer/src/search/search.service.ts
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;
});
}
}

0 comments on commit 8cc0cd9

Please sign in to comment.