Skip to content
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

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion applicationServer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"^@naver/(.*)$": "<rootDir>/src/auth/naver/$1",
"^@cookie/(.*)$": "<rootDir>/src/auth/cookie/$1",
"^@authUtil/(.*)$": "<rootDir>/src/auth/util/$1",
"^@follow/(.*)$": "<rootDir>/src/follow/$1"
"^@follow/(.*)$": "<rootDir>/src/follow/$1",
"^@search/(.*)$": "<rootDir>/src/search/$1"
}
}
}
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

점점 늘어가는 모듈들,,,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅠㅠ

],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
Expand Down
1 change: 1 addition & 0 deletions applicationServer/src/live/live.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import { AuthModule } from '@src/auth/core/auth.module';
imports: [MemberModule, AuthModule],
controllers: [LiveController],
providers: [LiveService],
exports: [LiveService],
})
export class LiveModule {}
16 changes: 11 additions & 5 deletions applicationServer/src/member/member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ class MemberService {
private memberRepository: Repository<Member>,
) {}

async findMembers() {
findMembers() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async를 빼신 이유가 있으실까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 });
}

Expand All @@ -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,
Expand All @@ -59,7 +65,7 @@ class MemberService {
return name;
}

async refreshStreamKey(id) {
refreshStreamKey(id) {
const newStreamKey = crypto.randomUUID();
this.memberRepository.update(id, { stream_key: newStreamKey });

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/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(' ', '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쿼리에서 공백을 없애는 이유가 있으신가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

검색 효율성을 위해 공백을 제거한 문자열 매칭으로 탐색하기 때문에 검색어에서도 공백을 제거했습니다.

const searchResult = {
lives: [],
members: [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 on/off 같이 변수명을 더 직관적이게 만들면 좋을 것 같아요.
하지만 오전에 FE분들이랑 이미 명세에 관해서 얘기 나눠서 괜찮을 것 같네요!

};
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) => {
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아까 개발일지를 봤었는데, kmp 알고리즘과 정규표현식의 비교를 통해 정규표현식으로 결정하셨네요!
비교까지 👍🏻👍🏻 좋습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
});
}
}
3 changes: 2 additions & 1 deletion applicationServer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"@member/*": ["src/member/*"],
"@live/*": ["src/live/*"],
"@follow/*": ["src/follow/*"],
"@google/*": ["src/auth/google/*"]
"@google/*": ["src/auth/google/*"],
"@search/*": ["src/search/*"]
},
"incremental": true,
"skipLibCheck": true,
Expand Down
Loading