diff --git a/src/main/java/org/hankki/hankkiserver/api/store/controller/StoreController.java b/src/main/java/org/hankki/hankkiserver/api/store/controller/StoreController.java index 1c2172f5..72199b30 100644 --- a/src/main/java/org/hankki/hankkiserver/api/store/controller/StoreController.java +++ b/src/main/java/org/hankki/hankkiserver/api/store/controller/StoreController.java @@ -82,9 +82,8 @@ public HankkiResponse deleteHeartStore(@UserId final Long u } @PostMapping("/stores/validate") - public HankkiResponse validateDuplicatedStore(@RequestBody final StoreDuplicateValidationRequest request) { - storeQueryService.validateDuplicatedStore(StoreValidationCommand.of(request)); - return HankkiResponse.success(CommonSuccessCode.OK); + public HankkiResponse validateDuplicatedStore(@RequestBody final StoreDuplicateValidationRequest request) { + return HankkiResponse.success(CommonSuccessCode.OK, storeQueryService.validateDuplicatedStore(StoreValidationCommand.of(request.universityId(), request.latitude(), request.longitude()))); } @PostMapping("/stores") diff --git a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java index 5e27f8b0..e87d3c7f 100644 --- a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreFinder.java @@ -29,8 +29,8 @@ protected Store findByIdWithHeartAndIsDeletedFalse(final Long id) { .orElseThrow(() -> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND)); } - protected Optional findByLatitudeAndLongitude(final double latitude, final double longitude) { - return storeRepository.findByPoint_LatitudeAndPoint_Longitude(latitude, longitude); + protected Optional findByLatitudeAndLongitudeWhereIsDeletedFalse(final double latitude, final double longitude) { + return storeRepository.findByPoint_LatitudeAndPoint_LongitudeAndIsDeletedFalse(latitude, longitude); } protected boolean existsByLatitudeAndLongitude(final double latitude, final double longitude) { @@ -44,4 +44,4 @@ public List findAllDynamicQuery(final Long universityId, final StoreCateg public List findAllByIdsWhereDeletedIsFalseOrderByCreatedAtDes(final List ids) { return storeRepository.findAllByIdsAndIsDeletedIsFalseOrderByCreatedAtDesc(ids); } -} \ No newline at end of file +} diff --git a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java index d52342a4..58421b42 100644 --- a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java +++ b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java @@ -7,8 +7,6 @@ import org.hankki.hankkiserver.api.store.parameter.SortOption; import org.hankki.hankkiserver.api.store.service.command.StoreValidationCommand; import org.hankki.hankkiserver.api.store.service.response.*; -import org.hankki.hankkiserver.common.code.StoreErrorCode; -import org.hankki.hankkiserver.common.exception.ConflictException; import org.hankki.hankkiserver.domain.heart.model.Heart; import org.hankki.hankkiserver.domain.store.model.Store; import org.hankki.hankkiserver.domain.store.model.StoreCategory; @@ -93,19 +91,9 @@ private boolean hasSameUserId(final Long id, final Heart heart) { } @Transactional(readOnly = true) - public void validateDuplicatedStore(final StoreValidationCommand command) { - storeFinder.findByLatitudeAndLongitude(command.latitude(), command.longitude()) - .ifPresent(store -> findUniversityStore(command.universityId(), store)); - - } - - private void findUniversityStore(final Long universityId, final Store store) { - if (isExistedUniversityStore(universityId, store)) { - throw new ConflictException(StoreErrorCode.STORE_ALREADY_REGISTERED); - } - } - - private boolean isExistedUniversityStore(final Long universityId, final Store store) { - return universityStoreFinder.existsByUniversityIdAndStore(universityId, store); + public StoreDuplicateValidationResponse validateDuplicatedStore(final StoreValidationCommand command) { + return storeFinder.findByLatitudeAndLongitudeWhereIsDeletedFalse(command.latitude(), command.longitude()) + .map(store -> StoreDuplicateValidationResponse.of(store.getId(), universityStoreFinder.existsByUniversityIdAndStore(command.universityId(), store))) + .orElseGet(() -> StoreDuplicateValidationResponse.of(null, false)); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/store/service/command/StoreValidationCommand.java b/src/main/java/org/hankki/hankkiserver/api/store/service/command/StoreValidationCommand.java index 6159d1c1..39fa4404 100644 --- a/src/main/java/org/hankki/hankkiserver/api/store/service/command/StoreValidationCommand.java +++ b/src/main/java/org/hankki/hankkiserver/api/store/service/command/StoreValidationCommand.java @@ -1,13 +1,11 @@ package org.hankki.hankkiserver.api.store.service.command; -import org.hankki.hankkiserver.api.store.controller.request.StoreDuplicateValidationRequest; - public record StoreValidationCommand ( long universityId, double latitude, double longitude ) { - public static StoreValidationCommand of(StoreDuplicateValidationRequest request) { - return new StoreValidationCommand(request.universityId(), request.latitude(), request.longitude()); + public static StoreValidationCommand of(long universityId, double latitude, double longitude) { + return new StoreValidationCommand(universityId, latitude, longitude); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/store/service/response/StoreDuplicateValidationResponse.java b/src/main/java/org/hankki/hankkiserver/api/store/service/response/StoreDuplicateValidationResponse.java new file mode 100644 index 00000000..33f902dc --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/store/service/response/StoreDuplicateValidationResponse.java @@ -0,0 +1,10 @@ +package org.hankki.hankkiserver.api.store.service.response; + +public record StoreDuplicateValidationResponse( + Long id, + boolean isRegistered +) { + public static StoreDuplicateValidationResponse of(Long id, boolean isRegistered) { + return new StoreDuplicateValidationResponse(id, isRegistered); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/common/code/StoreErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/StoreErrorCode.java index 7c3ba6ba..09505ad8 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/StoreErrorCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/StoreErrorCode.java @@ -9,7 +9,6 @@ public enum StoreErrorCode implements ErrorCode { STORE_NOT_FOUND(HttpStatus.NOT_FOUND, "가게를 찾을 수 없습니다."), - STORE_ALREADY_REGISTERED(HttpStatus.CONFLICT, "이미 등록된 가게입니다."), STORE_FILE_SIZE_EXCEEDED(HttpStatus.BAD_REQUEST, "파일 크기가 초과되었습니다."), BAD_STORE_INFO(HttpStatus.BAD_REQUEST, "잘못된 식당 정보입니다."); diff --git a/src/main/java/org/hankki/hankkiserver/domain/store/repository/StoreRepository.java b/src/main/java/org/hankki/hankkiserver/domain/store/repository/StoreRepository.java index 23bce28d..9916a65f 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/store/repository/StoreRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/store/repository/StoreRepository.java @@ -1,12 +1,12 @@ package org.hankki.hankkiserver.domain.store.repository; -import java.util.List; import org.hankki.hankkiserver.domain.store.model.Store; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import java.util.List; import java.util.Optional; -import org.springframework.data.repository.query.Param; public interface StoreRepository extends JpaRepository, CustomStoreRepository { @Query("select s from Store s where s.id = :id and s.isDeleted = false") @@ -15,11 +15,11 @@ public interface StoreRepository extends JpaRepository, CustomStore @Query("select distinct s from Store s left join fetch s.hearts where s.id = :id and s.isDeleted = false") Optional findByIdWithHeartAndIsDeletedFalse(Long id); - @Query("select s from Store s where s.point.latitude = :latitude and s.point.longitude = :longitude") - Optional findByPoint_LatitudeAndPoint_Longitude(double latitude, double longitude); + @Query("select s from Store s where s.point.latitude = :latitude and s.point.longitude = :longitude and s.isDeleted = false") + Optional findByPoint_LatitudeAndPoint_LongitudeAndIsDeletedFalse(double latitude, double longitude); boolean existsByPoint_LatitudeAndPoint_Longitude(double latitude, double longitude); @Query("select s from Store s where s.id in :ids and s.isDeleted = false order by s.createdAt desc") List findAllByIdsAndIsDeletedIsFalseOrderByCreatedAtDesc(@Param("ids") List ids); -} \ No newline at end of file +}