From 0d0a9f98b07be2164cbe769106658a160f4f23bb Mon Sep 17 00:00:00 2001 From: DebbieIsFree Date: Tue, 21 May 2024 17:11:19 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(#23)=20:=20=ED=95=AD=EA=B3=B5=EA=B6=8C?= =?UTF-8?q?(airplane)=20API=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/airplane/AirplaneBody.java | 16 +++ .../dto/airplane/AirplaneDto.java | 18 ++++ .../dto/airplane/AirplaneItem.java | 21 ++++ .../dto/airplane/AirplaneItems.java | 18 ++++ .../dto/airplane/AirplaneResponse.java | 16 +++ .../service/AirplaneService.java | 24 ++--- .../util/AirplaneInfoCollector.java | 99 ++++++------------- 7 files changed, 129 insertions(+), 83 deletions(-) create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneBody.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneDto.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItem.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItems.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneResponse.java diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneBody.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneBody.java new file mode 100644 index 0000000..7e66fb1 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneBody.java @@ -0,0 +1,16 @@ +package com.example.EnjoyTripBackend.dto.airplane; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class AirplaneBody { + private AirplaneItems items; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneDto.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneDto.java new file mode 100644 index 0000000..39865bb --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneDto.java @@ -0,0 +1,18 @@ +package com.example.EnjoyTripBackend.dto.airplane; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class AirplaneDto { + + private AirplaneResponse response; + +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItem.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItem.java new file mode 100644 index 0000000..07360b7 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItem.java @@ -0,0 +1,21 @@ +package com.example.EnjoyTripBackend.dto.airplane; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.*; + +@Getter +@Builder +@ToString +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class AirplaneItem { + private String airlineNm; + private String arrAirportNm; + private String arrPlandTime; + private String depAirportNm; + private String depPlandTime; + private String economyCharge; + private String prestigeCharge; + private String vihicleId; +} diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItems.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItems.java new file mode 100644 index 0000000..35b8797 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneItems.java @@ -0,0 +1,18 @@ +package com.example.EnjoyTripBackend.dto.airplane; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class AirplaneItems { + private List item; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneResponse.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneResponse.java new file mode 100644 index 0000000..01e37f4 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/airplane/AirplaneResponse.java @@ -0,0 +1,16 @@ +package com.example.EnjoyTripBackend.dto.airplane; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class AirplaneResponse { + private AirplaneBody body; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/AirplaneService.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/AirplaneService.java index 3b034f2..0b49513 100644 --- a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/AirplaneService.java +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/AirplaneService.java @@ -1,14 +1,12 @@ package com.example.EnjoyTripBackend.service; import com.example.EnjoyTripBackend.domain.Airplane; +import com.example.EnjoyTripBackend.dto.airplane.AirplaneItem; import com.example.EnjoyTripBackend.dto.NonPagingResponseResult; import com.example.EnjoyTripBackend.dto.PageRequestList; import com.example.EnjoyTripBackend.dto.ResponseResult; import com.example.EnjoyTripBackend.dto.airplane.AirplaneRequestDto; import com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto; -import com.example.EnjoyTripBackend.dto.golf.GolfRequestDto; -import com.example.EnjoyTripBackend.dto.golf.GolfResponseDto; -import com.example.EnjoyTripBackend.dto.place.PlaceResponseDto; import com.example.EnjoyTripBackend.exception.EnjoyTripException; import com.example.EnjoyTripBackend.exception.ErrorCode; import com.example.EnjoyTripBackend.repository.AirplaneRepository; @@ -18,8 +16,6 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; -import java.util.Date; -import java.util.Optional; @Service @RequiredArgsConstructor @@ -29,16 +25,16 @@ public class AirplaneService { private final AirplaneRepository airplaneRepository; @Transactional - public void save(AirplaneResponseDto airplaneResponseDto) { + public void save(AirplaneItem item) { Airplane airplane = Airplane.builder() - .airlineNm(airplaneResponseDto.getAirlineNm()) - .arrAirportNm(airplaneResponseDto.getArrAirportNm()) - .depAirportNm(airplaneResponseDto.getDepAirportNm()) - .arrPlandTime(airplaneResponseDto.getArrPlandTime()) - .depPlandTime(airplaneResponseDto.getDepPlandTime()) - .economyCharge(airplaneResponseDto.getEconomyCharge()) - .prestigeCharge(airplaneResponseDto.getPrestigeCharge()) - .vihicleId(airplaneResponseDto.getVihicleId()) + .airlineNm(item.getAirlineNm()) + .arrAirportNm(item.getArrAirportNm()) + .depAirportNm(item.getDepAirportNm()) + .arrPlandTime(item.getArrPlandTime()) + .depPlandTime(item.getDepPlandTime()) + .economyCharge(item.getEconomyCharge()) + .prestigeCharge(item.getPrestigeCharge()) + .vihicleId(item.getVihicleId()) .build(); airplaneRepository.save(airplane); diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/AirplaneInfoCollector.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/AirplaneInfoCollector.java index c403e05..6f363b2 100644 --- a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/AirplaneInfoCollector.java +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/AirplaneInfoCollector.java @@ -1,11 +1,10 @@ package com.example.EnjoyTripBackend.util; -import com.example.EnjoyTripBackend.domain.Airplane; -import com.example.EnjoyTripBackend.dto.airplane.AirplaneResponseDto; +import com.example.EnjoyTripBackend.dto.airplane.AirplaneDto; +import com.example.EnjoyTripBackend.dto.airplane.AirplaneItem; import com.example.EnjoyTripBackend.exception.EnjoyTripException; import com.example.EnjoyTripBackend.exception.ErrorCode; import com.example.EnjoyTripBackend.service.AirplaneService; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; @@ -22,87 +21,49 @@ @RequiredArgsConstructor public class AirplaneInfoCollector { - @Value("${flight.service.key}") + @Value("${airplane.url}") private String serviceKey; private final AirplaneService airplaneService; - private final RestTemplate restTemplate; - private final ObjectMapper objectMapper; - @Scheduled(cron = "0 48 19 * * *") // 매일 오후 7시 48분에 실행 + @Scheduled(cron = "* * * * * *") public void fetchFlightInfo() { LocalDate today = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); - List depAirportIds = List.of("NAARKJB", "NAARKJJ", "NAARKJK", "NAARKJY", "NAARKNW", "NAARKNY", "NAARKPC", "NAARKPK", "NAARKPS", "NAARKPU", "NAARKSI", "NAARKSS", "NAARKTH", "NAARKTN", "NAARKTU"); - List arrAirportIds = List.of("NAARKJB", "NAARKJJ", "NAARKJK", "NAARKJY", "NAARKNW", "NAARKNY", "NAARKPC", "NAARKPK", "NAARKPS", "NAARKPU", "NAARKSI", "NAARKSS", "NAARKTH", "NAARKTN", "NAARKTU"); + List airports = List.of("NAARKJB", "NAARKJJ", "NAARKJK", "NAARKJY", "NAARKNW", "NAARKNY", "NAARKPC", "NAARKPK", "NAARKPS", "NAARKPU", "NAARKSI", "NAARKSS", "NAARKTH", "NAARKTN", "NAARKTU"); List airlineIds = List.of("AAR", "ABL", "ASV", "ESR", "FGW", "HGG", "JJA", "JNA", "KAL", "TWB"); - for (int i = 0; i < 31; i++) { // Collect data for 31 days - LocalDate date = today.plusDays(i); - String depPlandTime = date.format(formatter); - - for (String depAirportId : depAirportIds) { - for (String arrAirportId : arrAirportIds) { - if (!depAirportId.equals(arrAirportId)) { // Ensure departure and arrival airports are different - for (String airlineId : airlineIds) { - fetchAndSaveFlightsForDate(depAirportId, arrAirportId, depPlandTime, airlineId); - } - } - } - } - } - } - - private void fetchAndSaveFlightsForDate(String depAirportId, String arrAirportId, String depPlandTime, String airlineId) { - String url = String.format("http://apis.data.go.kr/1613000/DmstcFlightNvgInfoService/getFlightOpratInfoList?serviceKey=%s&_type=json&depAirportId=%s&arrAirportId=%s&depPlandTime=%s&airlineId=%s", - serviceKey, depAirportId, arrAirportId, depPlandTime, airlineId); + String url = "http://apis.data.go.kr/1613000/DmstcFlightNvgInfoService/getFlightOpratInfoList?&_type=json&serviceKey=" + serviceKey; - ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); - - if (responseEntity.getStatusCode().is2xxSuccessful()) { - String responseBody = responseEntity.getBody(); - - try { - JsonNode rootNode = objectMapper.readTree(responseBody); - JsonNode itemsNode = rootNode.path("response").path("body").path("items").path("item"); - - if (itemsNode.isArray()) { - for (JsonNode itemNode : itemsNode) { - String airlineNm = getSafeText(itemNode, "airlineNm"); - String arrAirportNm = getSafeText(itemNode, "arrAirportNm"); - String depAirportNm = getSafeText(itemNode, "depAirportNm"); - String depPlandTimeStr = getSafeText(itemNode, "depPlandTime"); - String arrPlandTimeStr = getSafeText(itemNode, "arrPlandTime"); - String economyCharge = getSafeText(itemNode, "economyCharge"); - String prestigeCharge = getSafeText(itemNode, "prestigeCharge"); - String vihicleId = getSafeText(itemNode, "vihicleId"); - - if (arrAirportNm != null && depAirportNm != null && !arrAirportNm.equals(depAirportNm)) { - AirplaneResponseDto airplaneDto = new AirplaneResponseDto(); - airplaneDto.setAirlineNm(airlineNm); - airplaneDto.setArrAirportNm(arrAirportNm); - airplaneDto.setDepAirportNm(depAirportNm); - airplaneDto.setDepPlandTime(depPlandTimeStr); - airplaneDto.setArrPlandTime(arrPlandTimeStr); - airplaneDto.setEconomyCharge(economyCharge); - airplaneDto.setPrestigeCharge(prestigeCharge); - airplaneDto.setVihicleId(vihicleId); - - airplaneService.save(airplaneDto); + for (int i = 0; i < 31; i++) { + LocalDate date = today.plusDays(i); + String formattedDate = date.format(formatter); + + for (String depAirportId : airports) { + for (String arrAirportId : airports) { + if (!depAirportId.equals(arrAirportId)) { + final String targetUrl = url + "&depAirportId=" + depAirportId + "&arrAirportId=" + arrAirportId + "&depPlandTime=" + formattedDate; + RestTemplate rt = new RestTemplate(); + ResponseEntity response = rt.getForEntity(targetUrl, String.class); + ObjectMapper objectMapper = new ObjectMapper(); + + try { + AirplaneDto responseObject = objectMapper.readValue(response.getBody(), AirplaneDto.class); + + int size = responseObject.getResponse().getBody().getItems().getItem().size(); + + for (int n = 0; n < size; n++) { + AirplaneItem item = responseObject.getResponse().getBody().getItems().getItem().get(n); + airplaneService.save(item); + System.out.println(item); + } + } catch (Exception e) { + throw new EnjoyTripException(ErrorCode.FAIL_INSERT_AIRPLANE_DATA, e.getMessage()); } } } - } catch (Exception e) { - throw new EnjoyTripException(ErrorCode.FAIL_INSERT_AIRPLANE_DATA, e.getMessage()); } - } else { - throw new EnjoyTripException(ErrorCode.FAIL_INSERT_AIRPLANE_DATA, "Failed to fetch airplane data from the API"); } } - - private String getSafeText(JsonNode node, String key) { - JsonNode valueNode = node.get(key); - return valueNode != null ? valueNode.asText() : null; - } } From 4283e5366134c29bf6c6f71e0bba1853f6bf479a Mon Sep 17 00:00:00 2001 From: DebbieIsFree Date: Tue, 21 May 2024 17:18:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat(#28)=20:=20=EC=88=99=EB=B0=95=EC=8B=9C?= =?UTF-8?q?=EC=84=A4(house)=20API=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HouseController.java | 39 ++++++++++ .../EnjoyTripBackend/domain/House.java | 25 +++++++ .../EnjoyTripBackend/dto/house/HouseBody.java | 16 +++++ .../EnjoyTripBackend/dto/house/HouseDto.java | 18 +++++ .../EnjoyTripBackend/dto/house/HouseItem.java | 36 ++++++++++ .../dto/house/HouseItems.java | 18 +++++ .../dto/house/HouseRequestDto.java | 10 +++ .../dto/house/HouseResponse.java | 16 +++++ .../dto/house/HouseResponseDto.java | 22 ++++++ .../repository/HouseRepository.java | 18 +++++ .../service/HouseService.java | 71 +++++++++++++++++++ .../util/HouseInfoCollector.java | 48 +++++++++++++ .../main/resources/config/mybatis-config.xml | 1 + .../src/main/resources/mapper/house-query.xml | 42 +++++++++++ 14 files changed, 380 insertions(+) create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/HouseController.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/domain/House.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseBody.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseDto.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItem.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItems.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseRequestDto.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponse.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponseDto.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/HouseRepository.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/HouseService.java create mode 100644 EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/HouseInfoCollector.java create mode 100644 EnjoyTripBackend/src/main/resources/mapper/house-query.xml diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/HouseController.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/HouseController.java new file mode 100644 index 0000000..54af620 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/controller/HouseController.java @@ -0,0 +1,39 @@ +package com.example.EnjoyTripBackend.controller; + +import com.example.EnjoyTripBackend.dto.NonPagingResponseResult; +import com.example.EnjoyTripBackend.dto.ResponseResult; +import com.example.EnjoyTripBackend.dto.house.HouseRequestDto; +import com.example.EnjoyTripBackend.dto.house.HouseResponseDto; +import com.example.EnjoyTripBackend.service.HouseService; +import com.example.EnjoyTripBackend.util.LimitedSizePagination; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api") +@RequiredArgsConstructor +public class HouseController { + + private final HouseService houseService; + + @GetMapping("/houses") + public ResponseEntity>> houseList(@PageableDefault(size = 20) Pageable pageable){ + return ResponseEntity.ok().body(houseService.houseList(pageable)); + } + + @GetMapping("/houses/{id}") + public ResponseEntity> houseDetail(@PathVariable("id")String id){ + return ResponseEntity.ok().body(houseService.findById(id)); + } + + @PostMapping("/houses/search") + @LimitedSizePagination(maxSize = 20) + public ResponseEntity>> houseSearchList(@PageableDefault(size = 20) Pageable pageable, @RequestBody HouseRequestDto requestDto){ + return ResponseEntity.ok().body(houseService.houseSearchList(pageable, requestDto)); + } +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/domain/House.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/domain/House.java new file mode 100644 index 0000000..525ec2f --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/domain/House.java @@ -0,0 +1,25 @@ +package com.example.EnjoyTripBackend.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class House { + private String addr1; + private String addr2; + private String areacode; + private String contentid; + private String firstimage; + private String mapx; + private String mapy; + private String mlevel; + private String tel; + private String title; + private String sigungucode; + +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseBody.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseBody.java new file mode 100644 index 0000000..d08cde5 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseBody.java @@ -0,0 +1,16 @@ +package com.example.EnjoyTripBackend.dto.house; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class HouseBody { + private HouseItems items; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseDto.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseDto.java new file mode 100644 index 0000000..2275d05 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseDto.java @@ -0,0 +1,18 @@ +package com.example.EnjoyTripBackend.dto.house; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class HouseDto { + + private HouseResponse response; + +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItem.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItem.java new file mode 100644 index 0000000..7530ffb --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItem.java @@ -0,0 +1,36 @@ +package com.example.EnjoyTripBackend.dto.house; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.*; + +@Getter +@Builder +@ToString +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class HouseItem { + private String addr1; + private String addr2; + private String areacode; + private String benikia; + private String cat1; + private String cat2; + private String cat3; + private String contentid; + private String contenttypeid; + private String createdtime; + private String firstimage; + private String firstimage2; + private String cpyrhtDivCd; + private String goodstay; + private String hanok; + private String mapx; + private String mapy; + private String mlevel; + private String modifiedtime; + private String tel; + private String title; + private String booktour; + private String sigungucode; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItems.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItems.java new file mode 100644 index 0000000..958478c --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseItems.java @@ -0,0 +1,18 @@ +package com.example.EnjoyTripBackend.dto.house; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class HouseItems { + private List item; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseRequestDto.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseRequestDto.java new file mode 100644 index 0000000..e3508a3 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseRequestDto.java @@ -0,0 +1,10 @@ +package com.example.EnjoyTripBackend.dto.house; + +import lombok.*; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class HouseRequestDto { + private String areacode; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponse.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponse.java new file mode 100644 index 0000000..29ae114 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponse.java @@ -0,0 +1,16 @@ +package com.example.EnjoyTripBackend.dto.house; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class HouseResponse { + private HouseBody body; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponseDto.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponseDto.java new file mode 100644 index 0000000..e874c5d --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/dto/house/HouseResponseDto.java @@ -0,0 +1,22 @@ +package com.example.EnjoyTripBackend.dto.house; + +import lombok.*; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class HouseResponseDto { + private Long id; + private String addr1; + private String addr2; + private String areacode; + private String contentid; + private String firstimage; + private String mapx; + private String mapy; + private String mlevel; + private String tel; + private String title; + private String sigungucode; +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/HouseRepository.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/HouseRepository.java new file mode 100644 index 0000000..b793a6d --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/repository/HouseRepository.java @@ -0,0 +1,18 @@ +package com.example.EnjoyTripBackend.repository; + +import com.example.EnjoyTripBackend.domain.House; +import com.example.EnjoyTripBackend.dto.PageRequestList; +import com.example.EnjoyTripBackend.dto.house.HouseResponseDto; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; +import java.util.Optional; + +@Mapper +public interface HouseRepository { + Long save(House house); + List findAll(PageRequestList requestList); + Optional findById(String id); + List findAllBySearch(PageRequestList requestList); + Long findTotalCount(); +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/HouseService.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/HouseService.java new file mode 100644 index 0000000..1a459db --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/service/HouseService.java @@ -0,0 +1,71 @@ +package com.example.EnjoyTripBackend.service; + +import com.example.EnjoyTripBackend.domain.House; +import com.example.EnjoyTripBackend.dto.NonPagingResponseResult; +import com.example.EnjoyTripBackend.dto.PageRequestList; +import com.example.EnjoyTripBackend.dto.ResponseResult; +import com.example.EnjoyTripBackend.dto.house.HouseRequestDto; +import com.example.EnjoyTripBackend.dto.house.HouseResponseDto; +import com.example.EnjoyTripBackend.dto.house.HouseItem; +import com.example.EnjoyTripBackend.exception.EnjoyTripException; +import com.example.EnjoyTripBackend.exception.ErrorCode; +import com.example.EnjoyTripBackend.repository.HouseRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class HouseService { + + private final HouseRepository houseRepository; + + @Transactional + public void save(HouseItem item) { + House house = House.builder() + .addr1(item.getAddr1()) + .addr2(item.getAddr2()) + .areacode(item.getAreacode()) + .contentid(item.getContentid()) + .firstimage(item.getFirstimage()) + .mapx(item.getMapx()) + .mapy(item.getMapy()) + .mlevel(item.getMlevel()) + .tel(item.getTel()) + .title(item.getTitle()) + .sigungucode(item.getSigungucode()) + .build(); + + houseRepository.save(house); + } + + public ResponseResult> houseList(Pageable pageable) { + PageRequestList requestList = PageRequestList.builder() + .pageable(pageable) + .build(); + + long totalCount = houseRepository.findTotalCount(); + int totalPages = (int) Math.ceil((double) totalCount / pageable.getPageSize()); + + return ResponseResult.of("숙박시설 정보 게시글 목록입니다.", houseRepository.findAll(requestList),totalPages); + } + + public NonPagingResponseResult findById(String id) { + return NonPagingResponseResult.of("숙박시설 상세 정보 게시글 입니다.", houseRepository.findById(id).orElseThrow(() -> new EnjoyTripException(ErrorCode.CONTENT_NOT_FOUNT))); + } + + public ResponseResult> houseSearchList(Pageable pageable, HouseRequestDto houseRequestDto) { + PageRequestList requestList = PageRequestList.builder() + .pageable(pageable) + .data(houseRequestDto) + .build(); + List houses = houseRepository.findAllBySearch(requestList); + long totalCount = houses.size(); + int totalPages = (int) Math.ceil((double) totalCount / pageable.getPageSize()); + return ResponseResult.of("검색어 기반 숙박시설 정보 게시글 목록입니다.", houses, totalPages); + } +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/HouseInfoCollector.java b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/HouseInfoCollector.java new file mode 100644 index 0000000..f8753f1 --- /dev/null +++ b/EnjoyTripBackend/src/main/java/com/example/EnjoyTripBackend/util/HouseInfoCollector.java @@ -0,0 +1,48 @@ +package com.example.EnjoyTripBackend.util; + +import com.example.EnjoyTripBackend.dto.house.HouseDto; +import com.example.EnjoyTripBackend.dto.house.HouseItem; +import com.example.EnjoyTripBackend.exception.EnjoyTripException; +import com.example.EnjoyTripBackend.exception.ErrorCode; +import com.example.EnjoyTripBackend.service.HouseService; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import org.springframework.beans.factory.annotation.Value; + +import java.net.URISyntaxException; + +@Component +@RequiredArgsConstructor +public class HouseInfoCollector { + + @Value("${house.url}") + private String url; + + private final HouseService houseService; + + @Scheduled(cron = "0 0 0 1 * ?") // 매월 1일 자정에 실행 + public void houseInfoCollector() throws URISyntaxException { + + RestTemplate rt = new RestTemplate(); + + ResponseEntity response = rt.getForEntity(url, String.class); + ObjectMapper objectMapper = new ObjectMapper(); + + try { + HouseDto responseObject = objectMapper.readValue(response.getBody(), HouseDto.class); + + int size = responseObject.getResponse().getBody().getItems().getItem().size(); + + for(int i = 0; i < size ; i++) { + HouseItem item = responseObject.getResponse().getBody().getItems().getItem().get(i); + houseService.save(item); + } + } catch (Exception e) { + throw new EnjoyTripException(ErrorCode.FAIL_INSERT_GOLF_DATA, e.getMessage()); + } + } +} \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml b/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml index 4c5ae3a..4166eab 100644 --- a/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml +++ b/EnjoyTripBackend/src/main/resources/config/mybatis-config.xml @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/EnjoyTripBackend/src/main/resources/mapper/house-query.xml b/EnjoyTripBackend/src/main/resources/mapper/house-query.xml new file mode 100644 index 0000000..c3789ea --- /dev/null +++ b/EnjoyTripBackend/src/main/resources/mapper/house-query.xml @@ -0,0 +1,42 @@ + + + + + + + INSERT + INTO house (addr1, addr2, areacode, contentid, firstimage, mapx, mapy, mlevel, tel, title, sigungucode) + VALUES (#{addr1}, #{addr2}, #{areacode}, #{contentid}, #{firstimage}, #{mapx}, #{mapy}, #{mlevel}, #{tel}, #{title}, #{sigungucode}) + + + + + + + + + + +