Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.onebyone.kindergarten.domain.address.repository;

import com.onebyone.kindergarten.domain.address.entity.SubRegion;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SubRegionRepository extends JpaRepository<SubRegion, Long> {
SubRegion findBySubRegionId(Long subRegionId);

List<SubRegion> findAllByParent(SubRegion subRegion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ List<Kindergarten> findNearbyKindergartenEntities(
+ "AND k.subRegionId = :subRegionId")
List<Kindergarten> findAllByRegionIdAndSubRegionIdWithFetch(
@Param("regionId") Long regionId, @Param("subRegionId") Long subRegionId);

@Query(
"SELECT k FROM Kindergarten k "
+ "LEFT JOIN FETCH k.kindergartenWorkReviewAggregate w "
+ "LEFT JOIN FETCH k.kindergartenInternshipReviewAggregate i "
+ "WHERE k.regionId = :regionId "
+ "AND k.subRegionId in :ids")
List<Kindergarten> findAllByRegionIdAndSubRegionIdInWithFetch(Long regionId, List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,32 @@ public List<KindergartenResponseDTO> getKindergartenByRegion(Long regionId, Long
throw new BusinessException(ErrorCodes.REGION_NOT_FOUND_EXCEPTION);
}

SubRegion subRegion = subRegionRepository.findBySubRegionId(subRegionId);

if (subRegion == null) {
if (subRegionId == null) {
return kindergartenRepository.findAllByRegionIdWithFetch(regionId).stream()
.map(KindergartenResponseDTO::from)
.toList();
}
SubRegion subRegion = subRegionRepository.findBySubRegionId(subRegionId);

if (subRegion == null) {
throw new BusinessException(ErrorCodes.SUB_REGION_NOT_FOUND_EXCEPTION);
}

if (!regionId.equals(subRegion.getRegionId())) {
throw new BusinessException(ErrorCodes.REGION_NOT_MATCHED_WITH_SUB_REGION);
}

List<SubRegion> allByParentId = subRegionRepository.findAllByParent(subRegion);

if (!allByParentId.isEmpty()) {
List<Long> ids = allByParentId.stream().map(SubRegion::getSubRegionId).toList();
return kindergartenRepository
.findAllByRegionIdAndSubRegionIdInWithFetch(regionId, ids)
.stream()
.map(KindergartenResponseDTO::from)
.toList();
}

return kindergartenRepository
.findAllByRegionIdAndSubRegionIdWithFetch(regionId, subRegionId)
.stream()
Expand Down