Skip to content

Commit

Permalink
#18 [Refactor] 중복 함수 추출
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 31, 2022
1 parent 3fed251 commit b9be48f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 125 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/mpnp/baechelin/api/dto/LocationInfoDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.mpnp.baechelin.api.dto;

import com.mpnp.baechelin.api.model.LocationKeywordSearchForm;
import com.mpnp.baechelin.common.DataClarification;
import lombok.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@NoArgsConstructor
Expand Down Expand Up @@ -34,5 +37,23 @@ public boolean validate() {
return this.category != null && this.storeId != null && this.latitude != null
&& this.longitude != null && this.storeName != null;
}

public static LocationResponse KeywordToRes(LocationKeywordSearchForm locationKeywordSearchForm){
if (locationKeywordSearchForm == null) {
return null;
}
LocationKeywordSearchForm.Documents latLngDoc
= Arrays.stream(locationKeywordSearchForm.getDocuments()).findFirst().orElse(null);
if (latLngDoc == null) {
return null;
}
return LocationInfoDto.LocationResponse.builder()
.storeId(Long.valueOf(latLngDoc.getId()))
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.category(DataClarification.categoryFilter(latLngDoc.getCategory_name()))
.storeName(latLngDoc.getPlace_name())
.phoneNumber(latLngDoc.getPhone()).build();
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/mpnp/baechelin/api/dto/LocationPartDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.mpnp.baechelin.api.dto;

import com.mpnp.baechelin.api.model.LocationAddressSearchForm;
import com.mpnp.baechelin.api.model.LocationKeywordSearchForm;
import lombok.*;

import java.util.Arrays;

@AllArgsConstructor
@Getter @Setter
@Builder
Expand All @@ -19,6 +23,22 @@ public static class LatLong{
public boolean validate() {
return this.latitude != null && this.longitude != null;
}
public static LatLong convertPart(LocationKeywordSearchForm locationKeywordSearchForm){
LocationPartDto.LatLong locLl = LocationPartDto.LatLong.builder().build();
if (locationKeywordSearchForm == null) { // 비어 있을 때 status-false 저장
return locLl;
}
LocationKeywordSearchForm.Documents latLngDoc
= Arrays.stream(locationKeywordSearchForm.getDocuments()).findAny().orElse(null);
if (latLngDoc != null) {
locLl = LocationPartDto.LatLong.builder()
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.status(true)
.build();
}
return locLl;
}
}

@AllArgsConstructor
Expand All @@ -28,5 +48,20 @@ public static class Address{
@Builder.Default
private boolean status = false;
private String address;
public static Address formToDto(LocationAddressSearchForm resultRe) {
LocationPartDto.Address addressInfoDto = LocationPartDto.Address.builder().build();
if (resultRe == null)
return addressInfoDto;

LocationAddressSearchForm.TotalAddress address = Arrays.stream(resultRe.getDocuments()).findFirst().orElse(null);
if (address == null) {
return addressInfoDto;
} else {
return LocationPartDto.Address.builder()
.address(address.getAddress().getAddress_name())
.status(true)
.build();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.net.URI;
import java.util.*;

import static com.mpnp.baechelin.common.DataClarification.categoryFilter;

@Service
@Slf4j
@RequiredArgsConstructor
Expand All @@ -35,21 +37,8 @@ public class LocationServiceRT implements LocationService {
*/
public LocationPartDto.LatLong convertAddressToGeo(String address) {
// status, latitude, longitude 를 키로 가지는 HashMap 생성
LocationPartDto.LatLong locLl = LocationPartDto.LatLong.builder().build();
LocationKeywordSearchForm locationKeywordSearchForm = getLatLngByAddress(address);
if (locationKeywordSearchForm == null) { // 비어 있을 때 status-false 저장
return locLl;
}
LocationKeywordSearchForm.Documents latLngDoc
= Arrays.stream(locationKeywordSearchForm.getDocuments()).findAny().orElse(null);
if (latLngDoc != null) {
locLl = LocationPartDto.LatLong.builder()
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.status(true)
.build();
}
return locLl;
return LocationPartDto.LatLong.convertPart(locationKeywordSearchForm);
}

/**
Expand Down Expand Up @@ -136,21 +125,7 @@ private LocationKeywordSearchForm getCategoryByCode(String lat, String lng, Stri
public LocationInfoDto.LocationResponse convertGeoAndStoreNameToKeyword(String lat, String lng, String storeName) {
LocationKeywordSearchForm locationKeywordSearchForm = getCategoryByLatLngKeyword(lat, lng, storeName);
// 위도, 경도, 업장명을 가지고 업장 정보를 찾는다
if (locationKeywordSearchForm == null) {
return null;
}
LocationKeywordSearchForm.Documents latLngDoc
= Arrays.stream(locationKeywordSearchForm.getDocuments()).findFirst().orElse(null);
if (latLngDoc == null) {
return null;
}
return LocationInfoDto.LocationResponse.builder()
.storeId(Long.parseLong(latLngDoc.getId()))
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.category(categoryFilter(latLngDoc.getCategory_name()))
.storeName(latLngDoc.getPlace_name())
.phoneNumber(latLngDoc.getPhone()).build();
return LocationInfoDto.LocationResponse.KeywordToRes(locationKeywordSearchForm);
}

/**
Expand Down Expand Up @@ -204,21 +179,6 @@ private void getStoreResults(String lat, String lng, String address, String type
} while (locationKeywordSearchForm.getMeta().is_end()); // 마지막 페이지까지 검사
}


/**
* @param category 변환할 카테고리
* @return 카테고리의 중분류를 추출해 반환
*/
private String categoryFilter(String category) {
if (category == null) {
return Category.ETC.getDesc();
} else if (category.contains(">")) {
return Category.giveCategory(category.split(" > ")[1]).getDesc();
} else {
return null;
}
}

/**
* @param lat 위도
* @param lng 경도
Expand All @@ -242,26 +202,6 @@ public LocationPartDto.Address convertGeoToAddress(String lat, String lng) {
uri, HttpMethod.GET, new HttpEntity<>(headers), LocationAddressSearchForm.class
);
LocationAddressSearchForm locationKeywordSearchForm = resultRe.getBody();
return formToDto(locationKeywordSearchForm);
}

/**
* @param resultRe API에서 받아온 결과 Form
* @return DTO로 매핑한 결과
*/
private LocationPartDto.Address formToDto(LocationAddressSearchForm resultRe) {
LocationPartDto.Address addressInfoDto = LocationPartDto.Address.builder().build();
if (resultRe == null)
return addressInfoDto;

LocationAddressSearchForm.TotalAddress address = Arrays.stream(resultRe.getDocuments()).findFirst().orElse(null);
if (address == null) {
return addressInfoDto;
} else {
return LocationPartDto.Address.builder()
.address(address.getAddress().getAddress_name())
.status(true)
.build();
}
return LocationPartDto.Address.formToDto(locationKeywordSearchForm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mpnp.baechelin.api.dto.LocationPartDto;
import com.mpnp.baechelin.api.model.LocationAddressSearchForm;
import com.mpnp.baechelin.api.model.LocationKeywordSearchForm;
import com.mpnp.baechelin.common.DataClarification;
import com.mpnp.baechelin.common.httpclient.HttpConfig;
import com.mpnp.baechelin.store.domain.Category;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -39,21 +40,8 @@ public class LocationServiceWC implements LocationService {
*/
public LocationPartDto.LatLong convertAddressToGeo(String address) {
// status, latitude, longitude 를 키로 가지는 HashMap 생성
LocationPartDto.LatLong locLl = LocationPartDto.LatLong.builder().build();
LocationKeywordSearchForm locationKeywordSearchForm = getLatLngByAddress(address);
if (locationKeywordSearchForm == null) { // 비어 있을 때 status-false 저장
return locLl;
}
LocationKeywordSearchForm.Documents latLngDoc
= Arrays.stream(locationKeywordSearchForm.getDocuments()).findAny().orElse(null);
if (latLngDoc != null) {
locLl = LocationPartDto.LatLong.builder()
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.status(true)
.build();
}
return locLl;
return LocationPartDto.LatLong.convertPart(locationKeywordSearchForm);
}

/**
Expand Down Expand Up @@ -125,21 +113,7 @@ private LocationKeywordSearchForm getCategoryByCode(String lat, String lng, Stri
public LocationInfoDto.LocationResponse convertGeoAndStoreNameToKeyword(String lat, String lng, String storeName) {
LocationKeywordSearchForm locationKeywordSearchForm = getCategoryByLatLngKeyword(lat, lng, storeName);
// 위도, 경도, 업장명을 가지고 업장 정보를 찾는다
if (locationKeywordSearchForm == null) {
return null;
}
LocationKeywordSearchForm.Documents latLngDoc
= Arrays.stream(locationKeywordSearchForm.getDocuments()).findFirst().orElse(null);
if (latLngDoc == null) {
return null;
}
return LocationInfoDto.LocationResponse.builder()
.storeId(Long.valueOf(latLngDoc.getId()))
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.category(categoryFilter(latLngDoc.getCategory_name()))
.storeName(latLngDoc.getPlace_name())
.phoneNumber(latLngDoc.getPhone()).build();
return LocationInfoDto.LocationResponse.KeywordToRes(locationKeywordSearchForm);
}

/**
Expand Down Expand Up @@ -174,7 +148,7 @@ private void getStoreResults(String lat, String lng, String address, String type
LocationInfoDto.LocationResponse newResult = LocationInfoDto.LocationResponse.builder()
.latitude(latLngDoc.getY())
.longitude(latLngDoc.getX())
.category(categoryFilter(latLngDoc.getCategory_name()))
.category(DataClarification.categoryFilter(latLngDoc.getCategory_name()))
.storeName(latLngDoc.getPlace_name())
.storeId(Long.valueOf(latLngDoc.getId()))
.phoneNumber(latLngDoc.getPhone())
Expand All @@ -187,19 +161,7 @@ private void getStoreResults(String lat, String lng, String address, String type
} while (locationKeywordSearchForm.getMeta().is_end()); // 마지막 페이지까지 검사
}

/**
* @param category 변환할 카테고리
* @return 카테고리의 중분류를 추출해 반환
*/
private String categoryFilter(String category) {
if (category == null) {
return Category.ETC.getDesc();
} else if (category.contains(">")) {
return Category.giveCategory(category.split(" > ")[1]).getDesc();
} else {
return null;
}
}


public LocationPartDto.Address convertGeoToAddress(String lat, String lng) {
WebClient client = WebClient.builder()
Expand All @@ -218,22 +180,6 @@ public LocationPartDto.Address convertGeoToAddress(String lat, String lng) {
.toStream()
.findFirst()
.orElse(null);
return formToDto(locationAddressSearchForm);
}

private LocationPartDto.Address formToDto(LocationAddressSearchForm resultRe) {
LocationPartDto.Address addressInfoDto = LocationPartDto.Address.builder().build();
if (resultRe == null)
return addressInfoDto;

LocationAddressSearchForm.TotalAddress address = Arrays.stream(resultRe.getDocuments()).findFirst().orElse(null);
if (address == null) {
return addressInfoDto;
} else {
return LocationPartDto.Address.builder()
.address(address.getAddress().getAddress_name())
.status(true)
.build();
}
return LocationPartDto.Address.formToDto(locationAddressSearchForm);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/mpnp/baechelin/common/DataClarification.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package com.mpnp.baechelin.common;

import com.mpnp.baechelin.store.domain.Category;

public class DataClarification {
public static String clarifyString(String input){
// Trim
input = input.trim();
// replace all blanks
return input.replaceAll("\\s+", " ");
}
/**
* @param category 변환할 카테고리
* @return 카테고리의 중분류를 추출해 반환
*/
public static String categoryFilter(String category) {
if (category == null) {
return Category.ETC.getDesc();
} else if (category.contains(">")) {
return Category.giveCategory(category.split(" > ")[1]).getDesc();
} else {
return null;
}
}
}

0 comments on commit b9be48f

Please sign in to comment.