-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor: 이번달 러닝 서머리 V2를 위한 코드 리팩터링(임시) - V2, V1 공통 리스펀스 DTO RunningRecordMonthlySummaryResponse로 지정(이번달, 이번달 달린 거리) - V1 리스펀스 추가 - 서비스 로직 변경 - 컨트롤러에서 값 가공후 V1으로 리턴 * Feat: running-records/monthly-summary의 V2 추가 - 현재 달리고 있는 지구 한바퀴 코스에 대한 퍼센테이지 값 조회하는 서비스단구현(임시) - 컨트롤러 생성 - ResponseDto V2 생성
- Loading branch information
Showing
8 changed files
with
230 additions
and
55 deletions.
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
84 changes: 84 additions & 0 deletions
84
src/main/java/com/dnd/runus/application/running/RunningRecordServiceV2.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,84 @@ | ||
package com.dnd.runus.application.running; | ||
|
||
import com.dnd.runus.domain.running.RunningRecordRepository; | ||
import com.dnd.runus.domain.scale.ScaleAchievementLog; | ||
import com.dnd.runus.domain.scale.ScaleAchievementRepository; | ||
import com.dnd.runus.presentation.v2.running.dto.response.RunningRecordMonthlySummaryResponseV2; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.text.DecimalFormat; | ||
import java.util.List; | ||
|
||
import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER; | ||
|
||
@Service | ||
public class RunningRecordServiceV2 { | ||
|
||
private final RunningRecordRepository runningRecordRepository; | ||
private final ScaleAchievementRepository scaleAchievementRepository; | ||
|
||
private static final DecimalFormat KILO_METER_UNDER_1_POINT_FORMATTER = new DecimalFormat("#,###.#km"); | ||
|
||
public RunningRecordServiceV2( | ||
RunningRecordRepository runningRecordRepository, ScaleAchievementRepository scaleAchievementRepository) { | ||
this.runningRecordRepository = runningRecordRepository; | ||
this.scaleAchievementRepository = scaleAchievementRepository; | ||
} | ||
|
||
// todo 지구 한바퀴 작업 후 리팩터링 예정 | ||
@Transactional(readOnly = true) | ||
public RunningRecordMonthlySummaryResponseV2.PercentageBoxWithMessage getPercentageValues(long memberId) { | ||
// 지구한바퀴 코스 조회 | ||
List<ScaleAchievementLog> scaleAchievementLogs = scaleAchievementRepository.findScaleAchievementLogs(memberId); | ||
ScaleAchievementLog currentCourseLog = scaleAchievementLogs.stream() | ||
.filter(log -> log.achievedDate() == null) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
// todo 정해지면 하드코드 수정 | ||
if (currentCourseLog == null) { | ||
return RunningRecordMonthlySummaryResponseV2.PercentageBoxWithMessage.builder() | ||
.message("축하합니다! 지구 한바퀴 완주하셨네요!") | ||
.percentageBox(RunningRecordMonthlySummaryResponseV2.PercentageBox.builder() | ||
.startName("한국") | ||
.endName("지구 한바퀴") | ||
.percentage(100) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
// 사용자가 달린 전체 거리 확인 | ||
int totalRunningDistanceMeter = runningRecordRepository.findTotalDistanceMeterByMemberId(memberId); | ||
// 성취한 코스의 전체 합 거리 | ||
int achievedCourseMeterSum = scaleAchievementLogs.stream() | ||
.filter(log -> log.achievedDate() != null) | ||
.mapToInt(log -> log.scale().sizeMeter()) | ||
.sum(); | ||
|
||
// 현재 코스에서 사용자가 성취한 거리 합 | ||
double currentCourseAchievedKmSum = | ||
(totalRunningDistanceMeter - achievedCourseMeterSum) / METERS_IN_A_KILOMETER; | ||
// 현재 코스 완주를 위해 필요한 키로미터 값 | ||
double courseSizeKm = currentCourseLog.scale().sizeMeter() / METERS_IN_A_KILOMETER; | ||
|
||
double percentage = calPercentage(courseSizeKm, currentCourseAchievedKmSum); | ||
|
||
return RunningRecordMonthlySummaryResponseV2.PercentageBoxWithMessage.builder() | ||
.message(String.format( | ||
"%s까지 %s 남았어요!", | ||
currentCourseLog.scale().endName(), | ||
KILO_METER_UNDER_1_POINT_FORMATTER.format(courseSizeKm - currentCourseAchievedKmSum))) | ||
.percentageBox(RunningRecordMonthlySummaryResponseV2.PercentageBox.builder() | ||
.startName(currentCourseLog.scale().startName()) | ||
.endName(currentCourseLog.scale().endName()) | ||
.percentage(percentage) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
private double calPercentage(double totalRangeValue, double currentValue) { | ||
if (totalRangeValue <= 0) return 0; | ||
return currentValue / totalRangeValue; | ||
} | ||
} |
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
21 changes: 5 additions & 16 deletions
21
...m/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponse.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 |
---|---|---|
@@ -1,24 +1,13 @@ | ||
package com.dnd.runus.presentation.v1.running.dto.response; | ||
|
||
import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
import java.text.DecimalFormat; | ||
import lombok.Builder; | ||
|
||
//todo API 버저닝 관련 구조 정해지면 패키지 변경 예쩡 | ||
@Builder | ||
public record RunningRecordMonthlySummaryResponse( | ||
@Schema(description = "이번 달", example = "8월") | ||
String month, | ||
@Schema(description = "이번 달에 달린 키로 수", example = "2.55km") | ||
String monthlyKm, | ||
@Schema(description = "다음 레벨", example = "Level 2") | ||
String nextLevelName, | ||
@Schema(description = "다음 레벨까지 남은 키로 수", example = "2.55km") | ||
String nextLevelKm | ||
int month, | ||
int monthlyTotalMeter | ||
) { | ||
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("0.##km"); | ||
|
||
public RunningRecordMonthlySummaryResponse(int monthValue, int monthlyTotalMeter, String nextLevelName, String nextLevelKm) { | ||
this(monthValue + "월", KILO_METER_FORMATTER.format(monthlyTotalMeter / METERS_IN_A_KILOMETER), nextLevelName, nextLevelKm); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponseV1.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,23 @@ | ||
package com.dnd.runus.presentation.v1.running.dto.response; | ||
|
||
import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.text.DecimalFormat; | ||
|
||
public record RunningRecordMonthlySummaryResponseV1( | ||
@Schema(description = "이번 달", example = "8월") | ||
String month, | ||
@Schema(description = "이번 달에 달린 키로 수", example = "2.55km") | ||
String monthlyKm, | ||
@Schema(description = "다음 레벨", example = "Level 2") | ||
String nextLevelName, | ||
@Schema(description = "다음 레벨까지 남은 키로 수", example = "2.55km") | ||
String nextLevelKm | ||
) { | ||
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("0.##km"); | ||
|
||
public RunningRecordMonthlySummaryResponseV1(int monthValue, int monthlyTotalMeter, String nextLevelName, String nextLevelKm) { | ||
this(monthValue + "월", KILO_METER_FORMATTER.format(monthlyTotalMeter / METERS_IN_A_KILOMETER), nextLevelName, nextLevelKm); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/dnd/runus/presentation/v2/running/RunningRecordControllerV2.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,36 @@ | ||
package com.dnd.runus.presentation.v2.running; | ||
|
||
import com.dnd.runus.application.running.RunningRecordService; | ||
import com.dnd.runus.application.running.RunningRecordServiceV2; | ||
import com.dnd.runus.presentation.annotation.MemberId; | ||
import com.dnd.runus.presentation.v1.running.dto.response.RunningRecordMonthlySummaryResponse; | ||
import com.dnd.runus.presentation.v2.running.dto.response.RunningRecordMonthlySummaryResponseV2; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v2/running-records") | ||
public class RunningRecordControllerV2 { | ||
private final RunningRecordServiceV2 runningRecordService2; | ||
private final RunningRecordService runningRecordService; | ||
|
||
@Operation(summary = "이번 달 러닝 기록 조회(홈화면) V2", description = """ | ||
홈화면의 이번 달 러닝 기록을 조회 합니다.<br> | ||
""") | ||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("monthly-summary") | ||
public RunningRecordMonthlySummaryResponseV2 getMonthlyRunningSummary(@MemberId long memberId) { | ||
RunningRecordMonthlySummaryResponse monthlyRunningSummery = | ||
runningRecordService.getMonthlyRunningSummery(memberId); | ||
return new RunningRecordMonthlySummaryResponseV2( | ||
monthlyRunningSummery.month(), | ||
monthlyRunningSummery.monthlyTotalMeter(), | ||
runningRecordService2.getPercentageValues(memberId)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...dnd/runus/presentation/v2/running/dto/response/RunningRecordMonthlySummaryResponseV2.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,59 @@ | ||
package com.dnd.runus.presentation.v2.running.dto.response; | ||
|
||
|
||
import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.text.DecimalFormat; | ||
import lombok.Builder; | ||
|
||
|
||
public record RunningRecordMonthlySummaryResponseV2( | ||
@Schema(description = "이번 달", example = "8월") | ||
String month, | ||
@Schema(description = "이번 달에 달린 키로 수", example = "2.55km") | ||
String monthlyKm, | ||
@Schema(description = "서브 메세지", example = "대전까지 00km 남았어요!") | ||
String message, | ||
@Schema(description = "퍼센테이지 시작 위치 이름", example = "인천") | ||
String startName, | ||
@Schema(description = "퍼센테이지 종료 위치 이름", example = "대전") | ||
String endName, | ||
@Schema(description = "퍼센테이지값", example = "0.728") | ||
double percentage | ||
) { | ||
|
||
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("#,###.##km"); | ||
|
||
public RunningRecordMonthlySummaryResponseV2( | ||
int monthValue, | ||
int monthlyTotalMeter, | ||
PercentageBoxWithMessage percentageBoxWithMessage) { | ||
this( | ||
monthValue + "월", | ||
KILO_METER_FORMATTER.format(monthlyTotalMeter / METERS_IN_A_KILOMETER), | ||
percentageBoxWithMessage.message, | ||
percentageBoxWithMessage.percentageBox.startName, | ||
percentageBoxWithMessage.percentageBox.endName, | ||
percentageBoxWithMessage.percentageBox.percentage | ||
); | ||
} | ||
|
||
@Schema(name = "RunningRecordMonthlySummaryResponseV2 PercentageBox", | ||
description = "이번달 러닝 서머리 퍼센테이지 관련 값") | ||
@Builder | ||
public record PercentageBox( | ||
String startName, | ||
String endName, | ||
double percentage | ||
) { | ||
} | ||
|
||
@Builder | ||
public record PercentageBoxWithMessage( | ||
String message, | ||
RunningRecordMonthlySummaryResponseV2.PercentageBox percentageBox | ||
) { | ||
} | ||
|
||
} |
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