Skip to content

Commit

Permalink
mod:: hot club 좋아요 수 동일한 경우 처리 로직 추가
Browse files Browse the repository at this point in the history
- 좋아요 수가 동일한 경우 id 순으로 선택하는 것에서 랜덤으로 선택하도록 변경
- 추가로 hot club 비어있는 경우 예외처리 추가
  • Loading branch information
JeongYeonSeung committed Jul 19, 2024
1 parent 0784d5b commit f69d8bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/home/club/club-like.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export class ClubLikeRepository extends Repository<ClubLikeEntity> {
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);

// 일주일 간 개수가 가장 많은 동아리 4개 반환
// 일주일 간 좋아요 개수가 가장 많은 동아리 4개 반환, 좋아요 개수가 같은 경우 랜덤 선택
const topClubLikes = await this.createQueryBuilder('club_like')
.select('club_like.clubId')
.addSelect('COUNT(club_like.id)', 'likeCount')
.where('club_like.createdAt >= :oneWeekAgo', { oneWeekAgo })
.groupBy('club_like.clubId')
.orderBy('likeCount', 'DESC')
.addOrderBy('RAND()')
.limit(4)
.getRawMany();

Expand Down
16 changes: 16 additions & 0 deletions src/home/club/club.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export class ClubRepository extends Repository<ClubEntity> {
}

async findClubsByIdOrder(clubIds: number[]): Promise<ClubEntity[]> {
// hot club이 아직 없는 경우 빈 배열 반환
if (clubIds.length === 0) {
return [];
}
// 받은 id 순서대로 clubEntity를 찾아서 반환
const clubs = await this.createQueryBuilder('club')
.where('club.id IN (:...ids)', { ids: clubIds })
.orderBy(`FIELD(club.id, ${clubIds.join(',')})`)
Expand All @@ -44,7 +49,17 @@ export class ClubRepository extends Repository<ClubEntity> {
return clubs;
}

async findClubsByAllLikesAndRandom(): Promise<ClubEntity[]> {
// allLikes 순서대로 4개 반환, allLikes 같은 경우 랜덤으로 선택
return await this.createQueryBuilder('club')
.orderBy('club.allLikes', 'DESC')
.addOrderBy('RAND()')
.limit(4)
.getMany();
}

async findClubsByRandom(): Promise<ClubEntity[]> {
// 랜덤 4개 반환
return await this.createQueryBuilder('club')
.orderBy('RAND()')
.limit(4)
Expand All @@ -55,6 +70,7 @@ export class ClubRepository extends Repository<ClubEntity> {
category: string,
limit: number,
): Promise<ClubEntity[]> {
// 카테고리에 속한 club 랜덤하게 limit 만큼 선택하여 반환
return await this.createQueryBuilder('club')
.where('club.category = :category', { category })
.orderBy('RAND()')
Expand Down
7 changes: 2 additions & 5 deletions src/home/club/club.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,9 @@ export class ClubService {
const hotClubIds = topLikedClubsInfo.map((info) => info.clubId);
const hotClubs = await this.clubRepository.findClubsByIdOrder(hotClubIds);

// hotClubs의 개수가 4개 미만인 경우, 전체 개수 기준으로 높은 것부터 선택하여 부족한 수를 채움
// hotClubs의 개수가 4개 미만인 경우, 전체 좋아요 개수 기준으로 높은 것부터 선택(좋아요 개수 같은 경우 랜덤 선택)하여 부족한 수를 채움
const additionalClubsNeeded = 4 - hotClubs.length;
const allClubs = await this.clubRepository.find({
take: 4,
order: { allLikes: 'DESC' },
});
const allClubs = await this.clubRepository.findClubsByAllLikesAndRandom();

// 전체 찜 개수 기준으로 가져온 동아리 중 hotClubs내에 이미 포함된 경우 제거
const existingClubIds = new Set(hotClubs.map((hc) => hc.id));
Expand Down

0 comments on commit f69d8bb

Please sign in to comment.