-
Notifications
You must be signed in to change notification settings - Fork 1
[Feat] #660 앱잼팀 랭킹 API 구현 #664
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5ca3a40
[#660] feat: 앱잼 솝탬프 최근 인증한 팀 TOP 랭킹 반환 컨트롤러 구현
huncozyboy 899b9d3
[#660] feat: 앱잼 솝탬프 최근 인증한 팀 TOP 랭킹 조회 메서드 구현
huncozyboy 1328932
[#660] feat: 앱잼 솝탬프 최근 인증한 팀 TOP 랭킹 조회 매퍼 구현
huncozyboy 7d43241
[#660] mod: AppjamUser 언더스코어 사용해서 패키지명 변경
huncozyboy 5417160
[#660] mod: AppjamUser 언더스코어 사용해서 패키지명 변경
huncozyboy b4c2c3f
[#660] feat: 앱잼 솝탬프 최근 인증한 팀 TOP 랭킹 조회 dto 추가
huncozyboy cb825e2
[#660] feat: 앱잼 솝탬프 최근 인증한 팀 TOP 랭킹 조회 비즈니스 로직 구현
huncozyboy a2934b2
[#660] mod: 언더스코어 포함하지 않는 패키지명으로 재변경
huncozyboy eeb0a85
[#660] mod: 언더스코어 포함하지 않는 패키지명으로 변경
huncozyboy d1714ef
[#660] mod: AppjamRankFacade로 컨벤션 맞춰서 책임 분리
huncozyboy 15af122
[#660] mod: AppjamRankController로 컨벤션 맞춰서 책임 분리
huncozyboy cde497e
[#660] mod: AppjamRankCalculator 생성해서 랭킹 계산 책임 분리
huncozyboy 33ad2ea
[#660] feat: 앱잼 솝탬프 최근 인증한 팀 TOP 랭킹 dto 추가
huncozyboy ff4d142
[#660] mod: 오탈자 수정
huncozyboy 343a6d3
[#660] del: 미사용 클래스 삭제
huncozyboy 722540b
[#660] feat: 앱잼팀 오늘의 득점 랭킹 TOP10 조회하기 비즈니스 로직 구현
huncozyboy 46feaaa
[#660] feat: 앱잼팀 오늘의 득점 랭킹 TOP10 조회 파사드 구현
huncozyboy 9e4162a
[#660] feat: 앱잼팀 오늘의 득점 랭킹 TOP10 조회 컨트롤러 구현
huncozyboy aed427d
[#660] feat: 앱잼팀 오늘의 득점 랭킹 TOP10 조회 DTO 추가
huncozyboy 81687d9
[#660] mod: Map을 getRanking()로 한번에 생성, teamInfoByTeamNumber 제거
huncozyboy 1505ba1
[#660] fix: Timestamp 변환 에러 해결
huncozyboy e1d6601
[#660-1] mod: 앱잽팀 랭킹 리퀘스트로 size 입력받도록 수정
huncozyboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/main/java/org/sopt/app/application/appjamrank/AppjamRankCalculator.java
This file contains hidden or 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,150 @@ | ||
| package org.sopt.app.application.appjamrank; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.sopt.app.application.playground.dto.PlaygroundProfileInfo; | ||
| import org.sopt.app.common.exception.BadRequestException; | ||
| import org.sopt.app.common.response.ErrorCode; | ||
| import org.sopt.app.domain.entity.AppjamUser; | ||
| import org.sopt.app.domain.entity.soptamp.Stamp; | ||
| import org.sopt.app.domain.enums.TeamNumber; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RequiredArgsConstructor(access = AccessLevel.PUBLIC) | ||
| public class AppjamRankCalculator { | ||
|
|
||
| private static final int TODAY_TEAM_RANK_LIMIT = 10; | ||
|
|
||
| private final List<Stamp> latestStamps; | ||
| private final Map<Long, AppjamUser> uploaderAppjamUserByUserId; | ||
| private final Map<Long, PlaygroundProfileInfo.PlaygroundProfile> playgroundProfileByUserId; | ||
|
|
||
| public List<AppjamRankInfo.TeamRank> calculateRecentTeamRanks() { | ||
| return latestStamps.stream() | ||
| .map(stamp -> { | ||
| AppjamUser uploaderAppjamUser = Optional.ofNullable(uploaderAppjamUserByUserId.get(stamp.getUserId())) | ||
| .orElseThrow(() -> new BadRequestException(ErrorCode.USER_NOT_FOUND)); | ||
|
|
||
| PlaygroundProfileInfo.PlaygroundProfile playgroundProfile = | ||
| Optional.ofNullable(playgroundProfileByUserId.get(stamp.getUserId())) | ||
| .orElseThrow(() -> new BadRequestException(ErrorCode.USER_NOT_FOUND)); | ||
|
|
||
| TeamNumber teamNumber = uploaderAppjamUser.getTeamNumber(); | ||
|
|
||
| String firstImageUrl = Optional.ofNullable(stamp.getImages()) | ||
| .filter(images -> !images.isEmpty()) | ||
| .map(List::getFirst) | ||
| .orElse(""); | ||
|
|
||
| return AppjamRankInfo.TeamRank.of( | ||
| stamp, | ||
| firstImageUrl, | ||
| uploaderAppjamUser, | ||
| teamNumber, | ||
| playgroundProfile | ||
| ); | ||
| }) | ||
| .toList(); | ||
| } | ||
|
|
||
| public AppjamRankInfo.TodayTeamRankList calculateTodayTeamRanksTop10( | ||
| List<AppjamRankInfo.TodayRank> todayUserRanks, | ||
| Map<Long, Long> totalPointsByUserId, | ||
| List<AppjamUser> allAppjamUsers | ||
| ) { | ||
| Map<Long, AppjamRankInfo.TodayRank> todayRankByUserId = todayUserRanks.stream() | ||
| .collect(Collectors.toMap( | ||
| AppjamRankInfo.TodayRank::getUserId, | ||
| Function.identity(), | ||
| (existing, replacement) -> existing | ||
| )); | ||
|
|
||
| Map<TeamNumber, String> teamNameByTeamNumber = allAppjamUsers.stream() | ||
| .collect(Collectors.toMap( | ||
| AppjamUser::getTeamNumber, | ||
| AppjamUser::getTeamName, | ||
| (existing, replacement) -> existing | ||
| )); | ||
|
|
||
| Map<TeamNumber, List<AppjamUser>> membersByTeamNumber = allAppjamUsers.stream() | ||
| .collect(Collectors.groupingBy( | ||
| AppjamUser::getTeamNumber | ||
| )); | ||
|
|
||
| List<TeamAggregate> teamAggregates = membersByTeamNumber.entrySet().stream() | ||
| .map(entry -> aggregateTeam( | ||
| entry.getKey(), | ||
| entry.getValue(), | ||
| teamNameByTeamNumber.getOrDefault(entry.getKey(), ""), | ||
| todayRankByUserId, | ||
| totalPointsByUserId | ||
| )) | ||
| .sorted(Comparator | ||
| .comparingLong(TeamAggregate::todayPoints).reversed() | ||
| .thenComparing(TeamAggregate::firstCertifiedAtToday, Comparator.nullsLast(Comparator.naturalOrder())) | ||
| .thenComparing(TeamAggregate::teamNumber) | ||
| ) | ||
| .limit(TODAY_TEAM_RANK_LIMIT) | ||
| .toList(); | ||
|
|
||
| AtomicInteger rankCounter = new AtomicInteger(1); | ||
| List<AppjamRankInfo.TodayTeamRank> ranks = teamAggregates.stream() | ||
| .map(teamAggregate -> AppjamRankInfo.TodayTeamRank.of( | ||
| rankCounter.getAndIncrement(), | ||
| teamAggregate.teamNumber(), | ||
| teamAggregate.teamName(), | ||
| teamAggregate.todayPoints(), | ||
| teamAggregate.totalPoints() | ||
| )) | ||
| .toList(); | ||
|
|
||
| return AppjamRankInfo.TodayTeamRankList.of(ranks); | ||
| } | ||
|
|
||
| private TeamAggregate aggregateTeam( | ||
| TeamNumber teamNumber, | ||
| List<AppjamUser> teamMembers, | ||
| String teamName, | ||
| Map<Long, AppjamRankInfo.TodayRank> todayRankByUserId, | ||
| Map<Long, Long> totalPointsByUserId | ||
| ) { | ||
| long todayPointsSum = 0L; | ||
| long totalPointsSum = 0L; | ||
| LocalDateTime firstCertifiedAtToday = null; | ||
|
|
||
| for (AppjamUser teamMember : teamMembers) { | ||
| Long userId = teamMember.getUserId(); | ||
|
|
||
| AppjamRankInfo.TodayRank todayRank = todayRankByUserId.get(userId); | ||
| if (todayRank != null) { | ||
| todayPointsSum += todayRank.getTodayPoints(); | ||
|
|
||
| LocalDateTime certifiedAt = todayRank.getFirstCertifiedAtToday(); | ||
| if (certifiedAt != null && (firstCertifiedAtToday == null || certifiedAt.isBefore(firstCertifiedAtToday))) { | ||
| firstCertifiedAtToday = certifiedAt; | ||
| } | ||
| } | ||
|
|
||
| totalPointsSum += totalPointsByUserId.getOrDefault(userId, 0L); | ||
| } | ||
|
|
||
| return new TeamAggregate(teamNumber, teamName, todayPointsSum, totalPointsSum, firstCertifiedAtToday); | ||
| } | ||
|
|
||
| private record TeamAggregate( | ||
| TeamNumber teamNumber, | ||
| String teamName, | ||
| long todayPoints, | ||
| long totalPoints, | ||
| LocalDateTime firstCertifiedAtToday | ||
| ) {} | ||
| } | ||
147 changes: 147 additions & 0 deletions
147
src/main/java/org/sopt/app/application/appjamrank/AppjamRankInfo.java
This file contains hidden or 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,147 @@ | ||
| package org.sopt.app.application.appjamrank; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import org.sopt.app.application.playground.dto.PlaygroundProfileInfo; | ||
| import org.sopt.app.domain.entity.AppjamUser; | ||
| import org.sopt.app.domain.entity.soptamp.Stamp; | ||
| import org.sopt.app.domain.enums.TeamNumber; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.ToString; | ||
|
|
||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class AppjamRankInfo { | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public static class RankAggregate { | ||
|
|
||
| private final List<Stamp> latestStamps; | ||
| private final List<Long> uploaderUserIds; | ||
| private final Map<Long, AppjamUser> uploaderAppjamUserByUserId; | ||
|
|
||
| public static RankAggregate of( | ||
| List<Stamp> latestStamps, | ||
| List<Long> uploaderUserIds, | ||
| Map<Long, AppjamUser> uploaderAppjamUserByUserId | ||
| ) { | ||
| return RankAggregate.builder() | ||
| .latestStamps(latestStamps) | ||
| .uploaderUserIds(uploaderUserIds) | ||
| .uploaderAppjamUserByUserId(uploaderAppjamUserByUserId) | ||
| .build(); | ||
| } | ||
|
|
||
| public static RankAggregate empty() { | ||
| return RankAggregate.builder() | ||
| .latestStamps(List.of()) | ||
| .uploaderUserIds(List.of()) | ||
| .uploaderAppjamUserByUserId(Map.of()) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public static class TeamRank { | ||
| private final Long stampId; | ||
| private final Long missionId; | ||
| private final Long userId; | ||
| private final String imageUrl; | ||
| private final LocalDateTime createdAt; | ||
| private final String userName; | ||
| private final String userProfileImage; | ||
| private final String teamName; | ||
| private final TeamNumber teamNumber; | ||
|
|
||
| public static TeamRank of( | ||
| Stamp stamp, | ||
| String firstImageUrl, | ||
| AppjamUser uploaderAppjamUser, | ||
| TeamNumber teamNumber, | ||
| PlaygroundProfileInfo.PlaygroundProfile playgroundProfile | ||
| ) { | ||
| return TeamRank.builder() | ||
| .stampId(stamp.getId()) | ||
| .missionId(stamp.getMissionId()) | ||
| .userId(stamp.getUserId()) | ||
| .imageUrl(firstImageUrl) | ||
| .createdAt(stamp.getCreatedAt()) | ||
| .userName(playgroundProfile.getName()) | ||
| .userProfileImage(Optional.ofNullable(playgroundProfile.getProfileImage()).orElse("")) | ||
| .teamName(uploaderAppjamUser.getTeamName()) | ||
| .teamNumber(teamNumber) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @ToString | ||
| public static class TodayRank { | ||
| private final Long userId; | ||
| private final long todayPoints; | ||
| private final LocalDateTime firstCertifiedAtToday; | ||
|
|
||
| public static TodayRank of(Long userId, long todayPoints, LocalDateTime firstCertifiedAtToday) { | ||
| return TodayRank.builder() | ||
| .userId(userId) | ||
| .todayPoints(todayPoints) | ||
| .firstCertifiedAtToday(firstCertifiedAtToday) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public static class RankList { | ||
| private final List<TeamRank> ranks; | ||
|
|
||
| public static RankList of(List<TeamRank> ranks) { | ||
| return RankList.builder().ranks(ranks).build(); | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public static class TodayTeamRank { | ||
| private final int rank; | ||
| private final TeamNumber teamNumber; | ||
| private final String teamName; | ||
| private final long todayPoints; | ||
| private final long totalPoints; | ||
|
|
||
| public static TodayTeamRank of( | ||
| int rank, | ||
| TeamNumber teamNumber, | ||
| String teamName, | ||
| long todayPoints, | ||
| long totalPoints | ||
| ) { | ||
| return TodayTeamRank.builder() | ||
| .rank(rank) | ||
| .teamNumber(teamNumber) | ||
| .teamName(teamName) | ||
| .todayPoints(todayPoints) | ||
| .totalPoints(totalPoints) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public static class TodayTeamRankList { | ||
| private final List<TodayTeamRank> ranks; | ||
|
|
||
| public static TodayTeamRankList of(List<TodayTeamRank> ranks) { | ||
| return TodayTeamRankList.builder().ranks(ranks).build(); | ||
| } | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/sopt/app/application/appjamrank/AppjamRankService.java
This file contains hidden or 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,68 @@ | ||
| package org.sopt.app.application.appjamrank; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.sopt.app.domain.entity.AppjamUser; | ||
| import org.sopt.app.domain.entity.soptamp.Stamp; | ||
| import org.sopt.app.interfaces.postgres.AppjamUserRepository; | ||
| import org.sopt.app.interfaces.postgres.StampRepository; | ||
| import org.sopt.app.interfaces.postgres.StampRepositoryCustom; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AppjamRankService { | ||
|
|
||
| private static final int RECENT_RANK_LIMIT = 3; | ||
|
|
||
| private final StampRepository stampRepository; | ||
| private final AppjamUserRepository appjamUserRepository; | ||
|
|
||
| public AppjamRankInfo.RankAggregate findRecentTeamRanks() { | ||
| Pageable latestStampPageable = PageRequest.of(0, RECENT_RANK_LIMIT); | ||
|
|
||
| List<Stamp> latestStamps = stampRepository.findLatestStamps(latestStampPageable); | ||
| if (latestStamps.isEmpty()) { | ||
| return AppjamRankInfo.RankAggregate.empty(); | ||
| } | ||
|
|
||
| List<Long> uploaderUserIds = latestStamps.stream() | ||
| .map(Stamp::getUserId) | ||
| .distinct() | ||
| .toList(); | ||
|
|
||
| List<AppjamUser> uploaderAppjamUsers = appjamUserRepository.findAllByUserIdIn(uploaderUserIds); | ||
|
|
||
| Map<Long, AppjamUser> uploaderAppjamUserByUserId = uploaderAppjamUsers.stream() | ||
| .collect(Collectors.toMap( | ||
| AppjamUser::getUserId, | ||
| Function.identity(), | ||
| (existing, replacement) -> existing | ||
| )); | ||
|
|
||
| return AppjamRankInfo.RankAggregate.of( | ||
| latestStamps, | ||
| uploaderUserIds, | ||
| uploaderAppjamUserByUserId | ||
| ); | ||
| } | ||
|
||
|
|
||
| public List<StampRepositoryCustom.AppjamTodayRankSource> findTodayUserRankSources( | ||
| LocalDateTime todayStart, | ||
| LocalDateTime tomorrowStart | ||
| ) { | ||
| return stampRepository.findTodayUserRankSources(todayStart, tomorrowStart); | ||
| } | ||
|
|
||
| public List<AppjamUser> findAllAppjamUsers() { | ||
| return appjamUserRepository.findAll(); | ||
| } | ||
| } | ||
4 changes: 2 additions & 2 deletions
4
...n/app_service/AppServiceBadgeManager.java → ...on/appservice/AppServiceBadgeManager.java
This file contains hidden or 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
8 changes: 4 additions & 4 deletions
8
...n/app_service/AppServiceBadgeService.java → ...on/appservice/AppServiceBadgeService.java
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...plication/app_service/AppServiceName.java → ...pplication/appservice/AppServiceName.java
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
개인적인 생각으로 top3, top10 과 같은 값들을 상수로 관리하는 것보다 클라에서 유연하게 사용할 수 있도록 queryString으로 받는게 좋다고 생각해요. 어떻게 생각하세요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 해당 의견은 동의하긴합니다.
유동적으로 리퀘스트로 입력받으면, 기획쪽에서 변동 사항이 생겼을때도 유동적으로 대처할 수 있을거같네요 👍🏻