From 8660eec223aa1a7f77510c6b4d154d6f7c2ac7f5 Mon Sep 17 00:00:00 2001 From: hee9841 Date: Thu, 7 Nov 2024 21:08:15 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=EC=9D=B4=EB=B2=88=EB=8B=AC=20?= =?UTF-8?q?=EB=9F=AC=EB=8B=9D=20=EC=84=9C=EB=A8=B8=EB=A6=AC=20V2=EB=A5=BC?= =?UTF-8?q?=20=EC=9C=84=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=84=B0=EB=A7=81(=EC=9E=84=EC=8B=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - V2, V1 공통 리스펀스 DTO RunningRecordMonthlySummaryResponse로 지정(이번달, 이번달 달린 거리) - V1 리스펀스 추가 - 서비스 로직 변경 - 컨트롤러에서 값 가공후 V1으로 리턴 --- .../running/RunningRecordService.java | 27 ++++--------------- .../v1/running/RunningRecordController.java | 17 ++++++++++-- .../RunningRecordMonthlySummaryResponse.java | 21 ++++----------- ...RunningRecordMonthlySummaryResponseV1.java | 23 ++++++++++++++++ .../running/RunningRecordServiceTest.java | 18 +++---------- 5 files changed, 51 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponseV1.java diff --git a/src/main/java/com/dnd/runus/application/running/RunningRecordService.java b/src/main/java/com/dnd/runus/application/running/RunningRecordService.java index 4e8bdbce..bc01644f 100644 --- a/src/main/java/com/dnd/runus/application/running/RunningRecordService.java +++ b/src/main/java/com/dnd/runus/application/running/RunningRecordService.java @@ -13,10 +13,7 @@ import com.dnd.runus.domain.common.Pace; import com.dnd.runus.domain.goalAchievement.GoalAchievement; import com.dnd.runus.domain.goalAchievement.GoalAchievementRepository; -import com.dnd.runus.domain.level.Level; import com.dnd.runus.domain.member.Member; -import com.dnd.runus.domain.member.MemberLevel; -import com.dnd.runus.domain.member.MemberLevelRepository; import com.dnd.runus.domain.member.MemberRepository; import com.dnd.runus.domain.running.DailyRunningRecordSummary; import com.dnd.runus.domain.running.RunningRecord; @@ -47,7 +44,6 @@ public class RunningRecordService { private final RunningRecordRepository runningRecordRepository; private final MemberRepository memberRepository; - private final MemberLevelRepository memberLevelRepository; private final ChallengeRepository challengeRepository; private final ChallengeAchievementRepository challengeAchievementRepository; private final ChallengeAchievementPercentageRepository percentageValuesRepository; @@ -61,7 +57,6 @@ public class RunningRecordService { public RunningRecordService( RunningRecordRepository runningRecordRepository, MemberRepository memberRepository, - MemberLevelRepository memberLevelRepository, ChallengeRepository challengeRepository, ChallengeAchievementRepository challengeAchievementRepository, ChallengeAchievementPercentageRepository percentageValuesRepository, @@ -70,7 +65,6 @@ public RunningRecordService( @Value("${app.default-zone-offset}") ZoneOffset defaultZoneOffset) { this.runningRecordRepository = runningRecordRepository; this.memberRepository = memberRepository; - this.memberLevelRepository = memberLevelRepository; this.challengeRepository = challengeRepository; this.challengeAchievementRepository = challengeAchievementRepository; this.percentageValuesRepository = percentageValuesRepository; @@ -223,22 +217,11 @@ public RunningRecordMonthlySummaryResponse getMonthlyRunningSummery(long memberI OffsetDateTime startDateOfMonth = OffsetDateTime.now(ZoneId.of(SERVER_TIMEZONE)).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS); OffsetDateTime startDateOfNextMonth = startDateOfMonth.plusMonths(1); - - int monthValue = startDateOfMonth.getMonthValue(); - - int monthlyTotalDistance = runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate( - memberId, startDateOfMonth, startDateOfNextMonth); - - MemberLevel.Current currentMemberLevel = memberLevelRepository.findByMemberIdWithLevel(memberId); - - long nextLevel = currentMemberLevel.level().levelId() + 1; - int remainingKmToNextLevel = currentMemberLevel.level().expRangeEnd() - currentMemberLevel.currentExp(); - - return new RunningRecordMonthlySummaryResponse( - monthValue, - monthlyTotalDistance, - Level.formatLevelName(nextLevel), - Level.formatExp(remainingKmToNextLevel)); + return RunningRecordMonthlySummaryResponse.builder() + .month(startDateOfMonth.getMonthValue()) + .monthlyTotalMeter(runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate( + memberId, startDateOfMonth, startDateOfNextMonth)) + .build(); } private ChallengeAchievement handleChallengeMode(Long challengeId, long memberId, RunningRecord runningRecord) { diff --git a/src/main/java/com/dnd/runus/presentation/v1/running/RunningRecordController.java b/src/main/java/com/dnd/runus/presentation/v1/running/RunningRecordController.java index 11a3b600..d1f02ffe 100644 --- a/src/main/java/com/dnd/runus/presentation/v1/running/RunningRecordController.java +++ b/src/main/java/com/dnd/runus/presentation/v1/running/RunningRecordController.java @@ -1,9 +1,12 @@ package com.dnd.runus.presentation.v1.running; +import com.dnd.runus.application.member.MemberService; import com.dnd.runus.application.running.RunningRecordService; +import com.dnd.runus.domain.level.Level; import com.dnd.runus.global.exception.type.ApiErrorType; import com.dnd.runus.global.exception.type.ErrorType; import com.dnd.runus.presentation.annotation.MemberId; +import com.dnd.runus.presentation.v1.member.dto.response.MyProfileResponse; import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordRequest; import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordWeeklySummaryType; import com.dnd.runus.presentation.v1.running.dto.response.*; @@ -23,6 +26,7 @@ @RequestMapping("/api/v1/running-records") public class RunningRecordController { private final RunningRecordService runningRecordService; + private final MemberService memberService; @GetMapping("/{runningRecordId}") @Operation(summary = "러닝 기록 상세 조회", description = "RunngingRecord id로 러닝 상세 기록을 조회합니다.") @@ -76,8 +80,17 @@ public RunningRecordAddResultResponse addRunningRecord( """) @ResponseStatus(HttpStatus.OK) @GetMapping("monthly-summary") - public RunningRecordMonthlySummaryResponse getMonthlyRunningSummary(@MemberId long memberId) { - return runningRecordService.getMonthlyRunningSummery(memberId); + public RunningRecordMonthlySummaryResponseV1 getMonthlyRunningSummary(@MemberId long memberId) { + RunningRecordMonthlySummaryResponse monthlyRunningSummery = + runningRecordService.getMonthlyRunningSummery(memberId); + + MyProfileResponse myProfile = memberService.getMyProfile(memberId); + + return new RunningRecordMonthlySummaryResponseV1( + monthlyRunningSummery.month(), + monthlyRunningSummery.monthlyTotalMeter(), + Level.formatLevelName(myProfile.nextLevel()), + Level.formatExp(myProfile.nextLevelEndExpMeter() - myProfile.currentExpMeter())); } @Operation( diff --git a/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponse.java b/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponse.java index 29d794e0..d5aaed63 100644 --- a/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponse.java +++ b/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponse.java @@ -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); - } } diff --git a/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponseV1.java b/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponseV1.java new file mode 100644 index 00000000..b19f2a86 --- /dev/null +++ b/src/main/java/com/dnd/runus/presentation/v1/running/dto/response/RunningRecordMonthlySummaryResponseV1.java @@ -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); + } +} diff --git a/src/test/java/com/dnd/runus/application/running/RunningRecordServiceTest.java b/src/test/java/com/dnd/runus/application/running/RunningRecordServiceTest.java index bc8a4eff..96c8c891 100644 --- a/src/test/java/com/dnd/runus/application/running/RunningRecordServiceTest.java +++ b/src/test/java/com/dnd/runus/application/running/RunningRecordServiceTest.java @@ -8,10 +8,7 @@ import com.dnd.runus.domain.common.Pace; import com.dnd.runus.domain.goalAchievement.GoalAchievement; import com.dnd.runus.domain.goalAchievement.GoalAchievementRepository; -import com.dnd.runus.domain.level.Level; import com.dnd.runus.domain.member.Member; -import com.dnd.runus.domain.member.MemberLevel; -import com.dnd.runus.domain.member.MemberLevelRepository; import com.dnd.runus.domain.member.MemberRepository; import com.dnd.runus.domain.running.DailyRunningRecordSummary; import com.dnd.runus.domain.running.RunningRecord; @@ -60,9 +57,6 @@ class RunningRecordServiceTest { @Mock private MemberRepository memberRepository; - @Mock - private MemberLevelRepository memberLevelRepository; - @Mock private ChallengeRepository challengeRepository; @@ -85,7 +79,6 @@ void setUp() { runningRecordService = new RunningRecordService( runningRecordRepository, memberRepository, - memberLevelRepository, challengeRepository, challengeAchievementRepository, percentageValuesRepository, @@ -416,7 +409,7 @@ void addRunningRecord_check_cal_pace() { } @Test - @DisplayName("이번 달, 달린 키로 수, 러닝 레벨을 조회한다.") + @DisplayName("이번 달, 달린 키로수를 조회한다.") void getMonthlyRunningSummery() { // given long memberId = 1; @@ -430,19 +423,14 @@ void getMonthlyRunningSummery() { given(runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate(eq(memberId), any(), any())) .willReturn(45_780); - given(memberLevelRepository.findByMemberIdWithLevel(memberId)) - .willReturn(new MemberLevel.Current(new Level(1, 0, 50_000, "image"), 45_780)); - // when RunningRecordMonthlySummaryResponse monthlyRunningSummery = runningRecordService.getMonthlyRunningSummery(memberId); // then assertNotNull(monthlyRunningSummery); - assertThat(monthlyRunningSummery.month()).isEqualTo("1월"); - assertThat(monthlyRunningSummery.monthlyKm()).isEqualTo("45.78km"); - assertThat(monthlyRunningSummery.nextLevelName()).isEqualTo("Level 2"); - assertThat(monthlyRunningSummery.nextLevelKm()).isEqualTo("4.22km"); + assertThat(monthlyRunningSummery.month()).isEqualTo(1); + assertThat(monthlyRunningSummery.monthlyTotalMeter()).isEqualTo(45_780); } }