From aae611fa177435a28e348f9157d2322346668b45 Mon Sep 17 00:00:00 2001 From: JSoi Date: Thu, 28 Jul 2022 23:09:48 +0900 Subject: [PATCH] =?UTF-8?q?#18=20[Refactor]=20save=20=ED=95=A8=EC=88=98,?= =?UTF-8?q?=20Transaction=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/PublicApiService.java | 33 ++++++++++--------- .../store/service/StoreImageService.java | 28 ++++++++-------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java b/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java index 8e601d0..f9e29c8 100644 --- a/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java +++ b/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java @@ -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) { + } + } } @@ -112,16 +110,19 @@ private void setRowCategoryAndId(PublicApiV1Form.Row row) throws JsonProcessingE /** * @param rows 검증할 행 */ - @Transactional public void saveValidStores(List rows) { List 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()); + } } diff --git a/src/main/java/com/mpnp/baechelin/store/service/StoreImageService.java b/src/main/java/com/mpnp/baechelin/store/service/StoreImageService.java index 2564c1d..5f31ba7 100644 --- a/src/main/java/com/mpnp/baechelin/store/service/StoreImageService.java +++ b/src/main/java/com/mpnp/baechelin/store/service/StoreImageService.java @@ -28,6 +28,7 @@ import java.net.URL; import java.nio.file.Files; +import java.util.Optional; import java.util.UUID; @Service @@ -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 = storeRepository.findById(storeId); + if (store.isEmpty()) return; + Optional 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 saveImageByStoreId(Long storeId) { String url = "https://place.map.kakao.com/placePrint.daum?confirmid=" + storeId; Connection conn = Jsoup.connect(url); Document doc = null; @@ -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); @@ -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); } }