Skip to content

Commit

Permalink
refactor: List<Reservation>을 일급 컬렉션으로 래핑 (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
astraum committed Jun 9, 2022
1 parent 1d9247a commit 5930685
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.team4.airbnb.reservation;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -46,17 +43,12 @@ public ReservationElement make(ReservationRequest reservationRequest) {

@Transactional(readOnly = true)
public ReservationResponse findAllByCustomerId(Long customerId, Pageable pageable) {
Reservations reservations = new Reservations(reservationRepository
.findAllByCustomerId(customerId, pageable));

List<Reservation> reservations = reservationRepository
.findAllByCustomerId(customerId, pageable);
accommodationRepository.findAllWithImagesByIdIn(reservations.getAccommodationIds());

Set<Long> accommodationIds = reservations.stream()
.map(Reservation::getAccommodationId)
.collect(Collectors.toSet());

accommodationRepository.findAllWithImagesByIdIn(accommodationIds);

return ReservationResponse.from(reservations);
return reservations.toReservationResponse();
}

@Transactional
Expand Down
28 changes: 28 additions & 0 deletions BE/src/main/java/org/team4/airbnb/reservation/Reservations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.team4.airbnb.reservation;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.team4.airbnb.reservation.dto.ReservationElement;
import org.team4.airbnb.reservation.dto.ReservationResponse;

public class Reservations {

private final List<Reservation> elements;

public Reservations(List<Reservation> elements) {
this.elements = elements;
}

public Set<Long> getAccommodationIds() {
return elements.stream()
.map(Reservation::getAccommodationId)
.collect(Collectors.toSet());
}

public ReservationResponse toReservationResponse() {
return new ReservationResponse(elements.stream()
.map(ReservationElement::new)
.collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public ReservationElement(Reservation reservation) {
this.invoiceTotal = reservation.getInvoiceTotal();
this.hostName = reservation.getHostName();
this.accommodationType = reservation.getAccommodationTypeString();
this.accommodationImages = reservation.getAccommodationImageUrls();
this.accommodationImages = List.copyOf(reservation.getAccommodationImageUrls());
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package org.team4.airbnb.reservation.dto;

import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.team4.airbnb.reservation.Reservation;

@RequiredArgsConstructor
@Getter
public class ReservationResponse {

private final List<ReservationElement> data;

public static ReservationResponse from(List<Reservation> reservations) {
return new ReservationResponse(reservations.stream()
.map(ReservationElement::new)
.collect(Collectors.toList()));
public ReservationResponse(List<ReservationElement> data) {
this.data = List.copyOf(data);
}
}

0 comments on commit 5930685

Please sign in to comment.