Skip to content

Commit

Permalink
feat: Trip 업데이트 시 City 업데이트 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
mcodnjs committed Jul 27, 2023
1 parent 5f482bc commit 17ecddd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface TripCityRepository extends JpaRepository<TripCity, Long> {

List<TripCity> findByTripId(Long tripId);

void deleteAllByTripId(Long tripId);
}
24 changes: 15 additions & 9 deletions backend/src/main/java/hanglog/trip/service/TripService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ public Long save(final TripCreateRequest tripCreateRequest) {
tripCreateRequest.getStartDate(),
tripCreateRequest.getEndDate()
);
saveAllTripCities(cites, newTrip);
saveTripCities(cites, newTrip);
saveDayLogs(newTrip);
final Trip trip = tripRepository.save(newTrip);
return trip.getId();
}

private void saveTripCities(final List<City> cites, final Trip trip) {
final List<TripCity> tripCities = cites.stream()
.map(city -> new TripCity(trip, city))
.toList();
tripCityRepository.saveAll(tripCities);
}

private void saveDayLogs(final Trip savedTrip) {
final Period period = Period.between(savedTrip.getStartDate(), savedTrip.getEndDate().plusDays(1));
final List<DayLog> dayLogs = IntStream.rangeClosed(1, period.getDays() + 1)
Expand Down Expand Up @@ -84,6 +91,13 @@ public TripDetailResponse getTripDetail(final Long tripId) {
public void update(final Long tripId, final TripUpdateRequest updateRequest) {
final Trip trip = tripRepository.findById(tripId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_TRIP_ID));
final List<City> cities = updateRequest.getCityIds().stream()
.map(cityId -> cityRepository.findById(cityId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_CITY_ID)))
.toList();
tripCityRepository.deleteAllByTripId(tripId);
saveTripCities(cities, trip);

final int currentPeriod = Period.between(trip.getStartDate(), trip.getEndDate()).getDays() + 1;
final int requestPeriod =
Period.between(updateRequest.getStartDate(), updateRequest.getEndDate()).getDays() + 1;
Expand All @@ -101,7 +115,6 @@ public void update(final Long tripId, final TripUpdateRequest updateRequest) {
updateRequest.getDescription(),
trip.getDayLogs()
);
System.out.println(updatedTrip.getImageUrl());
tripRepository.save(updatedTrip);
}

Expand Down Expand Up @@ -140,11 +153,4 @@ public void delete(final Long tripId) {
private String getInitTitle(final List<City> cites) {
return cites.get(0).getName() + TITLE_POSTFIX;
}

private void saveAllTripCities(final List<City> cites, final Trip trip) {
final List<TripCity> tripCities = cites.stream()
.map(city -> new TripCity(trip, city))
.toList();
tripCityRepository.saveAll(tripCities);
}
}
12 changes: 10 additions & 2 deletions backend/src/test/java/hanglog/trip/service/TripServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ void update() {
LocalDate.of(2023, 7, 2),
LocalDate.of(2023, 7, 5),
"추가된 여행 설명",
List.of(1L, 2L, 3L)
List.of(1L, 2L)
);

given(tripRepository.findById(LONDON_TRIP.getId()))
.willReturn(Optional.of(LONDON_TRIP));
given(cityRepository.findById(1L))
.willReturn(Optional.of(PARIS));
given(cityRepository.findById(2L))
.willReturn(Optional.of(LONDON));

// when
tripService.update(LONDON_TRIP.getId(), updateRequest);
Expand Down Expand Up @@ -191,7 +195,7 @@ void changeDate(final int startDay, final int endDay) {
LocalDate.of(2023, 7, startDay),
LocalDate.of(2023, 7, endDay),
"추가된 여행 설명",
List.of(1L, 2L, 3L)
List.of(1L, 2L)
);

final Trip updatedTrip = new Trip(
Expand All @@ -208,6 +212,10 @@ void changeDate(final int startDay, final int endDay) {
.willReturn(Optional.of(trip));
given(tripRepository.save(any(Trip.class)))
.willReturn(updatedTrip);
given(cityRepository.findById(1L))
.willReturn(Optional.of(PARIS));
given(cityRepository.findById(2L))
.willReturn(Optional.of(LONDON));
}

@DisplayName("변경된 일정이 같은 경우")
Expand Down

0 comments on commit 17ecddd

Please sign in to comment.