From 90b04ef070371038fc2fd324e2e17fae8411a7ce Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Fri, 15 Mar 2024 17:29:12 +0900 Subject: [PATCH 01/10] =?UTF-8?q?feature:=20SpaceCloud=20API,=20Kakao=20?= =?UTF-8?q?=EC=A3=BC=EC=86=8C=20API=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license | 1 + .../config/auth/SecurityConfiguration.java | 1 + .../data/controller/DataController.java | 52 ++++++++++++ .../data/controller/dto/AddressResponse.java | 31 +++++++ .../data/controller/dto/SpaceResponse.java | 83 +++++++++++++++++++ .../data/controller/dto/address/Document.java | 20 +++++ .../controller/dto/address/JibunAddress.java | 31 +++++++ .../controller/dto/address/RoadAddress.java | 25 ++++++ .../data/controller/dto/space/BreakDay.java | 18 ++++ .../controller/dto/space/BreakHoliday.java | 18 ++++ .../data/controller/dto/space/BreakTime.java | 18 ++++ .../dto/space/FacilityCategory.java | 13 +++ .../controller/dto/space/FacilityInfo.java | 23 +++++ .../data/controller/dto/space/Location.java | 18 ++++ .../data/controller/dto/space/Product.java | 31 +++++++ .../data/controller/dto/space/SpaceInfo.java | 17 ++++ 16 files changed, 400 insertions(+) create mode 100644 src/main/java/com/modoospace/data/controller/DataController.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/AddressResponse.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/address/Document.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/Location.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/Product.java create mode 100644 src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java diff --git a/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license b/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license index e69de29..5c094a6 100644 --- a/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license +++ b/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license @@ -0,0 +1 @@ +56ec58ecb84b4c05943ffe343681d0d3 \ No newline at end of file diff --git a/src/main/java/com/modoospace/config/auth/SecurityConfiguration.java b/src/main/java/com/modoospace/config/auth/SecurityConfiguration.java index fad162d..923a383 100644 --- a/src/main/java/com/modoospace/config/auth/SecurityConfiguration.java +++ b/src/main/java/com/modoospace/config/auth/SecurityConfiguration.java @@ -22,6 +22,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .authorizeHttpRequests(request -> request .antMatchers(HttpMethod.GET, "/", "/error", "/api/v1/spaces/**", "/api/v1/spaces/*/facilities/**", + "/api/v1/test/**", "/api/v1/facilities/*/schedules/**", "/api/v1/visitors/reservations/facilities/*/availability/**").permitAll() .antMatchers(HttpMethod.POST, "/api/v1/alarms/send/**").permitAll() diff --git a/src/main/java/com/modoospace/data/controller/DataController.java b/src/main/java/com/modoospace/data/controller/DataController.java new file mode 100644 index 0000000..b6a7f3d --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/DataController.java @@ -0,0 +1,52 @@ +package com.modoospace.data.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.modoospace.data.controller.dto.AddressResponse; +import com.modoospace.data.controller.dto.SpaceResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/test") +public class DataController { + + private final ObjectMapper objectMapper = new ObjectMapper(); + private final HttpClient client = HttpClient.newHttpClient(); + + @Value("${spring.kakao.apikey}") + private String key; + + @GetMapping("/space/{spaceId}") + public SpaceResponse getSpace(@PathVariable String spaceId) throws IOException, InterruptedException { + HttpRequest spaceRequest = HttpRequest.newBuilder() + .uri(URI.create("https://new-api.spacecloud.kr/spaces/" + spaceId)) + .build(); + HttpResponse httpSpaceResponse = client.send(spaceRequest, HttpResponse.BodyHandlers.ofString()); + String body = httpSpaceResponse.body(); + return objectMapper.readValue(body, SpaceResponse.class); + } + + @GetMapping("/address") + public AddressResponse getAddress(String address) throws IOException, InterruptedException { + String encodedAddress = URLEncoder.encode(address, StandardCharsets.UTF_8); + HttpRequest addressRequest = HttpRequest.newBuilder() + .uri(URI.create("https://dapi.kakao.com/v2/local/search/address?query="+encodedAddress)) + .header("Authorization", "KakaoAK " + key) + .build(); + HttpResponse httpAddressResponse = client.send(addressRequest, HttpResponse.BodyHandlers.ofString()); + return objectMapper.readValue(httpAddressResponse.body(), AddressResponse.class); + } +} diff --git a/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java b/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java new file mode 100644 index 0000000..cb91de0 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java @@ -0,0 +1,31 @@ +package com.modoospace.data.controller.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.modoospace.data.controller.dto.address.Document; +import com.modoospace.space.domain.Address; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class AddressResponse { + + List documents; + + public Address toAddress() { + Document document = documents.get(0); + return Address.builder() + .depthFirst(document.getAddress().getDepthFirst()) + .depthSecond(document.getAddress().getDepthSecond()) + .depthThird(document.getAddress().getDepthThird()) + .addressNo(document.getAddress().getFullAddressNo()) + .roadName(document.getRoadAddress().getRoadName()) + .buildingNo(document.getRoadAddress().getFullBuildingNo()) + .x(document.getX()) + .y(document.getY()) + .build(); + } +} diff --git a/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java b/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java new file mode 100644 index 0000000..0ac173f --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java @@ -0,0 +1,83 @@ +package com.modoospace.data.controller.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.modoospace.data.controller.dto.space.*; +import com.modoospace.member.domain.Member; +import com.modoospace.space.domain.*; +import lombok.Getter; +import lombok.Setter; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.time.DayOfWeek; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class SpaceResponse { + + private List products; + private SpaceInfo info; + private Location location; + private List break_days; + private List break_holidays; + private List break_times; + + public Optional getEncodedAddress() { + return Optional.ofNullable(location) + .map(Location::getAddress) + .map(address -> URLEncoder.encode(address, StandardCharsets.UTF_8)); + } + + public Optional getCategoryName() { + return products.stream() + .findFirst() + .flatMap(p -> p.getCategories().stream().findFirst()) + .map(FacilityCategory::getName); + } + + public Space toSpace(Address address, Category category, Member member) { + return Space.builder() + .name(info.getName()) + .description(info.getDescription()) + .address(address) + .category(category) + .host(member) + .build(); + } + + public List toFacilities(Space space) { + return null; + } + + public WeekdaySettings getWeekdaySettings() { + List weekdaySettings = new ArrayList<>(Arrays.asList( + new WeekdaySetting(DayOfWeek.MONDAY), + new WeekdaySetting(DayOfWeek.TUESDAY), + new WeekdaySetting(DayOfWeek.WEDNESDAY), + new WeekdaySetting(DayOfWeek.THURSDAY), + new WeekdaySetting(DayOfWeek.FRIDAY)) + ); + + if (break_days.isEmpty() && break_holidays.isEmpty()) { + weekdaySettings.add(new WeekdaySetting(DayOfWeek.SATURDAY)); + weekdaySettings.add(new WeekdaySetting(DayOfWeek.SUNDAY)); + } + + return new WeekdaySettings(weekdaySettings); + } + + public TimeSettings getTimeSettings() { + List timeSettings = break_times.stream() + .map(breakTime -> new TimeRange(breakTime.getStartHour(), breakTime.getEndHour())) + .map(TimeSetting::new) + .collect(Collectors.toList()); + + return new TimeSettings(timeSettings); + } +} diff --git a/src/main/java/com/modoospace/data/controller/dto/address/Document.java b/src/main/java/com/modoospace/data/controller/dto/address/Document.java new file mode 100644 index 0000000..d7ae968 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/address/Document.java @@ -0,0 +1,20 @@ +package com.modoospace.data.controller.dto.address; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class Document { + + JibunAddress address; + + @JsonProperty("road_address") + RoadAddress roadAddress; + + String x; + String y; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java b/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java new file mode 100644 index 0000000..dcc8518 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java @@ -0,0 +1,31 @@ +package com.modoospace.data.controller.dto.address; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class JibunAddress { + + @JsonProperty("region_1depth_name") + String depthFirst; + + @JsonProperty("region_2depth_name") + String depthSecond; + + @JsonProperty("region_3depth_name") + String depthThird; + + @JsonProperty("main_address_no") + String addressNo; + + @JsonProperty("sub_address_no") + String subAddressNo; + + public String getFullAddressNo() { + return subAddressNo != null ? addressNo + "-" + subAddressNo : addressNo; + } +} diff --git a/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java b/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java new file mode 100644 index 0000000..86912d9 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java @@ -0,0 +1,25 @@ +package com.modoospace.data.controller.dto.address; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class RoadAddress { + + @JsonProperty("road_name") + String roadName; + + @JsonProperty("main_building_no") + String buildingNo; + + @JsonProperty("sub_building_no") + String subBuildingNo; + + public String getFullBuildingNo() { + return buildingNo + "-" + subBuildingNo; + } +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java b/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java new file mode 100644 index 0000000..983a379 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java @@ -0,0 +1,18 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class BreakDay { + + @JsonProperty("BRK_DAY_TP_CD") + String breakDayCd; // HOLID + + @JsonProperty("DAYW_CD") + String dayCd; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java b/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java new file mode 100644 index 0000000..3cb5e1a --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java @@ -0,0 +1,18 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class BreakHoliday { + + @JsonProperty("BRK_DAY_TP_CD") + String breakDayCd; // HOLID + + @JsonProperty("DAYW_CD") + String dayCd; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java b/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java new file mode 100644 index 0000000..cf56758 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java @@ -0,0 +1,18 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class BreakTime { + + @JsonProperty("end_time") + Integer startHour; + + @JsonProperty("start_time") + Integer endHour; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java b/src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java new file mode 100644 index 0000000..0e2d016 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java @@ -0,0 +1,13 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class FacilityCategory { + + String name; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java b/src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java new file mode 100644 index 0000000..9691ab6 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java @@ -0,0 +1,23 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class FacilityInfo { + + String name; + + @JsonProperty("desc") + String description; + + @JsonProperty("min_guest_policy") + Integer minUser = 1; + + @JsonProperty("max_guest_capacity") + Integer maxUser = 10; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/Location.java b/src/main/java/com/modoospace/data/controller/dto/space/Location.java new file mode 100644 index 0000000..8b5a21e --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/Location.java @@ -0,0 +1,18 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class Location { + + @JsonProperty("addr") + String address; + + @JsonProperty("addr_detail") + String detailAddress; +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/Product.java b/src/main/java/com/modoospace/data/controller/dto/space/Product.java new file mode 100644 index 0000000..d2a4eb5 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/Product.java @@ -0,0 +1,31 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.modoospace.space.domain.Facility; +import com.modoospace.space.domain.TimeSettings; +import com.modoospace.space.domain.WeekdaySettings; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class Product { + + FacilityInfo info; + List categories; + + public Facility toFacility(TimeSettings timeSettings, WeekdaySettings weekdaySettings) { + return Facility.builder() + .name(info.getName()) + .reservationEnable(true) + .minUser(info.getMinUser()) + .minUser(info.getMaxUser()) + .description(info.getDescription()) + .timeSettings(timeSettings) + .weekdaySettings(weekdaySettings) + .build(); + } +} diff --git a/src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java b/src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java new file mode 100644 index 0000000..7091a17 --- /dev/null +++ b/src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java @@ -0,0 +1,17 @@ +package com.modoospace.data.controller.dto.space; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class SpaceInfo { + + String name; + + @JsonProperty("desc") + String description; +} From eea299d900a32ec5bcd51ed86faa477c9f8846a2 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Fri, 15 Mar 2024 17:30:44 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feature:=20key=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license | 1 - 1 file changed, 1 deletion(-) diff --git a/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license b/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license index 5c094a6..e69de29 100644 --- a/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license +++ b/pinpoint-agent-2.2.3-NCP-RC1/pinpoint.license @@ -1 +0,0 @@ -56ec58ecb84b4c05943ffe343681d0d3 \ No newline at end of file From 323101d5b8aca33374b78deed82d9352a2026bf6 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 12:28:03 +0900 Subject: [PATCH 03/10] =?UTF-8?q?refactor:=20response=20=ED=95=84=EB=93=9C?= =?UTF-8?q?=20=EB=AA=85=EC=B9=AD=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- .../data/controller/dto/AddressResponse.java | 3 +- .../data/controller/dto/SpaceResponse.java | 64 +++++++++++-------- .../controller/dto/address/JibunAddress.java | 2 +- .../controller/dto/address/RoadAddress.java | 2 +- .../data/controller/dto/space/BreakDay.java | 2 +- .../controller/dto/space/BreakHoliday.java | 2 +- .../data/controller/dto/space/BreakTime.java | 4 +- .../{Product.java => FacilityResponse.java} | 8 ++- 8 files changed, 51 insertions(+), 36 deletions(-) rename src/main/java/com/modoospace/data/controller/dto/space/{Product.java => FacilityResponse.java} (81%) diff --git a/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java b/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java index cb91de0..88a5dac 100644 --- a/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java +++ b/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java @@ -15,7 +15,7 @@ public class AddressResponse { List documents; - public Address toAddress() { + public Address toAddress(String detailAddress) { Document document = documents.get(0); return Address.builder() .depthFirst(document.getAddress().getDepthFirst()) @@ -24,6 +24,7 @@ public Address toAddress() { .addressNo(document.getAddress().getFullAddressNo()) .roadName(document.getRoadAddress().getRoadName()) .buildingNo(document.getRoadAddress().getFullBuildingNo()) + .detailAddress(detailAddress) .x(document.getX()) .y(document.getY()) .build(); diff --git a/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java b/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java index 0ac173f..d6212da 100644 --- a/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java +++ b/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java @@ -1,19 +1,17 @@ package com.modoospace.data.controller.dto; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; import com.modoospace.data.controller.dto.space.*; import com.modoospace.member.domain.Member; import com.modoospace.space.domain.*; import lombok.Getter; import lombok.Setter; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.time.DayOfWeek; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; @Getter @@ -21,40 +19,41 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class SpaceResponse { - private List products; - private SpaceInfo info; + @JsonProperty("products") + private List facilityResponses = new ArrayList<>(); + + @JsonProperty("info") + private SpaceInfo spaceInfo; + private Location location; - private List break_days; - private List break_holidays; - private List break_times; - - public Optional getEncodedAddress() { - return Optional.ofNullable(location) - .map(Location::getAddress) - .map(address -> URLEncoder.encode(address, StandardCharsets.UTF_8)); - } - public Optional getCategoryName() { - return products.stream() + @JsonProperty("break_days") + private List breakDays = new ArrayList<>(); + + @JsonProperty("break_holidays") + private List breakHolidays = new ArrayList<>(); + + @JsonProperty("break_times") + private List breakTimes = new ArrayList<>(); + + public String getCategoryName() { + return facilityResponses.stream() .findFirst() .flatMap(p -> p.getCategories().stream().findFirst()) - .map(FacilityCategory::getName); + .map(FacilityCategory::getName) + .get(); } public Space toSpace(Address address, Category category, Member member) { return Space.builder() - .name(info.getName()) - .description(info.getDescription()) + .name(spaceInfo.getName()) + .description(spaceInfo.getDescription()) .address(address) .category(category) .host(member) .build(); } - public List toFacilities(Space space) { - return null; - } - public WeekdaySettings getWeekdaySettings() { List weekdaySettings = new ArrayList<>(Arrays.asList( new WeekdaySetting(DayOfWeek.MONDAY), @@ -64,7 +63,7 @@ public WeekdaySettings getWeekdaySettings() { new WeekdaySetting(DayOfWeek.FRIDAY)) ); - if (break_days.isEmpty() && break_holidays.isEmpty()) { + if (breakDays.isEmpty() && breakHolidays.isEmpty()) { weekdaySettings.add(new WeekdaySetting(DayOfWeek.SATURDAY)); weekdaySettings.add(new WeekdaySetting(DayOfWeek.SUNDAY)); } @@ -73,11 +72,24 @@ public WeekdaySettings getWeekdaySettings() { } public TimeSettings getTimeSettings() { - List timeSettings = break_times.stream() - .map(breakTime -> new TimeRange(breakTime.getStartHour(), breakTime.getEndHour())) + List timeSettings = breakTimes.stream() + .flatMap(breakTime -> makeTimeRangeFromBreakTime(breakTime).stream()) .map(TimeSetting::new) .collect(Collectors.toList()); return new TimeSettings(timeSettings); } + + // end 10 ~ start 2 + private static List makeTimeRangeFromBreakTime(BreakTime breakTime) { + List list = new ArrayList<>(); + if(breakTime.getEndHour() > breakTime.getStartHour()) { + list.add(new TimeRange(breakTime.getEndHour(), 24)); + list.add(new TimeRange(0, breakTime.getStartHour())); + } + else { + list.add(new TimeRange(breakTime.getEndHour(), breakTime.getStartHour())); + } + return list; + } } diff --git a/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java b/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java index dcc8518..cec8c49 100644 --- a/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java +++ b/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java @@ -26,6 +26,6 @@ public class JibunAddress { String subAddressNo; public String getFullAddressNo() { - return subAddressNo != null ? addressNo + "-" + subAddressNo : addressNo; + return subAddressNo == null || subAddressNo.isBlank() ? addressNo : addressNo + "-" + subAddressNo; } } diff --git a/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java b/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java index 86912d9..b718c2c 100644 --- a/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java +++ b/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java @@ -20,6 +20,6 @@ public class RoadAddress { String subBuildingNo; public String getFullBuildingNo() { - return buildingNo + "-" + subBuildingNo; + return subBuildingNo == null || subBuildingNo.isBlank() ? buildingNo : buildingNo + "-" + subBuildingNo; } } diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java b/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java index 983a379..e636bb9 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java +++ b/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java @@ -11,7 +11,7 @@ public class BreakDay { @JsonProperty("BRK_DAY_TP_CD") - String breakDayCd; // HOLID + String breakDayCd; @JsonProperty("DAYW_CD") String dayCd; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java b/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java index 3cb5e1a..ffc7d2a 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java +++ b/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java @@ -11,7 +11,7 @@ public class BreakHoliday { @JsonProperty("BRK_DAY_TP_CD") - String breakDayCd; // HOLID + String breakDayCd; @JsonProperty("DAYW_CD") String dayCd; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java b/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java index cf56758..106b348 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java +++ b/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java @@ -10,9 +10,9 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class BreakTime { - @JsonProperty("end_time") + @JsonProperty("start_time") Integer startHour; - @JsonProperty("start_time") + @JsonProperty("end_time") Integer endHour; } diff --git a/src/main/java/com/modoospace/data/controller/dto/space/Product.java b/src/main/java/com/modoospace/data/controller/dto/space/FacilityResponse.java similarity index 81% rename from src/main/java/com/modoospace/data/controller/dto/space/Product.java rename to src/main/java/com/modoospace/data/controller/dto/space/FacilityResponse.java index d2a4eb5..e10141e 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/Product.java +++ b/src/main/java/com/modoospace/data/controller/dto/space/FacilityResponse.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.modoospace.space.domain.Facility; +import com.modoospace.space.domain.Space; import com.modoospace.space.domain.TimeSettings; import com.modoospace.space.domain.WeekdaySettings; import lombok.Getter; @@ -12,18 +13,19 @@ @Getter @Setter @JsonIgnoreProperties(ignoreUnknown = true) -public class Product { +public class FacilityResponse { FacilityInfo info; List categories; - public Facility toFacility(TimeSettings timeSettings, WeekdaySettings weekdaySettings) { + public Facility toFacility(TimeSettings timeSettings, WeekdaySettings weekdaySettings, Space space) { return Facility.builder() .name(info.getName()) .reservationEnable(true) .minUser(info.getMinUser()) - .minUser(info.getMaxUser()) + .maxUser(info.getMaxUser()) .description(info.getDescription()) + .space(space) .timeSettings(timeSettings) .weekdaySettings(weekdaySettings) .build(); From d3a3770260656683ada594f0512131f0edb17d76 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 15:03:06 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feature:=20=EC=8A=A4=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=ED=81=B4=EB=9D=BC=EC=9A=B0=EB=93=9C=20=EB=B0=8F=20?= =?UTF-8?q?=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EC=A3=BC=EC=86=8C=20api=EB=A5=BC?= =?UTF-8?q?=20=EC=97=B0=EB=8F=99=ED=95=98=EC=97=AC=20=EB=AA=A8=EB=91=90?= =?UTF-8?q?=EC=8A=A4=ED=8E=98=EC=9D=B4=EC=8A=A4=20entity=EB=A5=BC=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=ED=95=9C=EB=8B=A4.=20(1~50000=EA=B9=8C?= =?UTF-8?q?=EC=A7=80=EC=99=84=EB=A3=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Space, Facility, TimeSetting, WeekDaySetting, Category Related: #54 --- .../common/GlobalExceptionHandler.java | 5 + .../exception/EmptyResponseException.java | 7 ++ .../data/controller/DataController.java | 45 +++++++-- .../modoospace/data/service/DataService.java | 60 ++++++++++++ .../space/domain/CategoryRepository.java | 3 + .../com/modoospace/space/domain/Space.java | 4 + .../data/controller/DataControllerTest.java | 93 +++++++++++++++++++ 7 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/modoospace/common/exception/EmptyResponseException.java create mode 100644 src/main/java/com/modoospace/data/service/DataService.java create mode 100644 src/test/java/com/modoospace/data/controller/DataControllerTest.java diff --git a/src/main/java/com/modoospace/common/GlobalExceptionHandler.java b/src/main/java/com/modoospace/common/GlobalExceptionHandler.java index b4df0aa..66e5538 100644 --- a/src/main/java/com/modoospace/common/GlobalExceptionHandler.java +++ b/src/main/java/com/modoospace/common/GlobalExceptionHandler.java @@ -49,6 +49,11 @@ public ResponseEntity handleNotFoundEntityException(NotFoundEntityExcept return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND); } + @ExceptionHandler(EmptyResponseException.class) + public ResponseEntity handleEmptyResponseException(EmptyResponseException exception) { + return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND); + } + @ExceptionHandler(InvalidTimeRangeException.class) public ResponseEntity handleInvalidTimeRangeException(InvalidTimeRangeException exception) { return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST); diff --git a/src/main/java/com/modoospace/common/exception/EmptyResponseException.java b/src/main/java/com/modoospace/common/exception/EmptyResponseException.java new file mode 100644 index 0000000..9c3480e --- /dev/null +++ b/src/main/java/com/modoospace/common/exception/EmptyResponseException.java @@ -0,0 +1,7 @@ +package com.modoospace.common.exception; + +public class EmptyResponseException extends RuntimeException { + public EmptyResponseException(String response, String identifier) { + super(response+"("+identifier+")응답 값이 비었습니다."); + } +} diff --git a/src/main/java/com/modoospace/data/controller/DataController.java b/src/main/java/com/modoospace/data/controller/DataController.java index b6a7f3d..1411cf6 100644 --- a/src/main/java/com/modoospace/data/controller/DataController.java +++ b/src/main/java/com/modoospace/data/controller/DataController.java @@ -1,14 +1,17 @@ package com.modoospace.data.controller; import com.fasterxml.jackson.databind.ObjectMapper; +import com.modoospace.common.exception.EmptyResponseException; +import com.modoospace.config.auth.LoginEmail; import com.modoospace.data.controller.dto.AddressResponse; import com.modoospace.data.controller.dto.SpaceResponse; +import com.modoospace.data.service.DataService; +import com.modoospace.space.controller.dto.space.SpaceDetailResponse; +import com.modoospace.space.domain.SpaceIndex; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.net.URI; @@ -21,11 +24,14 @@ @RestController @RequiredArgsConstructor @RequestMapping("/api/v1/test") +@Slf4j public class DataController { private final ObjectMapper objectMapper = new ObjectMapper(); private final HttpClient client = HttpClient.newHttpClient(); + private final DataService dataService; + @Value("${spring.kakao.apikey}") private String key; @@ -35,18 +41,41 @@ public SpaceResponse getSpace(@PathVariable String spaceId) throws IOException, .uri(URI.create("https://new-api.spacecloud.kr/spaces/" + spaceId)) .build(); HttpResponse httpSpaceResponse = client.send(spaceRequest, HttpResponse.BodyHandlers.ofString()); - String body = httpSpaceResponse.body(); - return objectMapper.readValue(body, SpaceResponse.class); + if (isResponseSpaceEmpty(httpSpaceResponse.body())) { + throw new EmptyResponseException("Space", spaceId); + } + log.info("exist space: {}", spaceId); + return objectMapper.readValue(httpSpaceResponse.body(), SpaceResponse.class); + } + + private boolean isResponseSpaceEmpty(String responseBody) { + return "{}".equals(responseBody.trim()); } @GetMapping("/address") public AddressResponse getAddress(String address) throws IOException, InterruptedException { String encodedAddress = URLEncoder.encode(address, StandardCharsets.UTF_8); HttpRequest addressRequest = HttpRequest.newBuilder() - .uri(URI.create("https://dapi.kakao.com/v2/local/search/address?query="+encodedAddress)) + .uri(URI.create("https://dapi.kakao.com/v2/local/search/address?query=" + encodedAddress)) .header("Authorization", "KakaoAK " + key) .build(); HttpResponse httpAddressResponse = client.send(addressRequest, HttpResponse.BodyHandlers.ofString()); - return objectMapper.readValue(httpAddressResponse.body(), AddressResponse.class); + AddressResponse addressResponse = objectMapper.readValue(httpAddressResponse.body(), AddressResponse.class); + if (addressResponse.getDocuments().isEmpty()) { + throw new EmptyResponseException("Address", address); + } + return addressResponse; + } + + @PostMapping("/space/{spaceId}") + public SpaceDetailResponse saveSpace(@PathVariable String spaceId, @LoginEmail String email) throws IOException, InterruptedException { + SpaceResponse spaceResponse = getSpace(spaceId); + AddressResponse addressResponse = getAddress(spaceResponse.getLocation().getAddress()); + return dataService.saveEntity(spaceResponse, addressResponse, email); + } + + @PostMapping("/space-index/{spaceId}") + public SpaceIndex saveSpaceIndex(@PathVariable Long spaceId) { + return dataService.saveIndex(spaceId); } } diff --git a/src/main/java/com/modoospace/data/service/DataService.java b/src/main/java/com/modoospace/data/service/DataService.java new file mode 100644 index 0000000..ebb1ce4 --- /dev/null +++ b/src/main/java/com/modoospace/data/service/DataService.java @@ -0,0 +1,60 @@ +package com.modoospace.data.service; + +import com.modoospace.common.exception.NotFoundEntityException; +import com.modoospace.data.controller.dto.AddressResponse; +import com.modoospace.data.controller.dto.SpaceResponse; +import com.modoospace.member.domain.Member; +import com.modoospace.member.service.MemberService; +import com.modoospace.space.controller.dto.space.SpaceDetailResponse; +import com.modoospace.space.domain.*; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class DataService { + + private final MemberService memberService; + private final CategoryRepository categoryRepository; + private final SpaceRepository spaceRepository; + private final SpaceIndexRepository spaceIndexRepository; + private final FacilityRepository facilityRepository; + + public SpaceDetailResponse saveEntity(SpaceResponse spaceResponse, AddressResponse addressResponse, String email) { + Space space = makeSpace(spaceResponse, addressResponse, email); + spaceIndexRepository.save(SpaceIndex.of(space)); + + List facilities = makeFacility(spaceResponse, space); + space.setFacilities(facilities); + return SpaceDetailResponse.of(space); + } + + public SpaceIndex saveIndex(Long spaceId){ + Space space = spaceRepository.findById(spaceId) + .orElseThrow(() -> new NotFoundEntityException("공간", spaceId)); + return spaceIndexRepository.save(SpaceIndex.of(space)); + } + + private Space makeSpace(SpaceResponse spaceResponse, AddressResponse addressResponse, String email) { + Member member = memberService.findMemberByEmail(email); + Address address = addressResponse.toAddress(spaceResponse.getLocation().getDetailAddress()); + Category category = findCategory(spaceResponse.getCategoryName()); + + return spaceRepository.save(spaceResponse.toSpace(address, category, member)); + } + + private Category findCategory(String name) { + Optional optionalCategory = categoryRepository.findByName(name); + return optionalCategory.orElseGet(() -> categoryRepository.save(new Category(name))); + } + + private List makeFacility(SpaceResponse spaceResponse, Space space) { + return spaceResponse.getFacilityResponses().stream() + .map(product -> facilityRepository.save(product.toFacility(spaceResponse.getTimeSettings(), spaceResponse.getWeekdaySettings(), space))) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/modoospace/space/domain/CategoryRepository.java b/src/main/java/com/modoospace/space/domain/CategoryRepository.java index 4f0c5b3..346455e 100644 --- a/src/main/java/com/modoospace/space/domain/CategoryRepository.java +++ b/src/main/java/com/modoospace/space/domain/CategoryRepository.java @@ -2,6 +2,9 @@ import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface CategoryRepository extends JpaRepository { + Optional findByName(String name); } diff --git a/src/main/java/com/modoospace/space/domain/Space.java b/src/main/java/com/modoospace/space/domain/Space.java index e773dcc..72355de 100644 --- a/src/main/java/com/modoospace/space/domain/Space.java +++ b/src/main/java/com/modoospace/space/domain/Space.java @@ -93,4 +93,8 @@ public void verifyDeletePermission(Member loginMember) { private boolean hasFacilities() { return !facilities.isEmpty(); } + + public void setFacilities(List facilities) { + this.facilities.addAll(facilities); + } } diff --git a/src/test/java/com/modoospace/data/controller/DataControllerTest.java b/src/test/java/com/modoospace/data/controller/DataControllerTest.java new file mode 100644 index 0000000..9f36b19 --- /dev/null +++ b/src/test/java/com/modoospace/data/controller/DataControllerTest.java @@ -0,0 +1,93 @@ +package com.modoospace.data.controller; + +import com.modoospace.AbstractIntegrationContainerBaseTest; +import com.modoospace.common.exception.EmptyResponseException; +import com.modoospace.data.controller.dto.AddressResponse; +import com.modoospace.data.controller.dto.SpaceResponse; +import com.modoospace.member.domain.Member; +import com.modoospace.member.domain.MemberRepository; +import com.modoospace.member.domain.Role; +import com.modoospace.space.controller.dto.space.SpaceDetailResponse; +import com.modoospace.space.domain.Category; +import com.modoospace.space.domain.CategoryRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@Transactional +class DataControllerTest extends AbstractIntegrationContainerBaseTest { + + @Autowired + private DataController dataController; + + @Autowired + private MemberRepository memberRepository; + + @Autowired + private CategoryRepository categoryRepository; + + private Member member; + + @BeforeEach + public void setUp() { + member = Member.builder() + .name("허정화") + .email("wjdghkwhdl@jr.naver.com") + .role(Role.HOST) + .build(); + memberRepository.save(member); + Category category = new Category("스터디룸"); + categoryRepository.save(category); + } + + @DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 Exception을 던진다.") + @Test + public void getSpace_throwException_ifEmptyResponse() { + assertThatThrownBy(() -> dataController.getSpace("1")) + .isInstanceOf(EmptyResponseException.class); + } + + @DisplayName("외부Api로 받아온 데이터를 반환한다.") + @Test + public void getSpace() throws IOException, InterruptedException { + SpaceResponse space = dataController.getSpace("58861"); + assertThat(space.getSpaceInfo().getName()).isEqualTo("감성공간 아르떼부암 "); + } + + @DisplayName("KakaoApi로 받아온 주소데이터가 비어있을 경우 Exception을 던진다.") + @Test + public void getAddress_throwException_ifEmptyResponse() { + assertThatThrownBy(() -> dataController.getAddress("address")) + .isInstanceOf(EmptyResponseException.class); + } + + @DisplayName("KakaoApi로 받아온 데이터를 반환한다.") + @Test + public void getAddress() throws IOException, InterruptedException { + AddressResponse address = dataController.getAddress("서울 종로구 자하문로 254"); + assertThat(address.getDocuments().get(0).getX()).isEqualTo("126.963964049851"); + assertThat(address.getDocuments().get(0).getY()).isEqualTo("37.5969625614596"); + } + + @DisplayName("외부Api로 받아온 데이터를 모두스페이스 엔티티로 변환 후 저장하여 반환한다.") + @Test + public void saveSpace_throwException_ifEmptyResponse() { + assertThatThrownBy(() -> dataController.saveSpace("2", member.getEmail())) + .isInstanceOf(EmptyResponseException.class); + } + + @DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 변환하지 않고 Exception을 던진다.") + @Test + public void saveSpace() throws IOException, InterruptedException { + SpaceDetailResponse space = dataController.saveSpace("58861", member.getEmail()); + assertThat(space.getName()).isEqualTo("감성공간 아르떼부암 "); + assertThat(space.getFacilities()).hasSize(1); + } +} \ No newline at end of file From 013db851733a4331e1d7f6b4d30c4a662e77652d Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 15:04:33 +0900 Subject: [PATCH 05/10] =?UTF-8?q?refactor:=20elastic=20search=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=8B=9C=20size(1000)=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 지정하지 않으면 10개까지만 return Related: #54 --- .../repository/SpaceIndexQueryRepository.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/modoospace/space/repository/SpaceIndexQueryRepository.java b/src/main/java/com/modoospace/space/repository/SpaceIndexQueryRepository.java index 70db1b6..b2cb501 100644 --- a/src/main/java/com/modoospace/space/repository/SpaceIndexQueryRepository.java +++ b/src/main/java/com/modoospace/space/repository/SpaceIndexQueryRepository.java @@ -1,11 +1,14 @@ package com.modoospace.space.repository; import com.modoospace.space.domain.SpaceIndex; + import java.util.List; import java.util.stream.Collectors; + import lombok.RequiredArgsConstructor; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; @@ -21,13 +24,14 @@ public List findIdByQuery(String queryString) { BoolQueryBuilder queryBuilder = makeQuery(queryString); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(queryBuilder) - .build(); + .withQuery(queryBuilder) + .withPageable(PageRequest.of(0, 1000)) + .build(); return elasticsearchOperations.search(searchQuery, SpaceIndex.class) - .stream() - .map(searchHit -> searchHit.getContent().getId()) - .collect(Collectors.toList()); + .stream() + .map(searchHit -> searchHit.getContent().getId()) + .collect(Collectors.toList()); } /** @@ -39,8 +43,8 @@ private BoolQueryBuilder makeQuery(String queryString) { BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery(); for (String term : terms) { queryBuilder = queryBuilder.must( - QueryBuilders.queryStringQuery("*" + term + "*") - .field("name").field("description").field("categoryName").field("address") + QueryBuilders.queryStringQuery("*" + term + "*") + .field("name").field("description").field("categoryName").field("address") ); } return queryBuilder; From 6f072a98c062a34cb1126569916ad8262a74778a Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 15:10:43 +0900 Subject: [PATCH 06/10] =?UTF-8?q?refactor:=20ES=20=ED=86=B5=EC=8B=A0?= =?UTF-8?q?=EC=9D=B4=20=ED=95=84=EC=9A=94=ED=95=9C=20booleanExpression=20?= =?UTF-8?q?=EC=9E=AC=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- .../modoospace/space/repository/SpaceQueryRepository.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/modoospace/space/repository/SpaceQueryRepository.java b/src/main/java/com/modoospace/space/repository/SpaceQueryRepository.java index 4ce94c4..8b97178 100644 --- a/src/main/java/com/modoospace/space/repository/SpaceQueryRepository.java +++ b/src/main/java/com/modoospace/space/repository/SpaceQueryRepository.java @@ -34,12 +34,14 @@ public class SpaceQueryRepository { private final SpaceIndexQueryRepository spaceIndexQueryRepository; public Page searchSpace(SpaceSearchRequest searchRequest, Pageable pageable) { + + BooleanExpression spaceIdInExpression = spaceIdInQueryResult(searchRequest.getQuery()); List content = jpaQueryFactory .selectFrom(space) .join(space.host, member).fetchJoin() .join(space.category, category).fetchJoin() .where( - spaceIdInQueryResult(searchRequest.getQuery()) + spaceIdInExpression , eqDepthFirst(searchRequest.getDepthFirst()) , eqDepthSecond(searchRequest.getDepthSecond()) , eqDepthThird(searchRequest.getDepthThird()) @@ -54,7 +56,7 @@ public Page searchSpace(SpaceSearchRequest searchRequest, Pageable pageab JPAQuery countQuery = jpaQueryFactory .selectFrom(space) .where( - spaceIdInQueryResult(searchRequest.getQuery()) + spaceIdInExpression , eqDepthFirst(searchRequest.getDepthFirst()) , eqDepthSecond(searchRequest.getDepthSecond()) , eqDepthThird(searchRequest.getDepthThird()) From 357db2054a4a67e96e35ac0b228bf7d4eabae430 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 15:27:52 +0900 Subject: [PATCH 07/10] =?UTF-8?q?test:=20display=20name=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- .../com/modoospace/data/controller/DataControllerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/modoospace/data/controller/DataControllerTest.java b/src/test/java/com/modoospace/data/controller/DataControllerTest.java index 9f36b19..319fcec 100644 --- a/src/test/java/com/modoospace/data/controller/DataControllerTest.java +++ b/src/test/java/com/modoospace/data/controller/DataControllerTest.java @@ -76,14 +76,14 @@ public void getAddress() throws IOException, InterruptedException { assertThat(address.getDocuments().get(0).getY()).isEqualTo("37.5969625614596"); } - @DisplayName("외부Api로 받아온 데이터를 모두스페이스 엔티티로 변환 후 저장하여 반환한다.") + @DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 변환하지 않고 Exception을 던진다.") @Test public void saveSpace_throwException_ifEmptyResponse() { assertThatThrownBy(() -> dataController.saveSpace("2", member.getEmail())) .isInstanceOf(EmptyResponseException.class); } - @DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 변환하지 않고 Exception을 던진다.") + @DisplayName("외부Api로 받아온 데이터를 모두스페이스 엔티티로 변환 후 저장하여 반환한다.") @Test public void saveSpace() throws IOException, InterruptedException { SpaceDetailResponse space = dataController.saveSpace("58861", member.getEmail()); From 8a7087579a2b7b7a7915d0345597a3f30e98823e Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 17:54:24 +0900 Subject: [PATCH 08/10] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20mockData=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- .../modoospace/data/service/DataService.java | 60 ------------------- .../controller/dto/MockAddressResponse.java} | 9 +-- .../controller/dto/MockSpaceResponse.java} | 14 ++--- .../controller/dto/address/Document.java | 2 +- .../controller/dto/address/JibunAddress.java | 2 +- .../controller/dto/address/RoadAddress.java | 2 +- .../controller/dto/space/BreakDay.java | 2 +- .../controller/dto/space/BreakHoliday.java | 2 +- .../controller/dto/space/BreakTime.java | 2 +- .../dto/space/FacilityCategory.java | 2 +- .../controller/dto/space/FacilityInfo.java | 2 +- .../dto/space/FacilityResponse.java | 5 +- .../controller/dto/space/Location.java | 2 +- .../controller/dto/space/SpaceInfo.java | 2 +- .../mockData/service/MockDataService.java | 47 +++++++++++++++ .../com/modoospace/space/domain/Space.java | 4 -- 16 files changed, 71 insertions(+), 88 deletions(-) delete mode 100644 src/main/java/com/modoospace/data/service/DataService.java rename src/main/java/com/modoospace/{data/controller/dto/AddressResponse.java => mockData/controller/dto/MockAddressResponse.java} (80%) rename src/main/java/com/modoospace/{data/controller/dto/SpaceResponse.java => mockData/controller/dto/MockSpaceResponse.java} (90%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/address/Document.java (86%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/address/JibunAddress.java (92%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/address/RoadAddress.java (91%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/BreakDay.java (86%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/BreakHoliday.java (86%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/BreakTime.java (86%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/FacilityCategory.java (80%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/FacilityInfo.java (89%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/FacilityResponse.java (87%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/Location.java (86%) rename src/main/java/com/modoospace/{data => mockData}/controller/dto/space/SpaceInfo.java (85%) create mode 100644 src/main/java/com/modoospace/mockData/service/MockDataService.java diff --git a/src/main/java/com/modoospace/data/service/DataService.java b/src/main/java/com/modoospace/data/service/DataService.java deleted file mode 100644 index ebb1ce4..0000000 --- a/src/main/java/com/modoospace/data/service/DataService.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.modoospace.data.service; - -import com.modoospace.common.exception.NotFoundEntityException; -import com.modoospace.data.controller.dto.AddressResponse; -import com.modoospace.data.controller.dto.SpaceResponse; -import com.modoospace.member.domain.Member; -import com.modoospace.member.service.MemberService; -import com.modoospace.space.controller.dto.space.SpaceDetailResponse; -import com.modoospace.space.domain.*; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -@Service -@RequiredArgsConstructor -public class DataService { - - private final MemberService memberService; - private final CategoryRepository categoryRepository; - private final SpaceRepository spaceRepository; - private final SpaceIndexRepository spaceIndexRepository; - private final FacilityRepository facilityRepository; - - public SpaceDetailResponse saveEntity(SpaceResponse spaceResponse, AddressResponse addressResponse, String email) { - Space space = makeSpace(spaceResponse, addressResponse, email); - spaceIndexRepository.save(SpaceIndex.of(space)); - - List facilities = makeFacility(spaceResponse, space); - space.setFacilities(facilities); - return SpaceDetailResponse.of(space); - } - - public SpaceIndex saveIndex(Long spaceId){ - Space space = spaceRepository.findById(spaceId) - .orElseThrow(() -> new NotFoundEntityException("공간", spaceId)); - return spaceIndexRepository.save(SpaceIndex.of(space)); - } - - private Space makeSpace(SpaceResponse spaceResponse, AddressResponse addressResponse, String email) { - Member member = memberService.findMemberByEmail(email); - Address address = addressResponse.toAddress(spaceResponse.getLocation().getDetailAddress()); - Category category = findCategory(spaceResponse.getCategoryName()); - - return spaceRepository.save(spaceResponse.toSpace(address, category, member)); - } - - private Category findCategory(String name) { - Optional optionalCategory = categoryRepository.findByName(name); - return optionalCategory.orElseGet(() -> categoryRepository.save(new Category(name))); - } - - private List makeFacility(SpaceResponse spaceResponse, Space space) { - return spaceResponse.getFacilityResponses().stream() - .map(product -> facilityRepository.save(product.toFacility(spaceResponse.getTimeSettings(), spaceResponse.getWeekdaySettings(), space))) - .collect(Collectors.toList()); - } -} diff --git a/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java b/src/main/java/com/modoospace/mockData/controller/dto/MockAddressResponse.java similarity index 80% rename from src/main/java/com/modoospace/data/controller/dto/AddressResponse.java rename to src/main/java/com/modoospace/mockData/controller/dto/MockAddressResponse.java index 88a5dac..4fb0d19 100644 --- a/src/main/java/com/modoospace/data/controller/dto/AddressResponse.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/MockAddressResponse.java @@ -1,19 +1,20 @@ -package com.modoospace.data.controller.dto; +package com.modoospace.mockData.controller.dto; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.modoospace.data.controller.dto.address.Document; +import com.modoospace.mockData.controller.dto.address.Document; import com.modoospace.space.domain.Address; import lombok.Getter; import lombok.Setter; +import java.util.ArrayList; import java.util.List; @Getter @Setter @JsonIgnoreProperties(ignoreUnknown = true) -public class AddressResponse { +public class MockAddressResponse { - List documents; + List documents = new ArrayList<>(); public Address toAddress(String detailAddress) { Document document = documents.get(0); diff --git a/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java b/src/main/java/com/modoospace/mockData/controller/dto/MockSpaceResponse.java similarity index 90% rename from src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java rename to src/main/java/com/modoospace/mockData/controller/dto/MockSpaceResponse.java index d6212da..f4311d2 100644 --- a/src/main/java/com/modoospace/data/controller/dto/SpaceResponse.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/MockSpaceResponse.java @@ -1,9 +1,9 @@ -package com.modoospace.data.controller.dto; +package com.modoospace.mockData.controller.dto; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; -import com.modoospace.data.controller.dto.space.*; import com.modoospace.member.domain.Member; +import com.modoospace.mockData.controller.dto.space.*; import com.modoospace.space.domain.*; import lombok.Getter; import lombok.Setter; @@ -17,7 +17,7 @@ @Getter @Setter @JsonIgnoreProperties(ignoreUnknown = true) -public class SpaceResponse { +public class MockSpaceResponse { @JsonProperty("products") private List facilityResponses = new ArrayList<>(); @@ -40,8 +40,7 @@ public String getCategoryName() { return facilityResponses.stream() .findFirst() .flatMap(p -> p.getCategories().stream().findFirst()) - .map(FacilityCategory::getName) - .get(); + .map(FacilityCategory::getName).orElse("스터디룸"); } public Space toSpace(Address address, Category category, Member member) { @@ -83,11 +82,10 @@ public TimeSettings getTimeSettings() { // end 10 ~ start 2 private static List makeTimeRangeFromBreakTime(BreakTime breakTime) { List list = new ArrayList<>(); - if(breakTime.getEndHour() > breakTime.getStartHour()) { + if (breakTime.getEndHour() > breakTime.getStartHour()) { list.add(new TimeRange(breakTime.getEndHour(), 24)); list.add(new TimeRange(0, breakTime.getStartHour())); - } - else { + } else { list.add(new TimeRange(breakTime.getEndHour(), breakTime.getStartHour())); } return list; diff --git a/src/main/java/com/modoospace/data/controller/dto/address/Document.java b/src/main/java/com/modoospace/mockData/controller/dto/address/Document.java similarity index 86% rename from src/main/java/com/modoospace/data/controller/dto/address/Document.java rename to src/main/java/com/modoospace/mockData/controller/dto/address/Document.java index d7ae968..31e5114 100644 --- a/src/main/java/com/modoospace/data/controller/dto/address/Document.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/address/Document.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.address; +package com.modoospace.mockData.controller.dto.address; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java b/src/main/java/com/modoospace/mockData/controller/dto/address/JibunAddress.java similarity index 92% rename from src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java rename to src/main/java/com/modoospace/mockData/controller/dto/address/JibunAddress.java index cec8c49..eb632d0 100644 --- a/src/main/java/com/modoospace/data/controller/dto/address/JibunAddress.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/address/JibunAddress.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.address; +package com.modoospace.mockData.controller.dto.address; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java b/src/main/java/com/modoospace/mockData/controller/dto/address/RoadAddress.java similarity index 91% rename from src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java rename to src/main/java/com/modoospace/mockData/controller/dto/address/RoadAddress.java index b718c2c..185d814 100644 --- a/src/main/java/com/modoospace/data/controller/dto/address/RoadAddress.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/address/RoadAddress.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.address; +package com.modoospace.mockData.controller.dto.address; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java b/src/main/java/com/modoospace/mockData/controller/dto/space/BreakDay.java similarity index 86% rename from src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/BreakDay.java index e636bb9..bc77a13 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/BreakDay.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/BreakDay.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java b/src/main/java/com/modoospace/mockData/controller/dto/space/BreakHoliday.java similarity index 86% rename from src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/BreakHoliday.java index ffc7d2a..04a9260 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/BreakHoliday.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/BreakHoliday.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java b/src/main/java/com/modoospace/mockData/controller/dto/space/BreakTime.java similarity index 86% rename from src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/BreakTime.java index 106b348..d4ef88d 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/BreakTime.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/BreakTime.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java b/src/main/java/com/modoospace/mockData/controller/dto/space/FacilityCategory.java similarity index 80% rename from src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/FacilityCategory.java index 0e2d016..25fa39f 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/FacilityCategory.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/FacilityCategory.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Getter; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java b/src/main/java/com/modoospace/mockData/controller/dto/space/FacilityInfo.java similarity index 89% rename from src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/FacilityInfo.java index 9691ab6..6e63f6c 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/FacilityInfo.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/FacilityInfo.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/FacilityResponse.java b/src/main/java/com/modoospace/mockData/controller/dto/space/FacilityResponse.java similarity index 87% rename from src/main/java/com/modoospace/data/controller/dto/space/FacilityResponse.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/FacilityResponse.java index e10141e..89caa7f 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/FacilityResponse.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/FacilityResponse.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.modoospace.space.domain.Facility; @@ -8,6 +8,7 @@ import lombok.Getter; import lombok.Setter; +import java.util.ArrayList; import java.util.List; @Getter @@ -16,7 +17,7 @@ public class FacilityResponse { FacilityInfo info; - List categories; + List categories = new ArrayList<>(); public Facility toFacility(TimeSettings timeSettings, WeekdaySettings weekdaySettings, Space space) { return Facility.builder() diff --git a/src/main/java/com/modoospace/data/controller/dto/space/Location.java b/src/main/java/com/modoospace/mockData/controller/dto/space/Location.java similarity index 86% rename from src/main/java/com/modoospace/data/controller/dto/space/Location.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/Location.java index 8b5a21e..62ef607 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/Location.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/Location.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java b/src/main/java/com/modoospace/mockData/controller/dto/space/SpaceInfo.java similarity index 85% rename from src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java rename to src/main/java/com/modoospace/mockData/controller/dto/space/SpaceInfo.java index 7091a17..e586619 100644 --- a/src/main/java/com/modoospace/data/controller/dto/space/SpaceInfo.java +++ b/src/main/java/com/modoospace/mockData/controller/dto/space/SpaceInfo.java @@ -1,4 +1,4 @@ -package com.modoospace.data.controller.dto.space; +package com.modoospace.mockData.controller.dto.space; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/com/modoospace/mockData/service/MockDataService.java b/src/main/java/com/modoospace/mockData/service/MockDataService.java new file mode 100644 index 0000000..bdc00a1 --- /dev/null +++ b/src/main/java/com/modoospace/mockData/service/MockDataService.java @@ -0,0 +1,47 @@ +package com.modoospace.mockData.service; + +import com.modoospace.member.domain.Member; +import com.modoospace.member.service.MemberService; +import com.modoospace.mockData.controller.dto.MockAddressResponse; +import com.modoospace.mockData.controller.dto.MockSpaceResponse; +import com.modoospace.space.domain.*; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class MockDataService { + + private final MemberService memberService; + private final CategoryRepository categoryRepository; + private final SpaceRepository spaceRepository; + private final SpaceIndexRepository spaceIndexRepository; + private final FacilityRepository facilityRepository; + + public Long saveEntity(MockSpaceResponse spaceResponse, MockAddressResponse addressResponse, String email) { + Space space = makeSpace(spaceResponse, addressResponse, email); + spaceIndexRepository.save(SpaceIndex.of(space)); + makeFacility(spaceResponse, space); + return space.getId(); + } + + private Space makeSpace(MockSpaceResponse spaceResponse, MockAddressResponse addressResponse, String email) { + Member member = memberService.findMemberByEmail(email); + Address address = addressResponse.toAddress(spaceResponse.getLocation().getDetailAddress()); + Category category = findCategory(spaceResponse.getCategoryName()); + + return spaceRepository.save(spaceResponse.toSpace(address, category, member)); + } + + private Category findCategory(String name) { + Optional optionalCategory = categoryRepository.findByName(name); + return optionalCategory.orElseGet(() -> categoryRepository.save(new Category(name))); + } + + private void makeFacility(MockSpaceResponse spaceResponse, Space space) { + spaceResponse.getFacilityResponses() + .forEach(product -> facilityRepository.save(product.toFacility(spaceResponse.getTimeSettings(), spaceResponse.getWeekdaySettings(), space))); + } +} diff --git a/src/main/java/com/modoospace/space/domain/Space.java b/src/main/java/com/modoospace/space/domain/Space.java index 72355de..e773dcc 100644 --- a/src/main/java/com/modoospace/space/domain/Space.java +++ b/src/main/java/com/modoospace/space/domain/Space.java @@ -93,8 +93,4 @@ public void verifyDeletePermission(Member loginMember) { private boolean hasFacilities() { return !facilities.isEmpty(); } - - public void setFacilities(List facilities) { - this.facilities.addAll(facilities); - } } From 5db6820805d143a9178dc7790c13b270b6c1a9c9 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 17:56:30 +0900 Subject: [PATCH 09/10] =?UTF-8?q?refactor:=20MockDataController=20Response?= =?UTF-8?q?Entity=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- .../controller/MockDataController.java} | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) rename src/main/java/com/modoospace/{data/controller/DataController.java => mockData/controller/MockDataController.java} (57%) diff --git a/src/main/java/com/modoospace/data/controller/DataController.java b/src/main/java/com/modoospace/mockData/controller/MockDataController.java similarity index 57% rename from src/main/java/com/modoospace/data/controller/DataController.java rename to src/main/java/com/modoospace/mockData/controller/MockDataController.java index 1411cf6..0b951e4 100644 --- a/src/main/java/com/modoospace/data/controller/DataController.java +++ b/src/main/java/com/modoospace/mockData/controller/MockDataController.java @@ -1,16 +1,15 @@ -package com.modoospace.data.controller; +package com.modoospace.mockData.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.modoospace.common.exception.EmptyResponseException; import com.modoospace.config.auth.LoginEmail; -import com.modoospace.data.controller.dto.AddressResponse; -import com.modoospace.data.controller.dto.SpaceResponse; -import com.modoospace.data.service.DataService; -import com.modoospace.space.controller.dto.space.SpaceDetailResponse; -import com.modoospace.space.domain.SpaceIndex; +import com.modoospace.mockData.controller.dto.MockAddressResponse; +import com.modoospace.mockData.controller.dto.MockSpaceResponse; +import com.modoospace.mockData.service.MockDataService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.io.IOException; @@ -23,20 +22,24 @@ @RestController @RequiredArgsConstructor -@RequestMapping("/api/v1/test") +@RequestMapping("/api/v1/mock-data") @Slf4j -public class DataController { +public class MockDataController { private final ObjectMapper objectMapper = new ObjectMapper(); private final HttpClient client = HttpClient.newHttpClient(); - private final DataService dataService; + private final MockDataService mockDataService; @Value("${spring.kakao.apikey}") private String key; @GetMapping("/space/{spaceId}") - public SpaceResponse getSpace(@PathVariable String spaceId) throws IOException, InterruptedException { + public ResponseEntity getSpace(@PathVariable String spaceId) throws IOException, InterruptedException { + return ResponseEntity.ok(getMockSpace(spaceId)); + } + + private MockSpaceResponse getMockSpace(String spaceId) throws IOException, InterruptedException { HttpRequest spaceRequest = HttpRequest.newBuilder() .uri(URI.create("https://new-api.spacecloud.kr/spaces/" + spaceId)) .build(); @@ -44,8 +47,7 @@ public SpaceResponse getSpace(@PathVariable String spaceId) throws IOException, if (isResponseSpaceEmpty(httpSpaceResponse.body())) { throw new EmptyResponseException("Space", spaceId); } - log.info("exist space: {}", spaceId); - return objectMapper.readValue(httpSpaceResponse.body(), SpaceResponse.class); + return objectMapper.readValue(httpSpaceResponse.body(), MockSpaceResponse.class); } private boolean isResponseSpaceEmpty(String responseBody) { @@ -53,14 +55,18 @@ private boolean isResponseSpaceEmpty(String responseBody) { } @GetMapping("/address") - public AddressResponse getAddress(String address) throws IOException, InterruptedException { + public ResponseEntity getAddress(String address) throws IOException, InterruptedException { + return ResponseEntity.ok(getMockAddress(address)); + } + + private MockAddressResponse getMockAddress(String address) throws IOException, InterruptedException { String encodedAddress = URLEncoder.encode(address, StandardCharsets.UTF_8); HttpRequest addressRequest = HttpRequest.newBuilder() .uri(URI.create("https://dapi.kakao.com/v2/local/search/address?query=" + encodedAddress)) .header("Authorization", "KakaoAK " + key) .build(); HttpResponse httpAddressResponse = client.send(addressRequest, HttpResponse.BodyHandlers.ofString()); - AddressResponse addressResponse = objectMapper.readValue(httpAddressResponse.body(), AddressResponse.class); + MockAddressResponse addressResponse = objectMapper.readValue(httpAddressResponse.body(), MockAddressResponse.class); if (addressResponse.getDocuments().isEmpty()) { throw new EmptyResponseException("Address", address); } @@ -68,14 +74,10 @@ public AddressResponse getAddress(String address) throws IOException, Interrupte } @PostMapping("/space/{spaceId}") - public SpaceDetailResponse saveSpace(@PathVariable String spaceId, @LoginEmail String email) throws IOException, InterruptedException { - SpaceResponse spaceResponse = getSpace(spaceId); - AddressResponse addressResponse = getAddress(spaceResponse.getLocation().getAddress()); - return dataService.saveEntity(spaceResponse, addressResponse, email); - } - - @PostMapping("/space-index/{spaceId}") - public SpaceIndex saveSpaceIndex(@PathVariable Long spaceId) { - return dataService.saveIndex(spaceId); + public ResponseEntity saveSpace(@PathVariable String spaceId, @LoginEmail String email) throws IOException, InterruptedException { + MockSpaceResponse spaceResponse = getMockSpace(spaceId); + MockAddressResponse addressResponse = getMockAddress(spaceResponse.getLocation().getAddress()); + Long entitySpaceId = mockDataService.saveEntity(spaceResponse, addressResponse, email); + return ResponseEntity.created(URI.create("/api/v1/spaces/" + entitySpaceId)).build(); } } From 4641bf11bb19a1da533c85f336c9b590ffb61973 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Wed, 20 Mar 2024 17:57:01 +0900 Subject: [PATCH 10/10] =?UTF-8?q?test:=20MockDataController=20ResponseEnti?= =?UTF-8?q?ty=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: #54 --- .../controller/MockDataControllerTest.java} | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) rename src/test/java/com/modoospace/{data/controller/DataControllerTest.java => mockData/controller/MockDataControllerTest.java} (64%) diff --git a/src/test/java/com/modoospace/data/controller/DataControllerTest.java b/src/test/java/com/modoospace/mockData/controller/MockDataControllerTest.java similarity index 64% rename from src/test/java/com/modoospace/data/controller/DataControllerTest.java rename to src/test/java/com/modoospace/mockData/controller/MockDataControllerTest.java index 319fcec..b8bc0e2 100644 --- a/src/test/java/com/modoospace/data/controller/DataControllerTest.java +++ b/src/test/java/com/modoospace/mockData/controller/MockDataControllerTest.java @@ -1,15 +1,16 @@ -package com.modoospace.data.controller; +package com.modoospace.mockData.controller; import com.modoospace.AbstractIntegrationContainerBaseTest; import com.modoospace.common.exception.EmptyResponseException; -import com.modoospace.data.controller.dto.AddressResponse; -import com.modoospace.data.controller.dto.SpaceResponse; import com.modoospace.member.domain.Member; import com.modoospace.member.domain.MemberRepository; import com.modoospace.member.domain.Role; -import com.modoospace.space.controller.dto.space.SpaceDetailResponse; +import com.modoospace.mockData.controller.dto.MockAddressResponse; +import com.modoospace.mockData.controller.dto.MockSpaceResponse; import com.modoospace.space.domain.Category; import com.modoospace.space.domain.CategoryRepository; +import com.modoospace.space.domain.Space; +import com.modoospace.space.domain.SpaceRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -17,19 +18,24 @@ import org.springframework.transaction.annotation.Transactional; import java.io.IOException; +import java.net.URI; +import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @Transactional -class DataControllerTest extends AbstractIntegrationContainerBaseTest { +class MockDataControllerTest extends AbstractIntegrationContainerBaseTest { @Autowired - private DataController dataController; + private MockDataController mockDataController; @Autowired private MemberRepository memberRepository; + @Autowired + private SpaceRepository spaceRepository; + @Autowired private CategoryRepository categoryRepository; @@ -50,28 +56,32 @@ public void setUp() { @DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 Exception을 던진다.") @Test public void getSpace_throwException_ifEmptyResponse() { - assertThatThrownBy(() -> dataController.getSpace("1")) + assertThatThrownBy(() -> mockDataController.getSpace("1")) .isInstanceOf(EmptyResponseException.class); } @DisplayName("외부Api로 받아온 데이터를 반환한다.") @Test public void getSpace() throws IOException, InterruptedException { - SpaceResponse space = dataController.getSpace("58861"); + MockSpaceResponse space = mockDataController.getSpace("58861").getBody(); + + assertThat(space).isNotNull(); assertThat(space.getSpaceInfo().getName()).isEqualTo("감성공간 아르떼부암 "); } @DisplayName("KakaoApi로 받아온 주소데이터가 비어있을 경우 Exception을 던진다.") @Test public void getAddress_throwException_ifEmptyResponse() { - assertThatThrownBy(() -> dataController.getAddress("address")) + assertThatThrownBy(() -> mockDataController.getAddress("address")) .isInstanceOf(EmptyResponseException.class); } @DisplayName("KakaoApi로 받아온 데이터를 반환한다.") @Test public void getAddress() throws IOException, InterruptedException { - AddressResponse address = dataController.getAddress("서울 종로구 자하문로 254"); + MockAddressResponse address = mockDataController.getAddress("서울 종로구 자하문로 254").getBody(); + + assertThat(address).isNotNull(); assertThat(address.getDocuments().get(0).getX()).isEqualTo("126.963964049851"); assertThat(address.getDocuments().get(0).getY()).isEqualTo("37.5969625614596"); } @@ -79,15 +89,24 @@ public void getAddress() throws IOException, InterruptedException { @DisplayName("외부api로 받아온 공간데이터가 비어있을 경우 변환하지 않고 Exception을 던진다.") @Test public void saveSpace_throwException_ifEmptyResponse() { - assertThatThrownBy(() -> dataController.saveSpace("2", member.getEmail())) + assertThatThrownBy(() -> mockDataController.saveSpace("2", member.getEmail())) .isInstanceOf(EmptyResponseException.class); } @DisplayName("외부Api로 받아온 데이터를 모두스페이스 엔티티로 변환 후 저장하여 반환한다.") @Test public void saveSpace() throws IOException, InterruptedException { - SpaceDetailResponse space = dataController.saveSpace("58861", member.getEmail()); - assertThat(space.getName()).isEqualTo("감성공간 아르떼부암 "); - assertThat(space.getFacilities()).hasSize(1); + URI location = mockDataController.saveSpace("58861", member.getEmail()).getHeaders().getLocation(); + assertThat(location).isNotNull(); + + Optional space = spaceRepository.findById(extractId(location)); + assertThat(space).isPresent(); + assertThat(space.get().getName()).isEqualTo("감성공간 아르떼부암 "); + } + + private Long extractId(URI location) { + String path = location.getPath(); + String id = path.substring(path.lastIndexOf("/") + 1); + return Long.parseLong(id); } } \ No newline at end of file