Skip to content

Commit

Permalink
Merge pull request #15 from DO-SOPT-CDS-TEAM-WEB7/feat/#14
Browse files Browse the repository at this point in the history
[#14] feat : 나라별 예약 최저가격 순으로 조회하기
  • Loading branch information
sss4920 authored Nov 27, 2023
2 parents e47137c + fd137df commit aca9c04
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public record AirReservationResponse(
DateDto dateDto,
List<AirDto> airDto
List<ReservationDto> reservationListDto

){
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import org.third.thirdseminar.domain.Reservation;

public record AirDto (
Long airId,
public record ReservationDto (
Long reservationId,
String airName,
TimeRangeDto startTime,
TimeRangeDto endTime,
String price,
int CO2
){
public static AirDto of(Reservation reservation, String price, TimeRangeDto startTime, TimeRangeDto endTime) {
return new AirDto(
public static ReservationDto of(Reservation reservation, String price, TimeRangeDto startTime, TimeRangeDto endTime) {
return new ReservationDto(
reservation.getId(),
reservation.getAir().getAirName(),
startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@


public record TimeRangeDto (
String startTime,
String endTime,
String start,
String end,
String during){
public static TimeRangeDto of(String startTime, String endTime, String during) {
public static TimeRangeDto of(String start, String end, String during) {
return new TimeRangeDto(
startTime, endTime, during
start, end, during
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.third.thirdseminar.domain;


public record ReservationMinPriceDTO (
Reservation reservation,
Long minPrice
){
public static ReservationMinPriceDTO of(Reservation reservation,Long minPrice) {
return new ReservationMinPriceDTO(reservation, minPrice);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.third.thirdseminar.controller.dto.response.AirDto;
import org.third.thirdseminar.domain.Reservation;
import org.third.thirdseminar.domain.ReservationMinPriceDTO;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public interface ReservationJpaRepository extends JpaRepository<Reservation, Long> {
@Query("SELECT r FROM Reservation r WHERE r.cityName <> :cityName")
List<Reservation> findAllExcludingCity(@Param("cityName") String cityName);


@Query("SELECT r, MIN(t.price) FROM Reservation r JOIN r.tickets t WHERE r.country = :country GROUP BY r ORDER BY MIN(t.price) ASC")
List<Object[]> findReservations(@Param("country") String country);


Optional<Reservation> findById(Long reservationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.third.thirdseminar.controller.dto.request.AirReservationRequest;
import org.third.thirdseminar.controller.dto.response.AirDto;
import org.third.thirdseminar.controller.dto.response.AirReservationResponse;
import org.third.thirdseminar.controller.dto.response.DateDto;
import org.third.thirdseminar.controller.dto.response.TimeRangeDto;
import org.third.thirdseminar.controller.dto.request.CreateReservationRequest;
import org.third.thirdseminar.controller.dto.response.*;
import org.third.thirdseminar.domain.Reservation;
import org.third.thirdseminar.domain.ReservationResult;
import org.third.thirdseminar.domain.Ticket;
import org.third.thirdseminar.domain.TimeRange;
import org.third.thirdseminar.domain.*;
import org.third.thirdseminar.infrastructure.ReservationJpaRepository;
import org.third.thirdseminar.infrastructure.ReservationResultJpaRepository;
import org.third.thirdseminar.infrastructure.TicketJpaRepository;
Expand All @@ -39,12 +35,17 @@ public class ReservationService {
private final DecimalFormat df = new DecimalFormat("###,###");

public AirReservationResponse getReservations(AirReservationRequest reqeust){
List<Reservation> reservations = reservationJpaRepository.findAll();
List<ReservationMinPriceDTO> reservationMinPriceDtoList = reservationJpaRepository.findReservations("일본").stream().map(
row -> ReservationMinPriceDTO.of(
(Reservation) row[0],
(Long) row[1]
)).toList();

List<AirDto> airList = new ArrayList<>();
for(Reservation reservation : reservations){
List<Ticket> ticket= tickectJpaRepository.findByReservationIdOrderByPriceAsc(reservation.getId());
airList.add(AirDto.of(reservation, df.format(ticket.get(0).getPrice()), TimeRangeFormat(reservation.getStartTime()), TimeRangeFormat(reservation.getEndTime())));

List<ReservationDto> airList = new ArrayList<>();
for(ReservationMinPriceDTO reservationMinPriceDTO : reservationMinPriceDtoList){
Reservation reservation = reservationMinPriceDTO.reservation();
airList.add(ReservationDto.of(reservation, df.format(reservationMinPriceDTO.minPrice()), TimeRangeFormat(reservation.getStartTime()), TimeRangeFormat(reservation.getEndTime())));
}
DateDto dateDto = new DateDto(reqeust.startDate(), reqeust.endDate());
return new AirReservationResponse(dateDto, airList);
Expand Down

0 comments on commit aca9c04

Please sign in to comment.