From c35440683ce63b479404b2fbee4fb29c8882f712 Mon Sep 17 00:00:00 2001 From: sungjindev Date: Sat, 20 Jan 2024 20:55:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor=20:=20=EC=95=BD=2020=EC=B4=88=20?= =?UTF-8?q?=EB=B3=91=EB=AA=A9=EC=9D=B4=20=EA=B1=B8=EB=A6=AC=EB=8D=98=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=EC=9D=84=20Hash=20=EA=B5=AC=EC=A1=B0?= =?UTF-8?q?=EB=A5=BC=20=EC=9D=B4=EC=9A=A9=ED=95=B4=EC=84=9C=200.5=EC=B4=88?= =?UTF-8?q?=EB=A1=9C=20=EA=B0=9C=EC=84=A0=20(#69)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StoreCertificationService.java | 19 ++++++++++--------- src/main/resources/backend-submodule | 2 +- src/test/resources/backend-submodule | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java b/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java index 510eb01..48ab22e 100644 --- a/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java +++ b/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java @@ -8,6 +8,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; @@ -22,16 +23,16 @@ public List findStoreCertificationsByLocation(Location north public List findStoreIdsWithMultipleCertifications() { List allStoreCertifications = storeCertificationRepository.findAll(); //중복된 id를 검사하기 위함 - List allStoreIds = new ArrayList<>(); - for (StoreCertification storeCertification : allStoreCertifications) { - allStoreIds.add(storeCertification.getStore().getId()); - } - List duplicatedIds = allStoreIds.stream() - .filter(e -> allStoreIds.indexOf(e) != allStoreIds.lastIndexOf(e)) //중복된 StoreId가 있는 경우 - .distinct() //해당 id를 모아서 1번씩만(중복 제거) 리스트에 담아 전달 - .collect(Collectors.toList()); + HashSet uniqueStoreIds = new HashSet<>(); //조회 성능을 높이기 위해 HashSet으로 저장 + HashSet duplicatedStoreIds = new HashSet<>(); - return duplicatedIds; + for (StoreCertification storeCertification : allStoreCertifications) { + Long storeId = storeCertification.getStore().getId(); + if (!uniqueStoreIds.add(storeId)) { //HashSet에 add를 했을 때 이미 존재하는 데이터면 false가 리턴되는 것을 활용 + duplicatedStoreIds.add(storeId); + } + } + return new ArrayList<>(duplicatedStoreIds); } } diff --git a/src/main/resources/backend-submodule b/src/main/resources/backend-submodule index ac45bcc..a51ab55 160000 --- a/src/main/resources/backend-submodule +++ b/src/main/resources/backend-submodule @@ -1 +1 @@ -Subproject commit ac45bcc38fb66231b0dc339bdfb9a6556bbe3aa3 +Subproject commit a51ab553892d30f7a7b7afaab5f26740c223af93 diff --git a/src/test/resources/backend-submodule b/src/test/resources/backend-submodule index ac3b02c..a51ab55 160000 --- a/src/test/resources/backend-submodule +++ b/src/test/resources/backend-submodule @@ -1 +1 @@ -Subproject commit ac3b02c9239a685ee51ec4874a6e703414bf183e +Subproject commit a51ab553892d30f7a7b7afaab5f26740c223af93 From 9650f3abab91157f452fd5f4cefa13e38d019d3c Mon Sep 17 00:00:00 2001 From: sungjindev Date: Sat, 20 Jan 2024 21:18:35 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor=20:=20=EB=B0=98=EB=B3=B5=EB=90=A0?= =?UTF-8?q?=20=ED=95=84=EC=9A=94=EA=B0=80=20=EC=97=86=EB=8A=94=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=A1=9C=EC=A7=81=EC=9D=84=20Service=EB=8B=A8?= =?UTF-8?q?=EC=97=90=20PostConstruct=20init=EC=9C=BC=EB=A1=9C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0=20(#69)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/StoreCertificationApi.java | 2 +- .../StoreCertificationService.java | 27 ++++++++++++++----- src/main/resources/backend-submodule | 2 +- src/test/resources/backend-submodule | 2 +- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/nainga/nainga/domain/storecertification/api/StoreCertificationApi.java b/src/main/java/com/nainga/nainga/domain/storecertification/api/StoreCertificationApi.java index 6ae5c2f..4d4dcad 100644 --- a/src/main/java/com/nainga/nainga/domain/storecertification/api/StoreCertificationApi.java +++ b/src/main/java/com/nainga/nainga/domain/storecertification/api/StoreCertificationApi.java @@ -45,7 +45,7 @@ public class StoreCertificationApi { @GetMapping("api/v1/storecertification/byLocation") public Result> findStoreCertificationsByLocation(@RequestParam("nwLong") double nwLong, @RequestParam("nwLat") double nwLat, @RequestParam("swLong") double swLong, @RequestParam("swLat") double swLat, @RequestParam("seLong") double seLong, @RequestParam("seLat") double seLat, @RequestParam("neLong") double neLong, @RequestParam("neLat") double neLat) { List storeCertificationsByLocation = storeCertificationService.findStoreCertificationsByLocation(new Location(nwLong, nwLat), new Location(swLong, swLat), new Location(seLong, seLat), new Location(neLong, neLat)); - List storeIdsWithMultipleCertifications = storeCertificationService.findStoreIdsWithMultipleCertifications(); //여러 인증제를 가지고 있는 가게의 id 리스트 + List storeIdsWithMultipleCertifications = storeCertificationService.getDuplicatedStoreIds(); //여러 인증제를 가지고 있는 가게의 id 리스트 List storeCertificationsByLocationResponses = new ArrayList<>(); //반환해줄 StoreCertificationsByLocationResponse들의 List Map map = new HashMap<>(); //여러 인증제를 가지고 있는 가게들의 response를 임시로 저장하고 있을 map diff --git a/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java b/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java index 48ab22e..1390ef4 100644 --- a/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java +++ b/src/main/java/com/nainga/nainga/domain/storecertification/application/StoreCertificationService.java @@ -3,7 +3,9 @@ import com.nainga.nainga.domain.store.domain.Location; import com.nainga.nainga.domain.storecertification.dao.StoreCertificationRepository; import com.nainga.nainga.domain.storecertification.domain.StoreCertification; +import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -14,25 +16,36 @@ @Service @Transactional(readOnly = true) -@RequiredArgsConstructor public class StoreCertificationService { private final StoreCertificationRepository storeCertificationRepository; - public List findStoreCertificationsByLocation(Location northWestLocation, Location southWestLocation, Location southEastLocation, Location northEastLocation) { - return storeCertificationRepository.findStoreCertificationsByLocation(northWestLocation, southWestLocation, southEastLocation, northEastLocation); + private List duplicatedStoreIds; //여러 인증제를 가지는 중복된 storeId를 담고있는 리스트 + + @Autowired + public StoreCertificationService(StoreCertificationRepository storeCertificationRepository) { + this.storeCertificationRepository = storeCertificationRepository; } - public List findStoreIdsWithMultipleCertifications() { + @PostConstruct + public void init() { //이 Service Bean이 생성된 이후에 한번만 중복된 storeId를 검사해서 Globally하게 저장 List allStoreCertifications = storeCertificationRepository.findAll(); //중복된 id를 검사하기 위함 HashSet uniqueStoreIds = new HashSet<>(); //조회 성능을 높이기 위해 HashSet으로 저장 - HashSet duplicatedStoreIds = new HashSet<>(); + HashSet duplicatedIds = new HashSet<>(); for (StoreCertification storeCertification : allStoreCertifications) { Long storeId = storeCertification.getStore().getId(); if (!uniqueStoreIds.add(storeId)) { //HashSet에 add를 했을 때 이미 존재하는 데이터면 false가 리턴되는 것을 활용 - duplicatedStoreIds.add(storeId); + duplicatedIds.add(storeId); } } - return new ArrayList<>(duplicatedStoreIds); + duplicatedStoreIds = new ArrayList<>(duplicatedIds); + } + + public List findStoreCertificationsByLocation(Location northWestLocation, Location southWestLocation, Location southEastLocation, Location northEastLocation) { + return storeCertificationRepository.findStoreCertificationsByLocation(northWestLocation, southWestLocation, southEastLocation, northEastLocation); + } + + public List getDuplicatedStoreIds() { + return duplicatedStoreIds; } } diff --git a/src/main/resources/backend-submodule b/src/main/resources/backend-submodule index a51ab55..67e3a62 160000 --- a/src/main/resources/backend-submodule +++ b/src/main/resources/backend-submodule @@ -1 +1 @@ -Subproject commit a51ab553892d30f7a7b7afaab5f26740c223af93 +Subproject commit 67e3a628d394ee510efccd846b3e70e63ce63e42 diff --git a/src/test/resources/backend-submodule b/src/test/resources/backend-submodule index a51ab55..67e3a62 160000 --- a/src/test/resources/backend-submodule +++ b/src/test/resources/backend-submodule @@ -1 +1 @@ -Subproject commit a51ab553892d30f7a7b7afaab5f26740c223af93 +Subproject commit 67e3a628d394ee510efccd846b3e70e63ce63e42