-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 랭킹 도메인 추가 * feat: 랭킹 갱신 * feat: 랭킹 갱신 푸시 알림 * fix: 갱신 시간 수정 * fix: 갱신 시간 수정 * fix: 랭킹 코드 정리 * fix: 랭킹 id name 지정 * fix: 랭킹 쿼리 수정 * fix: 갱신 시간 수정 * feat: 랭킹 등수 rank 추가 * fix: long casting 이슈 * fix: findAllMissionSymbolStack RankingService 변경 * fix: repository custom, impl 삭제 * test: 랭킹 테스트 코드 * test: 랭킹 테스트 코드 * fix: 안쓰는 로직 제거 * fix: spotlessApply
- Loading branch information
Showing
15 changed files
with
403 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/depromeet/domain/ranking/api/RankingController.java
This file contains 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,26 @@ | ||
package com.depromeet.domain.ranking.api; | ||
|
||
import com.depromeet.domain.ranking.application.RankingService; | ||
import com.depromeet.domain.ranking.dto.response.RankingResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "10. [랭킹]", description = "랭킹 관련 API") | ||
@RestController | ||
@RequestMapping("/ranking") | ||
@RequiredArgsConstructor | ||
public class RankingController { | ||
|
||
private final RankingService rankingService; | ||
|
||
@Operation(summary = "랭킹 조회", description = "랭킹을 조회합니다.") | ||
@GetMapping | ||
public List<RankingResponse> rankingFindAll() { | ||
return rankingService.findAllRanking(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/depromeet/domain/ranking/application/RankingService.java
This file contains 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,60 @@ | ||
package com.depromeet.domain.ranking.application; | ||
|
||
import com.depromeet.domain.mission.application.MissionService; | ||
import com.depromeet.domain.mission.dao.MissionRepository; | ||
import com.depromeet.domain.mission.domain.Mission; | ||
import com.depromeet.domain.missionRecord.domain.MissionRecord; | ||
import com.depromeet.domain.ranking.dao.RankingRepository; | ||
import com.depromeet.domain.ranking.domain.Ranking; | ||
import com.depromeet.domain.ranking.dto.RankingDto; | ||
import com.depromeet.domain.ranking.dto.response.RankingResponse; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class RankingService { | ||
|
||
private final RankingRepository rankingRepository; | ||
private final MissionRepository missionRepository; | ||
private final MissionService missionService; | ||
|
||
@Transactional(readOnly = true) | ||
public List<RankingResponse> findAllRanking() { | ||
List<Ranking> rankings = rankingRepository.findTop50ByOrderBySymbolStackDesc(); | ||
return IntStream.range(0, rankings.size()) | ||
.mapToObj(i -> RankingResponse.of(rankings.get(i), (long) i + 1)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public void updateSymbolStack(List<RankingDto> rankingDtos) { | ||
for (RankingDto rankingDto : rankingDtos) { | ||
Ranking ranking = Ranking.createRanking(rankingDto.symbolStack(), rankingDto.member()); | ||
rankingRepository.updateSymbolStackAndMemberId( | ||
ranking.getSymbolStack(), ranking.getMember().getId()); | ||
} | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<RankingDto> findAllMissionSymbolStack() { | ||
List<Mission> missions = missionRepository.findAllMissionWithRecords(); | ||
List<MissionRecord> completedMissionRecords = | ||
missionService.findCompletedMissionRecords(missions); | ||
|
||
return completedMissionRecords.stream() | ||
.collect(Collectors.groupingBy(MissionRecord::getMember)) | ||
.entrySet() | ||
.stream() | ||
.map( | ||
entry -> | ||
RankingDto.of( | ||
entry.getKey(), | ||
missionService.symbolStackCalculate(entry.getValue()))) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/depromeet/domain/ranking/dao/RankingRepository.java
This file contains 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,24 @@ | ||
package com.depromeet.domain.ranking.dao; | ||
|
||
import com.depromeet.domain.ranking.domain.Ranking; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface RankingRepository extends JpaRepository<Ranking, Long> { | ||
|
||
// 최대 50개의 랭킹을 조회한다. | ||
List<Ranking> findTop50ByOrderBySymbolStackDesc(); | ||
|
||
@Modifying | ||
@Query( | ||
value = | ||
"INSERT INTO Ranking (member_id, symbol_stack, created_at) " | ||
+ "VALUES (:memberId, :symbolStack, NOW()) " | ||
+ "ON DUPLICATE KEY UPDATE member_id = :memberId, symbol_stack = :symbolStack, updated_at = NOW()", | ||
nativeQuery = true) | ||
void updateSymbolStackAndMemberId( | ||
@Param("symbolStack") long symbolStack, @Param("memberId") Long memberId); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/depromeet/domain/ranking/domain/Ranking.java
This file contains 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,44 @@ | ||
package com.depromeet.domain.ranking.domain; | ||
|
||
import com.depromeet.domain.common.model.BaseTimeEntity; | ||
import com.depromeet.domain.member.domain.Member; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.OneToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.Comment; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Ranking extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "ranking_id") | ||
private Long id; | ||
|
||
@Comment("번개 스택") | ||
private Long symbolStack; | ||
|
||
@OneToOne | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
private Ranking(Long symbolStack, Member member) { | ||
this.symbolStack = symbolStack; | ||
this.member = member; | ||
} | ||
|
||
public static Ranking createRanking(Long symbolStack, Member member) { | ||
return Ranking.builder().symbolStack(symbolStack).member(member).build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/depromeet/domain/ranking/dto/RankingDto.java
This file contains 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,9 @@ | ||
package com.depromeet.domain.ranking.dto; | ||
|
||
import com.depromeet.domain.member.domain.Member; | ||
|
||
public record RankingDto(Member member, long symbolStack) { | ||
public static RankingDto of(Member member, long symbolStack) { | ||
return new RankingDto(member, symbolStack); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/depromeet/domain/ranking/dto/response/RankingResponse.java
This file contains 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,21 @@ | ||
package com.depromeet.domain.ranking.dto.response; | ||
|
||
import com.depromeet.domain.ranking.domain.Ranking; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
public record RankingResponse( | ||
@Schema(description = "사용자 ID", defaultValue = "1") Long memberId, | ||
@Schema(description = "랭킹", defaultValue = "1") long rank, | ||
@Schema(description = "번개 수", defaultValue = "1") long symbolStack, | ||
@Schema(description = "사용자 nickname", defaultValue = "default nickname") String nickname, | ||
@Schema(description = "프로필 이미지", defaultValue = "profile image url") | ||
String profileImageUrl) { | ||
public static RankingResponse of(Ranking ranking, long rank) { | ||
return new RankingResponse( | ||
ranking.getMember().getId(), | ||
rank, | ||
ranking.getSymbolStack(), | ||
ranking.getMember().getProfile().getNickname(), | ||
ranking.getMember().getProfile().getProfileImageUrl()); | ||
} | ||
} |
This file contains 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
This file contains 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.