Skip to content

Commit

Permalink
Controller, Dto 수정 통합테스트 MockMvc 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
SEONGHUN-95 committed Nov 21, 2024
1 parent f20affb commit 6961a09
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 404 deletions.
12 changes: 0 additions & 12 deletions api/src/main/java/org/onewayticket/controller/AuthController.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,91 @@

import lombok.RequiredArgsConstructor;
import org.onewayticket.dto.BookingDetailsDto;
import org.onewayticket.dto.BookingDto;
import org.onewayticket.dto.BookingRequestDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.time.format.DateTimeParseException;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/bookings")
public class BookingController {

// 예약 생성
@PostMapping
public ResponseEntity<String> createBooking(@RequestBody BookingRequestDto bookingRequestInfo) {
if (bookingRequestInfo.paymentId() == null || bookingRequestInfo.paymentId().isBlank()) {
return ResponseEntity.badRequest().body("Payment information is missing");
public ResponseEntity<BookingDetailsDto> createBooking(@RequestBody BookingRequestDto bookingRequestInfo) {
if (bookingRequestInfo.paymentId() == null || bookingRequestInfo.paymentId().isBlank() || !bookingRequestInfo.paymentId().equals("Confirmed")) {
return ResponseEntity.status(400).build();
}
return ResponseEntity.ok("Booking created");

return ResponseEntity.ok(new BookingDetailsDto("A1234", bookingRequestInfo.bookingName(), bookingRequestInfo.bookingEmail(), bookingRequestInfo.bookingPhoneNumber(),
bookingRequestInfo.flightId(), "ICN", "NRT", LocalDate.of(2023, 12, 1), LocalDate.of(2023, 12, 1), "Jane Doe",
LocalDate.parse(bookingRequestInfo.birthDate()), 25, "Female", "AB123456", "Korean", "12A", "Economy", BigDecimal.valueOf(500), "Confirmed"));
}

// 예약 상세 조회
@GetMapping("/{id}")
public ResponseEntity<BookingDetailsDto> getBookingDetails(@PathVariable String id) {
@GetMapping
public ResponseEntity<BookingDetailsDto> getBookingDetails(
@RequestParam("bookingId") String bookingId,
@RequestParam("name") String name,
@RequestParam("birthDate") String birthDate) {

// 날짜 유효성 검증
if (!isValidDate(birthDate)) {
return ResponseEntity.badRequest().body(null);
}

// 미리 정의된 예약 정보
BookingDetailsDto bookingDetails = new BookingDetailsDto(
id, "John Doe", "[email protected]", "123456789",
"B1234", "John Doe", "[email protected]", "123456789",
"FL123", "ICN", "NRT", LocalDate.of(2023, 12, 1), LocalDate.of(2023, 12, 1),
"Jane Doe", 25, "Female", "AB123456", "Korean", "12A", "Economy",
BigDecimal.valueOf(500), "Confirmed"
);
name, LocalDate.parse(birthDate), 25, "Female", "AB123456", "Korean", "12A", "Economy",
BigDecimal.valueOf(500), "Confirmed");

// bookingId가 일치하지 않을 경우 예외 처리
if (!bookingDetails.bookingId().equals(bookingId)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}

return ResponseEntity.ok(bookingDetails);
}


// 예약 취소
@DeleteMapping("/{id}")
public ResponseEntity<String> cancelBooking(@PathVariable String id, @RequestHeader("Authorization") String authHeader) {
public ResponseEntity<String> cancelBooking(
@PathVariable String id,
@RequestHeader(value = "Authorization", required = false) String authHeader) {

// 토큰 검증
if (!authHeader.equals("Bearer VALID_TOKEN")) {
return ResponseEntity.status(403).body("Unauthorized user");
if (authHeader == null || !authHeader.equals("Bearer VALID_TOKEN")) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); // 메시지 없이 401 반환
}

return ResponseEntity.ok("Booking canceled");
// 200 OK와 함께 예약 ID 반환
return ResponseEntity.ok("Booking with ID " + id + " has been canceled successfully.");
}


@GetMapping("/my")
public ResponseEntity<List<BookingDto>> getMyBookings(
@RequestParam("bookingId") String bookingId,
@RequestParam("name") String name,
@RequestParam("birthDate") String birthDate
) {
// 더미 데이터
List<BookingDto> bookings = List.of(
new BookingDto("BK001", "John Doe", "[email protected]", "123456789"),
new BookingDto("BK002", "Jane Smith", "[email protected]", "987654321")
);

// 사용자 정보로 필터링
List<BookingDto> filteredBookings = bookings.stream()
.filter(booking -> booking.bookingId().equals(bookingId) && booking.reservationName().equalsIgnoreCase(name))
.toList();

// 조건에 따라 응답 처리
if (filteredBookings.isEmpty()) {
return ResponseEntity.status(404).build(); // 404 반환
// 오늘 이후 날짜이거나 입력 값이 맞는지 확인
private boolean isValidDate(String date) {
try {
LocalDate parsedDate = LocalDate.parse(date); // 기본 포맷 yyyy-MM-dd
return !parsedDate.isAfter(LocalDate.now()); // 오늘 이후 날짜인지 확인
} catch (DateTimeParseException e) {
return false; // 형식이 잘못된 경우 false 반환
}

return ResponseEntity.ok(filteredBookings);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,53 @@ public ResponseEntity<List<FlightDto>> getCheapestFlights() {
.toList());
}

// 검색결과 보여줄 필터 추가
@GetMapping("/search")
public ResponseEntity<List<FlightDto>> searchFlights(
@RequestParam("departure") String departure,
@RequestParam("destination") String destination,
@RequestParam("departureDate") String departureDate,
@RequestParam("numberOfPassengers") Integer numberOfPassengers) {
@RequestParam("numberOfPassengers") Integer numberOfPassengers,
@RequestParam(value = "sort", defaultValue = "price") String sort) {

// 출발지와 목적지가 같은 경우
if (departure.equalsIgnoreCase(destination)) {
return ResponseEntity.badRequest().body(List.of());
}

// 예제 데이터 반환
return ResponseEntity.ok(List.of(
// 예제 데이터 생성
List<FlightDto> flights = List.of(
new FlightDto("FL001", BigDecimal.valueOf(50), LocalDateTime.now(), departure, destination,
LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(4), Duration.ofHours(2)),
new FlightDto("FL002", BigDecimal.valueOf(70), LocalDateTime.now(), departure, destination,
LocalDateTime.now().plusHours(3), LocalDateTime.now().plusHours(5), Duration.ofHours(2)),
new FlightDto("FL003", BigDecimal.valueOf(120), LocalDateTime.now(), departure, destination,
LocalDateTime.now().plusHours(2), LocalDateTime.now().plusHours(4), Duration.ofHours(2))
));
LocalDateTime.now().plusHours(4), LocalDateTime.now().plusHours(6), Duration.ofHours(2))
);

// 정렬 로직 추가
List<FlightDto> sortedFlights = flights.stream()
.sorted((f1, f2) -> {
switch (sort) {
case "price":
default:
return f1.price().compareTo(f2.price());
case "arrivalTime":
return f1.arrivalTime().compareTo(f2.arrivalTime());
case "flightDuration":
return f1.flightDuration().compareTo(f2.flightDuration());

}
})
.toList();

return ResponseEntity.ok(sortedFlights);
}


@GetMapping("/{flightId}")
public ResponseEntity<FlightDto> getFlightDetails(@PathVariable String flightId) {
if (flightId.length() > 50) { // 예제: ID가 50자를 초과하면 오류 반환
if (flightId.length() > 50) { // flightId 유효성 검사
return ResponseEntity.badRequest().build();
}

Expand Down

This file was deleted.

7 changes: 4 additions & 3 deletions api/src/main/java/org/onewayticket/dto/BookingDetailsDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
public record BookingDetailsDto(
// 기본 예약 정보
String bookingId, // 예약 ID
String reservationName, // 예약자 이름
String reservationEmail, // 예약자 이메일
String reservationPhoneNumber, // 예약자 전화번호
String bookingName, // 예약자 이름
String bookingEmail, // 예약자 이메일
String bookingPhoneNumber, // 예약자 전화번호

// 항공편 정보
String flightId, // 항공편 ID
Expand All @@ -19,6 +19,7 @@ public record BookingDetailsDto(

// 탑승자 정보
String passengerName, // 탑승자 이름
LocalDate passengerBirthDate, // 탑승자 생년월일
int passengerAge, // 탑승자 나이
String passengerGender, // 탑승자 성별
String passengerPassportNumber, // 여권 번호
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public record BookingRequestDto(
String bookingName,
String bookingEmail,
String bookingPhoneNumber,
String birthDate,
String flightId, // 항공편명
String birthDate,
String paymentId // 결제 id
) {
}
Loading

0 comments on commit 6961a09

Please sign in to comment.