-
Notifications
You must be signed in to change notification settings - Fork 1
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: 달성 기록 API v1 리팩터링 #309
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
application 계층에 있는 dto 클래스는 스웨거(컨트롤러 계층)이 없어야 할 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
V2 만드는 PR에 해당 부분 수정해서 올릴게요!