Skip to content

Commit

Permalink
[feat] 여행 기간을 계산하는 기능 구현 (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeyoung22 committed Aug 21, 2023
1 parent 3761f87 commit c19e472
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backend/src/main/java/dev/tripdraw/domain/trip/Trip.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static dev.tripdraw.domain.trip.TripStatus.ONGOING;
import static dev.tripdraw.exception.trip.TripExceptionType.NOT_AUTHORIZED_TO_TRIP;
import static dev.tripdraw.exception.trip.TripExceptionType.ONE_OR_NO_POINT;
import static dev.tripdraw.exception.trip.TripExceptionType.TRIP_INVALID_STATUS;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
Expand All @@ -17,6 +18,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.Duration;
import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -80,6 +82,18 @@ public void validateAuthorization(Member member) {
}
}

public TripDuration calculateTripDuration() {
List<Point> points = points();
if (points.size() == 1 || points.isEmpty()) {
throw new TripException(ONE_OR_NO_POINT);
}
Point startingPoint = points.get(0);
Point arrivalPoint = points.get(points.size() - 1);

Duration between = Duration.between(startingPoint.recordedAt(), arrivalPoint.recordedAt());
return TripDuration.of(between);
}

public void changeStatus(TripStatus status) {
validateStatus(status);
this.status = status;
Expand Down
41 changes: 41 additions & 0 deletions backend/src/main/java/dev/tripdraw/domain/trip/TripDuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.tripdraw.domain.trip;

import java.time.Duration;

public class TripDuration {

private static final String MINUTE = "분";
private static final String HOUR = "시간";
private static final String DAY = "일";
private static final String WHITE_SPACE = " ";

private final Duration duration;

private TripDuration(Duration duration) {
this.duration = duration;
}

public static TripDuration of(Duration duration) {
return new TripDuration(duration);
}

public String durationInMinutes() {
long minutes = duration.toMinutes();
return minutes + MINUTE;
}

public String durationInHoursAndMinutes() {
long hours = duration.toHours();
long minutes = duration.toMinutes() - (hours * 60);

return hours + HOUR + WHITE_SPACE + minutes + MINUTE;
}

public String durationInDaysAndHoursAndMinutes() {
long days = duration.toDays();
long hours = duration.toHours() - (days * 24);
long minutes = duration.toMinutes() - (days * 1440) - (hours * 60);

return days + DAY + WHITE_SPACE + hours + HOUR + WHITE_SPACE + minutes + MINUTE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum TripExceptionType implements ExceptionType {
TRIP_INVALID_STATUS(BAD_REQUEST, "잘못된 여행 상태입니다."),
POINT_ALREADY_HAS_POST(CONFLICT, "이미 감상이 등록된 위치입니다."),
TRIP_ALREADY_DELETED(CONFLICT, "이미 삭제된 여행입니다."),
ONE_OR_NO_POINT(BAD_REQUEST, ""),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.tripdraw.domain.trip;

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Duration;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class TripDurationTest {

private final TripDuration tripDuration = TripDuration.of(Duration.ofMinutes(3248));

@Test
void 여행기간을_분_단위로_나타낸다() {
// given & when
String durationInMinutes = tripDuration.durationInMinutes();

// then
assertThat(durationInMinutes).isEqualTo("3248분");
}

@Test
void 여행기간을_시간과_분_단위로_나타낸다() {
// given & when
String durationInHoursAndMinutes = tripDuration.durationInHoursAndMinutes();

// then
assertThat(durationInHoursAndMinutes).isEqualTo("54시간 8분");
}

@Test
void 여행기간을_일과_시간과_분_단위로_나타낸다() {
// given & when
String durationInDaysAndHoursAndMinutes = tripDuration.durationInDaysAndHoursAndMinutes();
// then
assertThat(durationInDaysAndHoursAndMinutes).isEqualTo("2일 6시간 8분");
}
}
9 changes: 9 additions & 0 deletions backend/src/test/java/dev/tripdraw/domain/trip/TripTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,13 @@ class TripTest {
// expect
assertThat(trip.points()).containsExactly(point1);
}

@Test
void 총_여행기간을_반환한다() {
// given

// when

// then
}
}

0 comments on commit c19e472

Please sign in to comment.