Skip to content

Commit

Permalink
[feat] 식당 제보 검증 api (#153)
Browse files Browse the repository at this point in the history
* [feat] add isDeleted condition

* [feat] add dto for response

* [feat] logic for fail response

* [feat] logic for duplicated store validation

* [feat] delete fail response

* [feat] logic for validate duplicated store

* [reface] delete unnecessary method

* [reface] apply code reviews

* [refac] delete unnecessary function

* [fix] correcting parameter variable errors
  • Loading branch information
kgy1008 authored Aug 13, 2024
1 parent 437d39c commit 4d06542
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ public HankkiResponse<HeartDeleteResponse> deleteHeartStore(@UserId final Long u
}

@PostMapping("/stores/validate")
public HankkiResponse<Void> validateDuplicatedStore(@RequestBody final StoreDuplicateValidationRequest request) {
storeQueryService.validateDuplicatedStore(StoreValidationCommand.of(request));
return HankkiResponse.success(CommonSuccessCode.OK);
public HankkiResponse<StoreDuplicateValidationResponse> validateDuplicatedStore(@RequestBody final StoreDuplicateValidationRequest request) {
return HankkiResponse.success(CommonSuccessCode.OK, storeQueryService.validateDuplicatedStore(StoreValidationCommand.of(request.universityId(), request.latitude(), request.longitude())));
}

@PostMapping("/stores")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ protected Store findByIdWithHeartAndIsDeletedFalse(final Long id) {
.orElseThrow(() -> new NotFoundException(StoreErrorCode.STORE_NOT_FOUND));
}

protected Optional<Store> findByLatitudeAndLongitude(final double latitude, final double longitude) {
return storeRepository.findByPoint_LatitudeAndPoint_Longitude(latitude, longitude);
protected Optional<Store> findByLatitudeAndLongitudeWhereIsDeletedFalse(final double latitude, final double longitude) {
return storeRepository.findByPoint_LatitudeAndPoint_LongitudeAndIsDeletedFalse(latitude, longitude);
}

protected boolean existsByLatitudeAndLongitude(final double latitude, final double longitude) {
Expand All @@ -44,4 +44,4 @@ public List<Store> findAllDynamicQuery(final Long universityId, final StoreCateg
public List<Store> findAllByIdsWhereDeletedIsFalseOrderByCreatedAtDes(final List<Long> ids) {
return storeRepository.findAllByIdsAndIsDeletedIsFalseOrderByCreatedAtDesc(ids);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, "잘못된 식당 정보입니다.");

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Store, Long>, CustomStoreRepository {
@Query("select s from Store s where s.id = :id and s.isDeleted = false")
Expand All @@ -15,11 +15,11 @@ public interface StoreRepository extends JpaRepository<Store, Long>, CustomStore
@Query("select distinct s from Store s left join fetch s.hearts where s.id = :id and s.isDeleted = false")
Optional<Store> findByIdWithHeartAndIsDeletedFalse(Long id);

@Query("select s from Store s where s.point.latitude = :latitude and s.point.longitude = :longitude")
Optional<Store> 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<Store> 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<Store> findAllByIdsAndIsDeletedIsFalseOrderByCreatedAtDesc(@Param("ids") List<Long> ids);
}
}

0 comments on commit 4d06542

Please sign in to comment.