Skip to content

Commit

Permalink
Merge pull request #70 from Korea-Certified-Store/refactor/refactor-s…
Browse files Browse the repository at this point in the history
…torecertification-api(#69)

가게 조회 API 성능 개선 (#69)
  • Loading branch information
sungjindev authored Jan 20, 2024
2 parents 8e7046a + 2f3c308 commit 0785272
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class StoreCertificationApi {
@GetMapping("api/v1/storecertification/byLocation")
public Result<List<StoreCertificationsByLocationResponse>> 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<StoreCertification> storeCertificationsByLocation = storeCertificationService.findStoreCertificationsByLocation(new Location(nwLong, nwLat), new Location(swLong, swLat), new Location(seLong, seLat), new Location(neLong, neLat));
List<Long> storeIdsWithMultipleCertifications = storeCertificationService.findStoreIdsWithMultipleCertifications(); //여러 인증제를 가지고 있는 가게의 id 리스트
List<Long> storeIdsWithMultipleCertifications = storeCertificationService.getDuplicatedStoreIds(); //여러 인증제를 가지고 있는 가게의 id 리스트
List<StoreCertificationsByLocationResponse> storeCertificationsByLocationResponses = new ArrayList<>(); //반환해줄 StoreCertificationsByLocationResponse들의 List
Map<Long, StoreCertificationsByLocationResponse> map = new HashMap<>(); //여러 인증제를 가지고 있는 가게들의 response를 임시로 저장하고 있을 map

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,49 @@
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;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class StoreCertificationService {
private final StoreCertificationRepository storeCertificationRepository;
public List<StoreCertification> findStoreCertificationsByLocation(Location northWestLocation, Location southWestLocation, Location southEastLocation, Location northEastLocation) {
return storeCertificationRepository.findStoreCertificationsByLocation(northWestLocation, southWestLocation, southEastLocation, northEastLocation);
private List<Long> duplicatedStoreIds; //여러 인증제를 가지는 중복된 storeId를 담고있는 리스트

@Autowired
public StoreCertificationService(StoreCertificationRepository storeCertificationRepository) {
this.storeCertificationRepository = storeCertificationRepository;
}

public List<Long> findStoreIdsWithMultipleCertifications() {
@PostConstruct
public void init() { //이 Service Bean이 생성된 이후에 한번만 중복된 storeId를 검사해서 Globally하게 저장
List<StoreCertification> allStoreCertifications = storeCertificationRepository.findAll(); //중복된 id를 검사하기 위함
List<Long> allStoreIds = new ArrayList<>();

HashSet<Long> uniqueStoreIds = new HashSet<>(); //조회 성능을 높이기 위해 HashSet으로 저장
HashSet<Long> duplicatedIds = new HashSet<>();

for (StoreCertification storeCertification : allStoreCertifications) {
allStoreIds.add(storeCertification.getStore().getId());
Long storeId = storeCertification.getStore().getId();
if (!uniqueStoreIds.add(storeId)) { //HashSet에 add를 했을 때 이미 존재하는 데이터면 false가 리턴되는 것을 활용
duplicatedIds.add(storeId);
}
}
duplicatedStoreIds = new ArrayList<>(duplicatedIds);
}

List<Long> duplicatedIds = allStoreIds.stream()
.filter(e -> allStoreIds.indexOf(e) != allStoreIds.lastIndexOf(e)) //중복된 StoreId가 있는 경우
.distinct() //해당 id를 모아서 1번씩만(중복 제거) 리스트에 담아 전달
.collect(Collectors.toList());
public List<StoreCertification> findStoreCertificationsByLocation(Location northWestLocation, Location southWestLocation, Location southEastLocation, Location northEastLocation) {
return storeCertificationRepository.findStoreCertificationsByLocation(northWestLocation, southWestLocation, southEastLocation, northEastLocation);
}

return duplicatedIds;
public List<Long> getDuplicatedStoreIds() {
return duplicatedStoreIds;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/backend-submodule
2 changes: 1 addition & 1 deletion src/test/resources/backend-submodule

0 comments on commit 0785272

Please sign in to comment.