Skip to content

Commit

Permalink
Fix: 활동요약 응답 형식 변경 (#263)
Browse files Browse the repository at this point in the history
* Fix: 이번주 날짜 응답 형식을 yyyy.MM.dd ~ yyyy.MM.dd로 변경

* Test: 필요없는 테스트 코드 삭제

* Feat: 요일별 활동 기록 DTO 추가

* Fix: 요일 별 활동 응답 형식을 double[]에서 List<WeeklyRunningRatingDto>로 변경

* Docs: 응답형식 변경으로 api docs 수정

* Style: 주석 괄호 밖으로 이동
  • Loading branch information
hee9841 authored Oct 14, 2024
1 parent 17dbdc4 commit 3f12475
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@
import com.dnd.runus.domain.scale.ScaleAchievementRepository;
import com.dnd.runus.domain.scale.ScaleRepository;
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.RunningRecordWeeklySummaryType;
import com.dnd.runus.presentation.v1.running.dto.response.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.time.*;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER;
import static com.dnd.runus.global.constant.MetricsConversionFactor.SECONDS_PER_HOUR;
Expand Down Expand Up @@ -149,10 +154,19 @@ public RunningRecordWeeklySummaryResponse getWeeklySummary(
conversionFactor = SECONDS_PER_HOUR;
}

double[] weeklyValues = new double[7];
List<WeeklyRunningRatingDto> weeklyValues = Arrays.stream(DayOfWeek.values())
.map(day -> new WeeklyRunningRatingDto(
day.getDisplayName(TextStyle.SHORT, Locale.KOREAN), 0.0)) // 초기값으로 0.0 설정
.collect(Collectors.toList());

// weeklyValues에 값 set
for (DailyRunningRecordSummary summary : weekSummaries) {
int dayOfWeek = summary.date().getDayOfWeek().getValue() - 1;
weeklyValues[dayOfWeek] = summary.sumValue() / conversionFactor;
DayOfWeek dayOfWeek = summary.date().getDayOfWeek();
weeklyValues.set(
dayOfWeek.getValue() - 1,
new WeeklyRunningRatingDto(
dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.KOREAN),
summary.sumValue() / conversionFactor));
}

return new RunningRecordWeeklySummaryResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ public RunningRecordMonthlySummaryResponse getMonthlyRunningSummary(@MemberId lo
- request 값이 DISTANCE 이면 이번주 요일별 달린 거리와, 지난주 달린 거리의 평균 값을 리턴합니다.<br>
- request 값이 TIME 이면 이번주 요일별 달린 시간과, 지난주 달린 시간의 평균 값을 리턴합니다.<br>
- request값과 상관 없이 공통의로 이번주 날짜(월요일 날짜 ~ 일요일 날짜)를 리턴합니다.<br>
이번 주 기록은 리스트형식으로(weeklyValues)로 리턴 되며 인덱스 값에 따른 데이터는 다음과 같습니다.<br>
- weeklyValues = [월요일 기록, 화요일 기록, .... , 일요일 기록]
""")
@ResponseStatus(HttpStatus.OK)
@GetMapping("weekly-summary")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dnd.runus.presentation.v1.running.dto;

import io.swagger.v3.oas.annotations.media.Schema;

public record WeeklyRunningRatingDto(
@Schema(description = "요일", example = "월")
String day,
@Schema(description = "거리는 km, 시간은 시간(Hour) 단위, 기록없으면 0")
double rating
) {
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
package com.dnd.runus.presentation.v1.running.dto.response;

import com.dnd.runus.presentation.v1.running.dto.WeeklyRunningRatingDto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

public record RunningRecordWeeklySummaryResponse(
@Schema(description = "이번주 날짜", example = "2024.09.09 ~ 09.15")
String date,
@Schema(description = "요일 별 활동 값, 거리는 km, 시간은 시간(Hour) 단위<br>"
+ "월요일(index:0) ~ 일요일(index:6)의 값, 기록없는 날은 0을 리턴")
double[] weeklyValues,
@Schema(description = "이번주 날짜", example = "2024.09.09 ~ 2024.09.15")
String weeklyDate,
@Schema(description = "요일 별 활동 값<br>"
+ " ~ 일 순서로 리턴")
List<WeeklyRunningRatingDto> weeklyValues,
@Schema(description = "지난주 평균 활동 값, 거리는 km, 시간은 시간(Hour) 단위<br>"
+ "지난주에 기록이 없으면 0을 리턴")
double lastWeekAvgValue
) {
public RunningRecordWeeklySummaryResponse(LocalDate startDate, LocalDate endDate, double[] weeklyValues, double lastWeekAvgValue) {
public RunningRecordWeeklySummaryResponse(LocalDate startDate, LocalDate endDate, List<WeeklyRunningRatingDto> weeklyValues, double lastWeekAvgValue) {
this(dateFormat(startDate, endDate), weeklyValues, lastWeekAvgValue);
}

private static String dateFormat(LocalDate startDate, LocalDate endDate) {
DateTimeFormatter yyyyMMddFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
String formattedStartDate = startDate.format(yyyyMMddFormatter);
String formattedEndDate = endDate.format(DateTimeFormatter.ofPattern("MM.dd"));
if (startDate.getYear() != endDate.getYear()) {
//연도가 다를 경우 yyyy.MM.dd ~ yyyy.MM.dd
formattedEndDate = endDate.format(yyyyMMddFormatter);
}
//yyyy.MM.dd ~ MM.dd 로 리턴
String formattedEndDate = endDate.format(yyyyMMddFormatter);
return formattedStartDate + " ~ " + formattedEndDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.dnd.runus.global.constant.RunningEmoji;
import com.dnd.runus.global.exception.NotFoundException;
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.RunningRecordWeeklySummaryType;
Expand Down Expand Up @@ -476,12 +477,10 @@ void getWeeklySummary_Distance() {
RunningRecordWeeklySummaryResponse response = runningRecordService.getWeeklySummary(memberId, summaryType);

// then
assertTrue(response.date().contains("~"));

double[] weeklyValues = response.weeklyValues();
int runningDateIdx = runningDate.get(DAY_OF_WEEK) - 1;
assertThat(weeklyValues.length).isEqualTo(7);
assertThat(weeklyValues[runningDateIdx]).isEqualTo(3.567);
List<WeeklyRunningRatingDto> weeklyValues = response.weeklyValues();
WeeklyRunningRatingDto weeklyValue = weeklyValues.get(runningDate.get(DAY_OF_WEEK) - 1);
assertThat(weeklyValues.size()).isEqualTo(7);
assertThat(weeklyValue.rating()).isEqualTo(3.567);
assertThat(response.lastWeekAvgValue()).isEqualTo(0.8);
}

Expand Down Expand Up @@ -513,12 +512,10 @@ void getWeeklySummary_Duration() {
RunningRecordWeeklySummaryResponse response = runningRecordService.getWeeklySummary(memberId, summaryType);

// then
assertTrue(response.date().contains("~"));

double[] weeklyValues = response.weeklyValues();
int runningDateIdx = runningDate.get(DAY_OF_WEEK) - 1;
assertThat(weeklyValues.length).isEqualTo(7);
assertThat(weeklyValues[runningDateIdx]).isEqualTo(expectedRunningDurationHour);
List<WeeklyRunningRatingDto> weeklyValues = response.weeklyValues();
WeeklyRunningRatingDto weeklyValue = weeklyValues.get(runningDate.get(DAY_OF_WEEK) - 1);
assertThat(weeklyValues.size()).isEqualTo(7);
assertThat(weeklyValue.rating()).isEqualTo(expectedRunningDurationHour);
assertThat(response.lastWeekAvgValue()).isEqualTo(expectedRunningDurationHour);
}

Expand Down

0 comments on commit 3f12475

Please sign in to comment.