Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend/feature/25 amadeus api #66

Merged
merged 23 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a810017
fix : 리펙토링 코드
Dayon-Hong Apr 8, 2024
a66ff72
fix : 리펙토링 코드 2
Dayon-Hong Apr 9, 2024
6bd7a88
fix : 리펙토링 코드
Dayon-Hong Apr 8, 2024
2f107f2
fix : 리펙토링 코드 2
Dayon-Hong Apr 9, 2024
2905883
fix : 항공권 정보 조회 data 정제
Dayon-Hong May 12, 2024
0790513
feat : 스카이스캐너 url 생성, 좋아요 관련 API
Dayon-Hong May 12, 2024
7d30fde
feat : 항공권 관련 error 코드 생성
Dayon-Hong May 12, 2024
4786323
refactor : 불필요한 주석 해제 및 함수 정리 #40
Dayon-Hong May 12, 2024
f413bf5
feat : 스카이스캐너 url 생성 DTO 수정
Dayon-Hong May 12, 2024
83caebc
feat : flight 관련 DB 데이터, DTO 정리
Dayon-Hong May 12, 2024
7dbac56
Merge branch 'backend/feature/25-amadeus-api' of https://github.com/t…
Dayon-Hong May 12, 2024
e881aab
fix : 리펙토링 코드
Dayon-Hong Apr 8, 2024
1c2a72d
fix : 리펙토링 코드 2
Dayon-Hong Apr 9, 2024
acfb59e
fix : 항공권 정보 조회 data 정제
Dayon-Hong May 12, 2024
dd1a8c8
feat : 스카이스캐너 url 생성, 좋아요 관련 API
Dayon-Hong May 12, 2024
961fedf
feat : 항공권 관련 error 코드 생성
Dayon-Hong May 12, 2024
d5e85e8
refactor : 불필요한 주석 해제 및 함수 정리 #40
Dayon-Hong May 12, 2024
6e67c42
feat : 스카이스캐너 url 생성 DTO 수정
Dayon-Hong May 12, 2024
ce9b527
feat : flight 관련 DB 데이터, DTO 정리
Dayon-Hong May 12, 2024
631b11b
fix : 리펙토링 코드 2
Dayon-Hong Apr 9, 2024
199b7fe
Merge branch 'backend/feature/25-amadeus-api' of https://github.com/t…
Dayon-Hong May 12, 2024
1d8d5b8
fix : 리펙토링 코드 수정
Dayon-Hong May 12, 2024
86bd0af
fix : url 주소 변경
Dayon-Hong May 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/countries")
@RequiredArgsConstructor
Expand All @@ -21,11 +23,12 @@ public class CountryController {
@PostMapping("/locations")
public ResponseEntity<LocationResponse> findLocation(@RequestBody LocationRequest requestDTO) {
String country = requestDTO.getCountry();
LocationResponse responseDTO = countryService.findLocationByCity(country);
if (responseDTO == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Optional<LocationResponse> responseDTO = countryService.findLocationByCity(country);
if (responseDTO.isPresent()) {
return ResponseEntity.ok(responseDTO.get());
} else {
return ResponseEntity.notFound().build();
}
return new ResponseEntity<>(responseDTO, HttpStatus.OK);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.isp.backend.domain.country.entity.Country;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CountryRepository extends JpaRepository<Country, Long> {
import java.util.Optional;

Country findIdByCity(String city); // 여행나라 이름으로 id 찾기
public interface CountryRepository extends JpaRepository<Country, Long> {

Country findCountryById(Long countryId); //
Country findAirportCodeByCity(String countryName); // 나라 이름으로 공항 코드 찾기
Optional<Country> findIdByCity(String city); // 여행나라 이름으로 id 찾기
Country findCountryById(Long countryId);
Optional<Country> findAirportCodeByCity(String countryName); // 나라 이름으로 공항 코드 찾기
Optional<Country> findCountryByAirportCode(String airportCode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


@Service
public class CountryService {
Expand All @@ -17,15 +19,14 @@ public class CountryService {


/** 여행지 좌표 찾기 **/
public LocationResponse findLocationByCity(String city) {
Country country = countryRepository.findIdByCity(city);
if (country == null) {
throw new CountryNotFoundException();
}
LocationResponse locationDTO = new LocationResponse();
locationDTO.setLatitude(country.getLatitude());
locationDTO.setLongitude(country.getLongitude());
return locationDTO;
public Optional<LocationResponse> findLocationByCity(String city) {
Optional<Country> findCountry = countryRepository.findIdByCity(city);
return findCountry.map(country -> {
LocationResponse locationDTO = new LocationResponse();
locationDTO.setLatitude(country.getLatitude());
locationDTO.setLongitude(country.getLongitude());
return locationDTO;
});
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.isp.backend.domain.flight.controller;

import com.amadeus.exceptions.ResponseException;
import com.isp.backend.domain.flight.dto.request.FlightLikeRequest;
import com.isp.backend.domain.flight.dto.request.SkyScannerRequest;
import com.isp.backend.domain.flight.dto.response.FlightLikeResponse;
import com.isp.backend.domain.flight.service.FlightOfferService;
import com.isp.backend.domain.flight.dto.request.FlightSearchRequest;
import com.isp.backend.global.exception.flight.FlightSearchFailedException;
import com.isp.backend.global.exception.flight.SkyScannerGenerateFailedException;
import com.isp.backend.global.security.CustomUserDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bookings/flights")
Expand All @@ -22,20 +26,58 @@ public class FlightOfferController {
@Autowired
private FlightOfferService flightOfferService;

/**
* 항공권 검색 API
*/
@GetMapping("/flights")
/** 항공권 검색 API **/
@GetMapping("/search")
public ResponseEntity<String> getFlightOffers(@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody FlightSearchRequest request) {
String memberUid = customUserDetails.getUsername();
try {
String flightOffersJson = flightOfferService.getFlightOffers(request);
return ResponseEntity.ok(flightOffersJson);
} catch (ResponseException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred while fetching flight offers");
throw new FlightSearchFailedException();
}
}


/** 항공권 선택시 스카이스캐너 사이트로 연결 API **/
@PostMapping("/connect")
public ResponseEntity<String> getFlightSearchUrl(@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody SkyScannerRequest request) {
String memberUid = customUserDetails.getUsername();
try {
String skyscannerUrl = flightOfferService.generateSkyscannerUrl(request);
return ResponseEntity.ok("{\"skyscannerUrl\": \"" + skyscannerUrl + "\"}");
} catch (Exception e) {
throw new SkyScannerGenerateFailedException();
}
}


/** 항공권 좋아요 저장 API **/
@PostMapping("/like")
public ResponseEntity<Void> addLikeFlight(@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody FlightLikeRequest flightLikeRequest) {
String memberUid = customUserDetails.getUsername();
flightOfferService.addLikeFlight(memberUid, flightLikeRequest);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

/** 항공권 나의 좋아요 목록 불러오기 API **/
@GetMapping("/likes")
public ResponseEntity<List<FlightLikeResponse>> getLikedFlights(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
String memberUid = customUserDetails.getUsername();
List<FlightLikeResponse> likedFlights = flightOfferService.getLikedFlights(memberUid);
return ResponseEntity.ok(likedFlights);
}

/** 항공권 나의 좋아요 삭제 API **/
@DeleteMapping("/like/{id}")
public ResponseEntity<Void> deleteLikeFlight(@AuthenticationPrincipal CustomUserDetails customUserDetails,
@PathVariable Long id) {
String memberUid = customUserDetails.getUsername();
flightOfferService.deleteLikeFlight(memberUid, id);
return ResponseEntity.ok().build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.isp.backend.domain.flight.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class FlightLikeRequest {

private String carrierCode;

private int totalPrice;

private String abroadDuration;

private String abroadDepartureTime;

private String abroadArrivalTime;

private String homeDuration;

private String homeDepartureTime;

private String homeArrivalTime;

private String departureIataCode;

private String arrivalIataCode;

private String transferCount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.isp.backend.domain.flight.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Getter
public class SkyScannerRequest {

private String departureIataCode;

private String arrivalIataCode;

private String departureDate;

private String returnDate;

private int adults;

private int children;

private String departureTime;

private int transferCount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.isp.backend.domain.flight.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class FlightLikeResponse {

private Long id;

private String carrierCode;

private int totalPrice;

private String abroadDuration;

private String abroadDepartureTime;

private String abroadArrivalTime;

private String homeDuration;

private String homeDepartureTime;

private String homeArrivalTime;

private String departureIataCode;

private String arrivalIataCode;

private String transferCount;

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
package com.isp.backend.domain.flight.entity;

import com.isp.backend.domain.country.entity.Country;
import com.isp.backend.domain.member.entity.Member;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@Entity
@Builder
@NoArgsConstructor
@Table(name = "flight")
public class Flight {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;

private String carrierCode ;

private Double price ;

private String abroadDuration ;

private String abroadDepartureTime ; // 출국-출발

private String abroadArrivalTime ; // 출국-도착

@ManyToOne
@JoinColumn(name = "departure_iata_code_id")
private Country departureIataCode ;

private String homeDuration ;

private String homeDepartureTime ; // 입국-출발

private String homeArrivalTime ; // 입국-도착
@ManyToOne
@JoinColumn(name = "arrival_iata_code_id")
private Country arrivalIataCode ;

private int transferCount ;


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.isp.backend.domain.flight.mapper;

import com.isp.backend.domain.country.entity.Country;
import com.isp.backend.domain.flight.dto.request.FlightLikeRequest;
import com.isp.backend.domain.flight.dto.response.FlightLikeResponse;
import com.isp.backend.domain.flight.entity.Flight;
import com.isp.backend.domain.member.entity.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class FlightMapper {

public Flight toEntity(FlightLikeRequest request, Member member, Country departureIataCode, Country arrivalIataCode) {
return Flight.builder()
.member(member) // 주입된 member 객체 사용
.carrierCode(request.getCarrierCode())
.price((double) request.getTotalPrice())
.abroadDuration(request.getAbroadDuration())
.abroadDepartureTime(request.getAbroadDepartureTime())
.abroadArrivalTime(request.getAbroadArrivalTime())
.homeDuration(request.getHomeDuration())
.homeDepartureTime(request.getHomeDepartureTime())
.homeArrivalTime(request.getHomeArrivalTime())
.departureIataCode(departureIataCode)
.arrivalIataCode(arrivalIataCode)
.transferCount(Integer.parseInt(request.getTransferCount()))
.build();
}

public FlightLikeResponse toFlightLikeRequest(Flight flight) {
FlightLikeResponse response = new FlightLikeResponse();
response.setId(flight.getId());
response.setCarrierCode(flight.getCarrierCode());
response.setTotalPrice((int) Math.round(flight.getPrice()));
response.setAbroadDuration(flight.getAbroadDuration());
response.setAbroadDepartureTime(flight.getAbroadDepartureTime());
response.setAbroadArrivalTime(flight.getAbroadArrivalTime());
response.setHomeDuration(flight.getHomeDuration());
response.setHomeDepartureTime(flight.getHomeDepartureTime());
response.setHomeArrivalTime(flight.getHomeArrivalTime());
response.setDepartureIataCode(flight.getDepartureIataCode().getAirportCode());
response.setArrivalIataCode(flight.getArrivalIataCode().getAirportCode());
response.setTransferCount(String.valueOf(flight.getTransferCount()));
return response;
}

public List<FlightLikeResponse> toFlightLikeRequestList(List<Flight> flights) {
return flights.stream()
.map(this::toFlightLikeRequest)
.collect(Collectors.toList());
}

}
Loading
Loading