Skip to content

Commit

Permalink
#18 [Refactor] save 함수, Transaction 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 28, 2022
1 parent f54fb2f commit aae611f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
33 changes: 17 additions & 16 deletions src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ public void processApiV1(PublicApiRequestDto publicApiRequestDto) {
* @param publicApiV1Form API 호출 결과
*/
private void setInfos(PublicApiV1Form publicApiV1Form) {
publicApiV1Form.getTouristFoodInfo().getRow().forEach(row -> {
try {
if (!setRowLngLat(row)) return; // 주소를 가지고 위/경도를 찾는다
} catch (JsonProcessingException e) {
throw new CustomException(ErrorCode.API_LOAD_FAILURE);
}
try {
setRowCategoryAndId(row); // 위/경도/매장명을 가지고 키워드 설정
} catch (JsonProcessingException e) {
throw new CustomException(ErrorCode.API_LOAD_FAILURE);
}
}
);
for (PublicApiV1Form.Row row : publicApiV1Form.getTouristFoodInfo().getRow()) {
try {
if (!setRowLngLat(row)) return; // 주소를 가지고 위/경도를 찾는다
} catch (JsonProcessingException e) {
return;
}
try {
setRowCategoryAndId(row); // 위/경도/매장명을 가지고 키워드 설정
} catch (JsonProcessingException ignore) {
}
}
}


Expand Down Expand Up @@ -112,16 +110,19 @@ private void setRowCategoryAndId(PublicApiV1Form.Row row) throws JsonProcessingE
/**
* @param rows 검증할 행
*/
@Transactional
public void saveValidStores(List<PublicApiV1Form.Row> rows) {
List<Store> storeList = rows.stream().filter(PublicApiV1Form.Row::validation)
.map(Store::new).collect(Collectors.toList());
// storeRepository 구현 시 save 호출하기
for (Store store : storeList) {
if (!storeRepository.existsById(store.getId())) {
storeRepository.saveAndFlush(store);
storeImageService.saveImage(store.getId());
saveStore(store);
}
}
}
@Transactional
public void saveStore(Store store) {
storeRepository.save(store);
storeImageService.saveImage(store.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.net.URL;
import java.nio.file.Files;
import java.util.Optional;
import java.util.UUID;

@Service
Expand All @@ -39,19 +40,21 @@ public class StoreImageService {
private final StoreRepository storeRepository;
@Value("${user.agent}")
private String userAgent;

@Transactional
public void saveImage(Long storeId) {
Store store = storeRepository.findById(storeId).orElseThrow(() -> new CustomException(ErrorCode.WRONG_INPUT));
String storeImgUrl = saveImageByStoreId(storeId);
if (storeImgUrl == null) return;
Optional<Store> store = storeRepository.findById(storeId);
if (store.isEmpty()) return;
Optional<String> storeImgUrl = saveImageByStoreId(storeId);
if (storeImgUrl.isEmpty()) return;
StoreImage img = StoreImage.builder()
.store(store)
.storeImageUrl(storeImgUrl)
.store(store.get())
.storeImageUrl(storeImgUrl.get())
.build();
storeImgRepository.saveAndFlush(img);
storeImgRepository.save(img);
}

private String saveImageByStoreId(Long storeId) {
public Optional<String> saveImageByStoreId(Long storeId) {
String url = "https://place.map.kakao.com/placePrint.daum?confirmid=" + storeId;
Connection conn = Jsoup.connect(url);
Document doc = null;
Expand All @@ -64,15 +67,14 @@ private String saveImageByStoreId(Long storeId) {
Document document = response.parse();
Elements select = document.select("body div div div.popup_body div.wrap_info div img");
String val = select.select("img").attr("src");
if (val.equals("")) return null;
return downloadImage("https:" + val);
if (val.equals("")) return Optional.empty();
return Optional.ofNullable(downloadImage("https:" + val));
} catch (IOException ignored) {
throw new CustomException(ErrorCode.IMAGE_PROCESS_FAIL);
return Optional.empty();
}
}

private String downloadImage(String imgUrl) throws IOException {
log.info("imageurlcheck : {}", imgUrl);
ClassPathResource resource = new ClassPathResource("");
String fileName = resource.getPath() + UUID.randomUUID() + ".jpg";
URL url = new URL(imgUrl);
Expand All @@ -96,11 +98,11 @@ private String downloadImage(String imgUrl) throws IOException {
fileInputStream.close();
outputStream.close();
} catch (IOException ex) {
throw new CustomException(ErrorCode.IMAGE_PROCESS_FAIL);
return null;
}

MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
boolean deleteResult = file.delete();
file.delete();
return awsS3Manager.uploadFile(multipartFile);
}
}

0 comments on commit aae611f

Please sign in to comment.