Skip to content

Commit

Permalink
[hotfix] refactor: 이미 DB에 존재하는 장소의 경우 등록시 예외를 던지도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jinkonu committed Oct 30, 2024
1 parent 5dbbc3e commit 6736f3a
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public enum ErrorMessage {
*/
PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 장소입니다."),
PLATFORM_PLACE_NO_SEARCH_RESULT(HttpStatus.NOT_FOUND, "지도 검색 결과가 없습니다."),
PLACE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "이미 등록된 장소입니다."),

/**
* BOOKMARK
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/recordy/server/place/controller/PlaceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ public interface PlaceApi {
)
)
),
@ApiResponse(
responseCode = "400",
description = "이미 존재하는 장소입니다.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {
@ExampleObject(
value = """
{
"errorCode": "400 BAD REQUEST",
"errorMessage": "이미 존재하는 장소입니다."
}
"""
)
},
schema = @Schema(
implementation = ErrorResponse.class
)
)
),
@ApiResponse(
responseCode = "500",
description = "서버 내부 오류입니다.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface PlaceRepository {
Place save(Place place);

// query
boolean existsByPlatformId(String platformId);
Place findById(long id);
Place findByName(String name);
PlaceGetResponse findDetailById(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -50,6 +51,16 @@ public class PlaceQueryDslRepository {
locationGetResponse
);

public boolean existsByPlatformId(String platformId) {
return Objects.nonNull(
jpaQueryFactory
.selectOne()
.from(placeEntity)
.where(placeEntity.platformId.eq(platformId))
.fetchFirst()
);
}

public PlaceEntity findById(long id) {
return jpaQueryFactory
.select(placeEntity)
Expand All @@ -76,28 +87,28 @@ private Long findIdWith(BooleanExpression... expressions) {

public PlaceGetResponse findById(Long id) {
PlaceGetResponse place = Optional.ofNullable(jpaQueryFactory
.select(placeGetResponse)
.from(placeEntity)
.join(placeEntity.location, locationEntity)
.where(placeEntity.id.eq(id))
.fetchOne())
.select(placeGetResponse)
.from(placeEntity)
.join(placeEntity.location, locationEntity)
.where(placeEntity.id.eq(id))
.fetchOne())
.orElseThrow(() -> new PlaceException(ErrorMessage.PLACE_NOT_FOUND));

Long exhibitionSize = Optional.ofNullable(jpaQueryFactory
.select(exhibitionEntity.count())
.from(placeEntity)
.leftJoin(exhibitionEntity).on(exhibitionEntity.place.eq(placeEntity))
.where(placeEntity.id.eq(id))
.fetchOne())
.select(exhibitionEntity.count())
.from(placeEntity)
.leftJoin(exhibitionEntity).on(exhibitionEntity.place.eq(placeEntity))
.where(placeEntity.id.eq(id))
.fetchOne())
.orElse(0L);
place.setExhibitionSize(exhibitionSize);

Long recordSize = Optional.ofNullable(jpaQueryFactory
.select(recordEntity.count())
.from(placeEntity)
.leftJoin(recordEntity).on(recordEntity.place.id.eq(id))
.where(placeEntity.id.eq(id))
.fetchOne())
.select(recordEntity.count())
.from(placeEntity)
.leftJoin(recordEntity).on(recordEntity.place.id.eq(id))
.where(placeEntity.id.eq(id))
.fetchOne())
.orElse(0L);
place.setRecordSize(recordSize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public Place save(Place place) {
return Place.from(entity);
}

@Override
public boolean existsByPlatformId(String platformId) {
return placeQueryDslRepository.existsByPlatformId(platformId);
}

@Override
public Place findById(long id) {
PlaceEntity entity = placeQueryDslRepository.findById(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.recordy.server.place.service.impl;

import lombok.RequiredArgsConstructor;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.location.domain.Location;
import org.recordy.server.place.controller.dto.request.PlaceCreateRequest;
import org.recordy.server.place.controller.dto.response.PlaceGetResponse;
import org.recordy.server.place.controller.dto.response.PlaceReviewGetResponse;
import org.recordy.server.place.domain.Place;
import org.recordy.server.place.domain.usecase.PlaceCreate;
import org.recordy.server.place.exception.PlaceException;
import org.recordy.server.place.repository.PlaceRepository;
import org.recordy.server.place.repository.PlaceReviewRepository;
import org.recordy.server.place.service.PlaceService;
Expand All @@ -32,6 +34,10 @@ public class PlaceServiceImpl implements PlaceService {
@Transactional
@Override
public Place create(PlaceCreateRequest request) {
if (placeRepository.existsByPlatformId(request.id())) {
throw new PlaceException(ErrorMessage.PLACE_ALREADY_EXISTS);
}

Location location = Location.of(geometryConverter.of(request.latitude(), request.longitude()));
Place place = placeRepository.save(Place.create(PlaceCreate.from(request, location)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public Place save(Place place) {
return realPlace;
}

@Override
public boolean existsByPlatformId(String platformId) {
return false;
}

@Override
public Place findById(long id) {
return places.get(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PlatformPlaceServiceTest {
@ParameterizedTest
void 검색_결과중_가_정확도가_높은_장소에_대한_정보를_수집할__있다(String query) {
// when
List<PlatformPlaceSearchResponse> result = platformPlaceService.search(query, 0);
List<PlatformPlaceSearchResponse> result = platformPlaceService.search(query, 1);

// then
assertThat(result).isNotEmpty();
Expand Down

0 comments on commit 6736f3a

Please sign in to comment.