-
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: API 버저닝을 위한 달성 기록 리팩터링 - 서비스 단에서 전체 코스 리스트(달성한, 달성하지 않은, 현재) 및 달성 기록의 정보를 리턴하도록 구현(CoursesDto 반환) - 바뀐 서비스 단에 맞게 서비스 재 구성 - ScaleCoursesResponse 및 ScaleControllerV1에서 데이터 가공 * Test: /api/v1/scale/courses Response가 맞게 반환되었는지 확인
- Loading branch information
Showing
6 changed files
with
424 additions
and
102 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
61 changes: 61 additions & 0 deletions
61
src/main/java/com/dnd/runus/application/scale/dto/CoursesDto.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,61 @@ | ||
package com.dnd.runus.application.scale.dto; | ||
|
||
|
||
import com.dnd.runus.domain.scale.ScaleAchievementLog; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.time.OffsetDateTime; | ||
import java.util.List; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CoursesDto( | ||
int totalCoursesCount, | ||
int totalCoursesDistanceMeter, | ||
int myTotalRunningMeter, | ||
List<Course> achievedCourses, | ||
Course currentCourse, | ||
List<Course> nextCourses | ||
) { | ||
|
||
/** | ||
* 코스 DTO | ||
* @param order 코스 순서 | ||
* @param achievedMeter 사용자의 코스에 대한 달성 미터 값 | ||
* (ex. totalMeter가 1000, requiredMeterForAchieve가 3000, | ||
* 사용자가 전체 달린 거리가 2600이라면 | ||
* achievedMeter는 600 | ||
* ) | ||
* @param sizeMeter 코스의 거리 미터 값 | ||
* @param requiredMeterForAchieve 코스를 달성하기 위한 미터값 | ||
* (ex. 2코스라면, 1코스 totalMeter + 2코스 totalMeter의 값) | ||
* @param achievedAt 달성한 날짜, 달성하지 않으면 null | ||
*/ | ||
@Schema(name = "course", description = "코스") | ||
public record Course( | ||
String name, | ||
String StartName, | ||
String endName, | ||
int order, | ||
int achievedMeter, | ||
int sizeMeter, | ||
int requiredMeterForAchieve, | ||
OffsetDateTime achievedAt | ||
) { | ||
public static Course of( | ||
ScaleAchievementLog scaleAchievementLog, | ||
int requiredTotalMeterForAchieve, | ||
int achievedMeter | ||
) { | ||
return new Course( | ||
scaleAchievementLog.scale().name(), | ||
scaleAchievementLog.scale().startName(), | ||
scaleAchievementLog.scale().endName(), | ||
scaleAchievementLog.scale().index(), | ||
achievedMeter, | ||
scaleAchievementLog.scale().sizeMeter(), | ||
requiredTotalMeterForAchieve, | ||
scaleAchievementLog.achievedDate() | ||
); | ||
} | ||
} | ||
} |
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
114 changes: 75 additions & 39 deletions
114
src/main/java/com/dnd/runus/presentation/v1/scale/dto/ScaleCoursesResponse.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,77 +1,113 @@ | ||
package com.dnd.runus.presentation.v1.scale.dto; | ||
|
||
import com.dnd.runus.application.scale.dto.CoursesDto; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
import java.text.DecimalFormat; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
import lombok.Builder; | ||
|
||
import static com.dnd.runus.global.constant.MetricsConversionFactor.METERS_IN_A_KILOMETER; | ||
|
||
@Builder | ||
public record ScaleCoursesResponse( | ||
Info info, | ||
List<AchievedCourse> achievedCourses, | ||
CurrentCourse currentCourse | ||
) { | ||
|
||
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("0.#km"); | ||
private static final DecimalFormat KILO_METER_FORMATTER = new DecimalFormat("#,###.#km"); | ||
|
||
@Schema(name = "course Info", description = "코스 정보") | ||
/** | ||
* 거리 formater | ||
* | ||
* @return distanceMeter가 1km보다 작을 경우 "m"로 아니면 "#,###.#km"형식으로 반환합니다. | ||
*/ | ||
private static String formaterForDistance(int distanceMeter) { | ||
if (distanceMeter < METERS_IN_A_KILOMETER) { | ||
return distanceMeter + "m"; | ||
} | ||
return KILO_METER_FORMATTER.format(distanceMeter / METERS_IN_A_KILOMETER); | ||
} | ||
|
||
|
||
private static String makeMessageAboutLeftKm(String goalPoint, int leftMeter) { | ||
//소수점 둘째자리에서 반올림 | ||
double leftKm = Math.round((leftMeter / METERS_IN_A_KILOMETER) * 10.0) / 10.0; | ||
return goalPoint + "까지 " + KILO_METER_FORMATTER.format(leftKm) + " 남았어요!"; | ||
} | ||
|
||
|
||
|
||
@Schema(name = "courseResponseV1 Info", description = "코스 정보") | ||
public record Info( | ||
@Schema(description = "총 코스 수 (공개되지 않은 코스 포함)", example = "18") | ||
int totalCourses, | ||
@Schema(description = "총 코스 거리 (공개되지 않은 코스 포함)", example = "1000km") | ||
String totalDistance | ||
@Schema(description = "총 코스 수 (공개되지 않은 코스 포함)", example = "18") | ||
int totalCourses, | ||
@Schema(description = "총 코스 거리 (공개되지 않은 코스 포함)", example = "1000km") | ||
String totalDistance | ||
) { | ||
public Info( | ||
int totalCourses, | ||
int totalMeter | ||
int totalCourses, | ||
int totalMeter | ||
) { | ||
this(totalCourses, KILO_METER_FORMATTER.format(totalMeter / METERS_IN_A_KILOMETER)); | ||
this(totalCourses, formaterForDistance(totalMeter)); | ||
} | ||
} | ||
|
||
@Schema(name = "courseResponseV1 AchievedCourse", description = "달성한 코스") | ||
public record AchievedCourse( | ||
@Schema(description = "코스 이름", example = "서울에서 인천") | ||
String name, | ||
@Schema(description = "코스 총 거리", example = "30km") | ||
String totalDistance, | ||
@Schema(description = "달성 일자") | ||
LocalDate achievedAt | ||
@Schema(description = "코스 이름", example = "서울에서 인천") | ||
String name, | ||
@Schema(description = "코스 총 거리", example = "30km") | ||
String totalDistance, | ||
@Schema(description = "달성 일자") | ||
LocalDate achievedAt | ||
) { | ||
public AchievedCourse( | ||
String name, | ||
int totalMeter, | ||
LocalDate achievedAt | ||
) { | ||
this(name, KILO_METER_FORMATTER.format(totalMeter / METERS_IN_A_KILOMETER), achievedAt); | ||
public static ScaleCoursesResponse.AchievedCourse from(CoursesDto.Course achievedCourse) { | ||
return new ScaleCoursesResponse.AchievedCourse( | ||
achievedCourse.name(), | ||
formaterForDistance(achievedCourse.sizeMeter()), | ||
achievedCourse.achievedAt().toLocalDate() | ||
); | ||
} | ||
} | ||
|
||
@Schema(name = "courseResponseV1 CurrentCourse", description = "현재 코스") | ||
public record CurrentCourse( | ||
@Schema(description = "현재 코스 이름", example = "서울에서 부산") | ||
String name, | ||
@Schema(description = "현재 코스 총 거리", example = "200km") | ||
String totalDistance, | ||
@Schema(description = "현재 달성한 거리, 현재 32.3km 달성", example = "32.3km") | ||
String achievedDistance, | ||
@Schema(description = "현재 코스 설명 메시지", example = "대전까지 100km 남았어요!") | ||
String message | ||
@Schema(description = "현재 코스 이름", example = "서울에서 부산") | ||
String name, | ||
@Schema(description = "현재 코스 총 거리", example = "200km") | ||
String totalDistance, | ||
@Schema(description = "현재 달성한 거리, 현재 32.3km 달성", example = "32.3km") | ||
String achievedDistance, | ||
@Schema(description = "현재 코스 설명 메시지", example = "대전까지 100km 남았어요!") | ||
String message | ||
) { | ||
public CurrentCourse( | ||
String name, | ||
int totalMeter, | ||
int achievedMeter, | ||
String message | ||
String name, | ||
int totalDistanceMeter, | ||
int achievedDistanceMeter, | ||
String message | ||
) { | ||
this(name, KILO_METER_FORMATTER.format(totalMeter / METERS_IN_A_KILOMETER), formatAchievedDistance(achievedMeter), message); | ||
this( | ||
name, | ||
formaterForDistance(totalDistanceMeter), | ||
formaterForDistance(achievedDistanceMeter), | ||
message | ||
); | ||
} | ||
|
||
private static String formatAchievedDistance(int achievedMeter) { | ||
if (achievedMeter < METERS_IN_A_KILOMETER) { | ||
return achievedMeter + "m"; | ||
} | ||
return KILO_METER_FORMATTER.format(achievedMeter / METERS_IN_A_KILOMETER); | ||
public static ScaleCoursesResponse.CurrentCourse from(CoursesDto.Course course) { | ||
return new ScaleCoursesResponse.CurrentCourse( | ||
course.name(), | ||
formaterForDistance(course.sizeMeter()), | ||
formaterForDistance(course.achievedMeter()), | ||
makeMessageAboutLeftKm( | ||
course.endName(), | ||
Math.max(0, course.sizeMeter() - course.achievedMeter()) | ||
) | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.