-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from f-lab-edu/feature/54-testdata
[#54] 테스트용 데이터 생성
- Loading branch information
Showing
22 changed files
with
634 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/modoospace/common/exception/EmptyResponseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.modoospace.common.exception; | ||
|
||
public class EmptyResponseException extends RuntimeException { | ||
public EmptyResponseException(String response, String identifier) { | ||
super(response+"("+identifier+")응답 값이 비었습니다."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/com/modoospace/mockData/controller/MockDataController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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.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; | ||
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/mock-data") | ||
@Slf4j | ||
public class MockDataController { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private final HttpClient client = HttpClient.newHttpClient(); | ||
|
||
private final MockDataService mockDataService; | ||
|
||
@Value("${spring.kakao.apikey}") | ||
private String key; | ||
|
||
@GetMapping("/space/{spaceId}") | ||
public ResponseEntity<MockSpaceResponse> 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(); | ||
HttpResponse<String> httpSpaceResponse = client.send(spaceRequest, HttpResponse.BodyHandlers.ofString()); | ||
if (isResponseSpaceEmpty(httpSpaceResponse.body())) { | ||
throw new EmptyResponseException("Space", spaceId); | ||
} | ||
return objectMapper.readValue(httpSpaceResponse.body(), MockSpaceResponse.class); | ||
} | ||
|
||
private boolean isResponseSpaceEmpty(String responseBody) { | ||
return "{}".equals(responseBody.trim()); | ||
} | ||
|
||
@GetMapping("/address") | ||
public ResponseEntity<MockAddressResponse> 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<String> httpAddressResponse = client.send(addressRequest, HttpResponse.BodyHandlers.ofString()); | ||
MockAddressResponse addressResponse = objectMapper.readValue(httpAddressResponse.body(), MockAddressResponse.class); | ||
if (addressResponse.getDocuments().isEmpty()) { | ||
throw new EmptyResponseException("Address", address); | ||
} | ||
return addressResponse; | ||
} | ||
|
||
@PostMapping("/space/{spaceId}") | ||
public ResponseEntity<Void> 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(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/modoospace/mockData/controller/dto/MockAddressResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.modoospace.mockData.controller.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
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 MockAddressResponse { | ||
|
||
List<Document> documents = new ArrayList<>(); | ||
|
||
public Address toAddress(String detailAddress) { | ||
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()) | ||
.detailAddress(detailAddress) | ||
.x(document.getX()) | ||
.y(document.getY()) | ||
.build(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/modoospace/mockData/controller/dto/MockSpaceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.modoospace.mockData.controller.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.modoospace.member.domain.Member; | ||
import com.modoospace.mockData.controller.dto.space.*; | ||
import com.modoospace.space.domain.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.time.DayOfWeek; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class MockSpaceResponse { | ||
|
||
@JsonProperty("products") | ||
private List<FacilityResponse> facilityResponses = new ArrayList<>(); | ||
|
||
@JsonProperty("info") | ||
private SpaceInfo spaceInfo; | ||
|
||
private Location location; | ||
|
||
@JsonProperty("break_days") | ||
private List<BreakDay> breakDays = new ArrayList<>(); | ||
|
||
@JsonProperty("break_holidays") | ||
private List<BreakHoliday> breakHolidays = new ArrayList<>(); | ||
|
||
@JsonProperty("break_times") | ||
private List<BreakTime> breakTimes = new ArrayList<>(); | ||
|
||
public String getCategoryName() { | ||
return facilityResponses.stream() | ||
.findFirst() | ||
.flatMap(p -> p.getCategories().stream().findFirst()) | ||
.map(FacilityCategory::getName).orElse("스터디룸"); | ||
} | ||
|
||
public Space toSpace(Address address, Category category, Member member) { | ||
return Space.builder() | ||
.name(spaceInfo.getName()) | ||
.description(spaceInfo.getDescription()) | ||
.address(address) | ||
.category(category) | ||
.host(member) | ||
.build(); | ||
} | ||
|
||
public WeekdaySettings getWeekdaySettings() { | ||
List<WeekdaySetting> 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 (breakDays.isEmpty() && breakHolidays.isEmpty()) { | ||
weekdaySettings.add(new WeekdaySetting(DayOfWeek.SATURDAY)); | ||
weekdaySettings.add(new WeekdaySetting(DayOfWeek.SUNDAY)); | ||
} | ||
|
||
return new WeekdaySettings(weekdaySettings); | ||
} | ||
|
||
public TimeSettings getTimeSettings() { | ||
List<TimeSetting> 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<TimeRange> makeTimeRangeFromBreakTime(BreakTime breakTime) { | ||
List<TimeRange> 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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/modoospace/mockData/controller/dto/address/Document.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.modoospace.mockData.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; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/modoospace/mockData/controller/dto/address/JibunAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.modoospace.mockData.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 || subAddressNo.isBlank() ? addressNo : addressNo + "-" + subAddressNo; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/modoospace/mockData/controller/dto/address/RoadAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.modoospace.mockData.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 subBuildingNo == null || subBuildingNo.isBlank() ? buildingNo : buildingNo + "-" + subBuildingNo; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/modoospace/mockData/controller/dto/space/BreakDay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.modoospace.mockData.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; | ||
|
||
@JsonProperty("DAYW_CD") | ||
String dayCd; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/modoospace/mockData/controller/dto/space/BreakHoliday.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.modoospace.mockData.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; | ||
|
||
@JsonProperty("DAYW_CD") | ||
String dayCd; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/modoospace/mockData/controller/dto/space/BreakTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.modoospace.mockData.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("start_time") | ||
Integer startHour; | ||
|
||
@JsonProperty("end_time") | ||
Integer endHour; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/modoospace/mockData/controller/dto/space/FacilityCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.modoospace.mockData.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; | ||
} |
Oops, something went wrong.