Skip to content
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

Refactor: 러닝 결과 추가 관련 함수 및 클래스 이름 변경(기존 V1) #321

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.dnd.runus.domain.running.RunningRecordRepository;
import com.dnd.runus.global.exception.NotFoundException;
import com.dnd.runus.presentation.v1.running.dto.WeeklyRunningRatingDto;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordRequest;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordRequestV1;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordWeeklySummaryType;
import com.dnd.runus.presentation.v1.running.dto.response.*;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -167,8 +167,21 @@ public RunningRecordWeeklySummaryResponse getWeeklySummary(
avgValue / conversionFactor);
}

@Transactional(readOnly = true)
public RunningRecordMonthlySummaryResponse getMonthlyRunningSummery(long memberId) {

OffsetDateTime startDateOfMonth =
OffsetDateTime.now(ZoneId.of(SERVER_TIMEZONE)).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
OffsetDateTime startDateOfNextMonth = startDateOfMonth.plusMonths(1);
return RunningRecordMonthlySummaryResponse.builder()
.month(startDateOfMonth.getMonthValue())
.monthlyTotalMeter(runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate(
memberId, startDateOfMonth, startDateOfNextMonth))
.build();
}

@Transactional
public RunningRecordAddResultResponse addRunningRecord(long memberId, RunningRecordRequest request) {
public RunningRecordAddResultResponseV1 addRunningRecordV1(long memberId, RunningRecordRequestV1 request) {
Member member =
memberRepository.findById(memberId).orElseThrow(() -> new NotFoundException(Member.class, memberId));

Expand Down Expand Up @@ -201,27 +214,14 @@ public RunningRecordAddResultResponse addRunningRecord(long memberId, RunningRec
case CHALLENGE -> {
ChallengeAchievement challengeAchievement =
handleChallengeMode(request.challengeId(), memberId, record);
return RunningRecordAddResultResponse.of(record, challengeAchievement);
return RunningRecordAddResultResponseV1.of(record, challengeAchievement);
}
case GOAL -> {
GoalAchievement goalAchievement = handleGoalMode(record, request.goalDistance(), request.goalTime());
return RunningRecordAddResultResponse.of(record, goalAchievement);
return RunningRecordAddResultResponseV1.of(record, goalAchievement);
}
}
return RunningRecordAddResultResponse.from(record);
}

@Transactional(readOnly = true)
public RunningRecordMonthlySummaryResponse getMonthlyRunningSummery(long memberId) {

OffsetDateTime startDateOfMonth =
OffsetDateTime.now(ZoneId.of(SERVER_TIMEZONE)).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
OffsetDateTime startDateOfNextMonth = startDateOfMonth.plusMonths(1);
return RunningRecordMonthlySummaryResponse.builder()
.month(startDateOfMonth.getMonthValue())
.monthlyTotalMeter(runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate(
memberId, startDateOfMonth, startDateOfNextMonth))
.build();
return RunningRecordAddResultResponseV1.from(record);
}

private ChallengeAchievement handleChallengeMode(Long challengeId, long memberId, RunningRecord runningRecord) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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.RunningRecordRequestV1;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordWeeklySummaryType;
import com.dnd.runus.presentation.v1.running.dto.response.*;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -66,9 +66,9 @@ public RunningRecordSummaryResponse getRunningRecordSummaries(
})
@PostMapping
@ResponseStatus(HttpStatus.OK)
public RunningRecordAddResultResponse addRunningRecord(
@MemberId long memberId, @Valid @RequestBody RunningRecordRequest request) {
return runningRecordService.addRunningRecord(memberId, request);
public RunningRecordAddResultResponseV1 addRunningRecord(
@MemberId long memberId, @Valid @RequestBody RunningRecordRequestV1 request) {
return runningRecordService.addRunningRecordV1(memberId, request);
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.time.LocalDateTime;

public record RunningRecordRequest(
public record RunningRecordRequestV1(
@NotNull
LocalDateTime startAt,
@NotNull
Expand All @@ -36,7 +36,7 @@ public record RunningRecordRequest(
@NotNull
RunningRecordMetricsForAddDto runningData
) {
public RunningRecordRequest {
public RunningRecordRequestV1 {
if (startAt.isAfter(endAt)) {
throw new BusinessException(ErrorType.START_AFTER_END, startAt + " ~ " + endAt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.time.LocalDateTime;

public record RunningRecordAddResultResponse(
public record RunningRecordAddResultResponseV1(
long runningRecordId,
@Schema(description = "러닝 시작 시간")
LocalDateTime startAt,
Expand All @@ -32,11 +32,11 @@ public record RunningRecordAddResultResponse(
@NotNull
RunningRecordMetricsDto runningData
) {
public static RunningRecordAddResultResponse from(RunningRecord runningRecord) {
public static RunningRecordAddResultResponseV1 from(RunningRecord runningRecord) {
return buildResponse(runningRecord, null, null, RunningAchievementMode.NORMAL);
}

public static RunningRecordAddResultResponse of(RunningRecord runningRecord, ChallengeAchievement achievement) {
public static RunningRecordAddResultResponseV1 of(RunningRecord runningRecord, ChallengeAchievement achievement) {
return buildResponse(runningRecord,
new ChallengeDto(
achievement.challenge().challengeId(),
Expand All @@ -50,7 +50,7 @@ public static RunningRecordAddResultResponse of(RunningRecord runningRecord, Cha
);
}

public static RunningRecordAddResultResponse of(RunningRecord runningRecord, GoalAchievement achievement) {
public static RunningRecordAddResultResponseV1 of(RunningRecord runningRecord, GoalAchievement achievement) {
return buildResponse(runningRecord,
null,
new GoalResultDto(
Expand All @@ -63,8 +63,8 @@ public static RunningRecordAddResultResponse of(RunningRecord runningRecord, Goa
);
}

private static RunningRecordAddResultResponse buildResponse(RunningRecord runningRecord, ChallengeDto challenge, GoalResultDto goal, RunningAchievementMode achievementMode) {
return new RunningRecordAddResultResponse(
private static RunningRecordAddResultResponseV1 buildResponse(RunningRecord runningRecord, ChallengeDto challenge, GoalResultDto goal, RunningAchievementMode achievementMode) {
return new RunningRecordAddResultResponseV1(
runningRecord.runningId(),
runningRecord.startAt().toLocalDateTime(),
runningRecord.endAt().toLocalDateTime(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import com.dnd.runus.presentation.v1.running.dto.RunningRecordMetricsForAddDto;
import com.dnd.runus.presentation.v1.running.dto.WeeklyRunningRatingDto;
import com.dnd.runus.presentation.v1.running.dto.request.RunningAchievementMode;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordRequest;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordRequestV1;
import com.dnd.runus.presentation.v1.running.dto.request.RunningRecordWeeklySummaryType;
import com.dnd.runus.presentation.v1.running.dto.response.RunningRecordAddResultResponse;
import com.dnd.runus.presentation.v1.running.dto.response.RunningRecordAddResultResponseV1;
import com.dnd.runus.presentation.v1.running.dto.response.RunningRecordMonthlySummaryResponse;
import com.dnd.runus.presentation.v1.running.dto.response.RunningRecordQueryResponse;
import com.dnd.runus.presentation.v1.running.dto.response.RunningRecordWeeklySummaryResponse;
Expand Down Expand Up @@ -188,7 +188,7 @@ void getRunningRecord_challenge() {
@DisplayName("CHALLENGE 모드의 러닝 기록 추가 요청시, challengeId에 해당하는 챌린지가 있을 경우, 정상적으로 러닝 기록이 추가된다.")
void addRunningRecord_challenge() {
// given
RunningRecordRequest request = new RunningRecordRequest(
RunningRecordRequestV1 request = new RunningRecordRequestV1(
LocalDateTime.of(2021, 1, 1, 12, 10, 30),
LocalDateTime.of(2021, 1, 1, 13, 12, 10),
"start location",
Expand Down Expand Up @@ -217,7 +217,7 @@ void addRunningRecord_challenge() {
given(challengeAchievementRepository.save(challengeAchievement)).willReturn(challengeAchievement);

// when
RunningRecordAddResultResponse response = runningRecordService.addRunningRecord(1L, request);
RunningRecordAddResultResponseV1 response = runningRecordService.addRunningRecordV1(1L, request);

// then
assertEquals(request.startAt(), response.startAt());
Expand All @@ -228,7 +228,7 @@ void addRunningRecord_challenge() {
@DisplayName("GOAL 모드의 러닝 기록 추가 시,goalTime이 null이 아니고 목표값보다 실제값이 더 크다면, 성공한 goalAchievement와 함께 정상적으로 러닝 기록이 추가된다.")
void addRunningRecord_goal_time_success() {
// given
RunningRecordRequest request = new RunningRecordRequest(
RunningRecordRequestV1 request = new RunningRecordRequestV1(
LocalDateTime.of(2021, 1, 1, 12, 10, 30),
LocalDateTime.of(2021, 1, 1, 13, 12, 10),
"start location",
Expand All @@ -250,7 +250,7 @@ void addRunningRecord_goal_time_success() {
given(goalAchievementRepository.save(goalAchievement)).willReturn(goalAchievement);

// when
RunningRecordAddResultResponse response = runningRecordService.addRunningRecord(1L, request);
RunningRecordAddResultResponseV1 response = runningRecordService.addRunningRecordV1(1L, request);

// then
assertEquals(request.startAt(), response.startAt());
Expand All @@ -267,7 +267,7 @@ void addRunningRecord_goal_time_success() {
"GOAL 모드의 러닝 기록 추가 시, goalTime이 null이 아니고 목표값보다 실제값이 더 낮다면, 실패한 goalAchievement와 함께 정상적으로 러닝 기록이 추가된다.")
void addRunningRecord_goal_time_fail() {
// given
RunningRecordRequest request = new RunningRecordRequest(
RunningRecordRequestV1 request = new RunningRecordRequestV1(
LocalDateTime.of(2021, 1, 1, 12, 10, 30),
LocalDateTime.of(2021, 1, 1, 13, 12, 10),
"start location",
Expand All @@ -289,7 +289,7 @@ void addRunningRecord_goal_time_fail() {
given(goalAchievementRepository.save(goalAchievement)).willReturn(goalAchievement);

// when
RunningRecordAddResultResponse response = runningRecordService.addRunningRecord(1L, request);
RunningRecordAddResultResponseV1 response = runningRecordService.addRunningRecordV1(1L, request);

// then
assertEquals(request.startAt(), response.startAt());
Expand All @@ -306,7 +306,7 @@ void addRunningRecord_goal_time_fail() {
"GOAL 모드의 러닝 기록 추가 요청시, goalDistance가 null이 아니고 목표값보다 실제값이 더 높다면 성공한 goalAchievement와 함께 정상적으로 러닝 기록이 추가된다.")
void addRunningRecord_goal_distance() {
// given
RunningRecordRequest request = new RunningRecordRequest(
RunningRecordRequestV1 request = new RunningRecordRequestV1(
LocalDateTime.of(2021, 1, 1, 12, 10, 30),
LocalDateTime.of(2021, 1, 1, 13, 12, 10),
"start location",
Expand All @@ -328,7 +328,7 @@ void addRunningRecord_goal_distance() {
given(goalAchievementRepository.save(goalAchievement)).willReturn(goalAchievement);

// when
RunningRecordAddResultResponse response = runningRecordService.addRunningRecord(1L, request);
RunningRecordAddResultResponseV1 response = runningRecordService.addRunningRecordV1(1L, request);

// then
assertEquals(request.startAt(), response.startAt());
Expand All @@ -345,7 +345,7 @@ void addRunningRecord_goal_distance() {
"GOAL 모드의 러닝 기록 추가 요청시, goalDistance가 null이 아니고 목표값보다 실제값과 같다면 성공한 goalAchievement와 함께 정상적으로 러닝 기록이 추가된다.")
void addRunningRecord_goal_distance_same_value() {
// given
RunningRecordRequest request = new RunningRecordRequest(
RunningRecordRequestV1 request = new RunningRecordRequestV1(
LocalDateTime.of(2021, 1, 1, 12, 10, 30),
LocalDateTime.of(2021, 1, 1, 13, 12, 10),
"start location",
Expand All @@ -367,7 +367,7 @@ void addRunningRecord_goal_distance_same_value() {
given(goalAchievementRepository.save(goalAchievement)).willReturn(goalAchievement);

// when
RunningRecordAddResultResponse response = runningRecordService.addRunningRecord(1L, request);
RunningRecordAddResultResponseV1 response = runningRecordService.addRunningRecordV1(1L, request);

// then
assertEquals(request.startAt(), response.startAt());
Expand All @@ -383,7 +383,7 @@ void addRunningRecord_goal_distance_same_value() {
@DisplayName("러닝의 페이스가 올바르게 계산되었는지 확인한다.")
void addRunningRecord_check_cal_pace() {
// given
RunningRecordRequest request = new RunningRecordRequest(
RunningRecordRequestV1 request = new RunningRecordRequestV1(
LocalDateTime.of(2021, 1, 1, 12, 10, 30),
LocalDateTime.of(2021, 1, 1, 13, 12, 10),
"start location",
Expand All @@ -402,7 +402,7 @@ void addRunningRecord_check_cal_pace() {
given(runningRecordRepository.save(expected)).willReturn(expected);

// when
RunningRecordAddResultResponse response = runningRecordService.addRunningRecord(1L, request);
RunningRecordAddResultResponseV1 response = runningRecordService.addRunningRecordV1(1L, request);

// then
assertEquals(new Pace(8, 28), response.runningData().averagePace());
Expand Down Expand Up @@ -502,7 +502,7 @@ void getWeeklySummary_Duration() {
assertThat(response.lastWeekAvgValue()).isEqualTo(expectedRunningDurationHour);
}

private RunningRecord createRunningRecord(RunningRecordRequest request, Member member) {
private RunningRecord createRunningRecord(RunningRecordRequestV1 request, Member member) {

return RunningRecord.builder()
.member(member)
Expand Down