Skip to content

Commit

Permalink
Merge pull request #66 from tukcomCD2024/backend/feature/25-amadeus-api
Browse files Browse the repository at this point in the history
Backend/feature/25 amadeus api
  • Loading branch information
Dayon-Hong authored May 12, 2024
2 parents b5b864b + 86bd0af commit 50e1d5f
Show file tree
Hide file tree
Showing 32 changed files with 758 additions and 267 deletions.
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

0 comments on commit 50e1d5f

Please sign in to comment.