diff --git a/src/main/java/org/sophy/sophy/InitDb.java b/src/main/java/org/sophy/sophy/InitDb.java index da7dbb1..e74b5cd 100644 --- a/src/main/java/org/sophy/sophy/InitDb.java +++ b/src/main/java/org/sophy/sophy/InitDb.java @@ -1,9 +1,7 @@ package org.sophy.sophy; import lombok.RequiredArgsConstructor; -import org.sophy.sophy.domain.Author; -import org.sophy.sophy.domain.Authority; -import org.sophy.sophy.domain.Member; +import org.sophy.sophy.domain.*; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -65,6 +63,21 @@ public void dbInit() { author.setBookTalkCount(3); author.setBookCount(3); em.persist(author); + + Place place = Place.builder() + .name("장소") + .city(City.UIJEONGBU_SI) + .address("의정부시 무슨동 뭐시기") + .maximum(30) + .build(); + Place place2 = Place.builder() + .name("장소") + .city(City.GANEUNG_DONG) + .address("의정부시 가능동") + .maximum(10) + .build(); + em.persist(place); + em.persist(place2); } } } diff --git a/src/main/java/org/sophy/sophy/common/advice/ControllerExceptionAdvice.java b/src/main/java/org/sophy/sophy/common/advice/ControllerExceptionAdvice.java index 459b3ea..ecc9398 100644 --- a/src/main/java/org/sophy/sophy/common/advice/ControllerExceptionAdvice.java +++ b/src/main/java/org/sophy/sophy/common/advice/ControllerExceptionAdvice.java @@ -1,15 +1,16 @@ package org.sophy.sophy.common.advice; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; import io.jsonwebtoken.ExpiredJwtException; import io.lettuce.core.RedisCommandExecutionException; import org.sophy.sophy.common.dto.ApiResponseDto; import org.sophy.sophy.exception.ErrorStatus; +import org.sophy.sophy.exception.model.SophyException; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; -import org.sophy.sophy.exception.model.SophyException; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice @@ -35,6 +36,12 @@ protected ApiResponseDto handleExpiredRefreshTokenException(final ExpiredJwtExce return ApiResponseDto.error(ErrorStatus.REFRESH_TOKEN_TIME_EXPIRED_EXCEPTION); } + @ResponseStatus(HttpStatus.NOT_FOUND) + @ExceptionHandler(InvalidFormatException.class) + protected ApiResponseDto handleInvalidFormatException(final InvalidFormatException e) { + return ApiResponseDto.error(ErrorStatus.NOT_FOUND_CITY_EXCEPTION); + } + /** * Sopt custom error */ diff --git a/src/main/java/org/sophy/sophy/controller/PlaceController.java b/src/main/java/org/sophy/sophy/controller/PlaceController.java new file mode 100644 index 0000000..393df01 --- /dev/null +++ b/src/main/java/org/sophy/sophy/controller/PlaceController.java @@ -0,0 +1,26 @@ +package org.sophy.sophy.controller; + +import lombok.RequiredArgsConstructor; +import org.sophy.sophy.common.dto.ApiResponseDto; +import org.sophy.sophy.controller.dto.request.PlaceRequestDto; +import org.sophy.sophy.controller.dto.response.PlaceResponseDto; +import org.sophy.sophy.exception.SuccessStatus; +import org.sophy.sophy.service.PlaceService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/place") +public class PlaceController { + private final PlaceService placeService; + + @GetMapping + public ApiResponseDto> getPlacesByCity(@RequestBody PlaceRequestDto placeRequestDto) { + return ApiResponseDto.success(SuccessStatus.GET_PLACES_BY_CITY_SUCCESS, placeService.getPlacesByCity(placeRequestDto)); + } +} diff --git a/src/main/java/org/sophy/sophy/controller/dto/request/PlaceRequestDto.java b/src/main/java/org/sophy/sophy/controller/dto/request/PlaceRequestDto.java new file mode 100644 index 0000000..41489cf --- /dev/null +++ b/src/main/java/org/sophy/sophy/controller/dto/request/PlaceRequestDto.java @@ -0,0 +1,9 @@ +package org.sophy.sophy.controller.dto.request; + +import lombok.Getter; +import org.sophy.sophy.domain.City; + +@Getter +public class PlaceRequestDto { + private City city; +} diff --git a/src/main/java/org/sophy/sophy/controller/dto/response/PlaceResponseDto.java b/src/main/java/org/sophy/sophy/controller/dto/response/PlaceResponseDto.java new file mode 100644 index 0000000..afb1bbf --- /dev/null +++ b/src/main/java/org/sophy/sophy/controller/dto/response/PlaceResponseDto.java @@ -0,0 +1,24 @@ +package org.sophy.sophy.controller.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.sophy.sophy.domain.Place; + +@Getter +@AllArgsConstructor +public class PlaceResponseDto { + private Long placeId; + private String placeImage; + private String name; + private String address; + private Integer maximum; + + public static PlaceResponseDto of(Place place) { + return new PlaceResponseDto( + place.getId(), + place.getPlaceImage(), + place.getName(), + place.getAddress(), + place.getMaximum()); + } +} diff --git a/src/main/java/org/sophy/sophy/exception/ErrorStatus.java b/src/main/java/org/sophy/sophy/exception/ErrorStatus.java index 610e703..4e82687 100644 --- a/src/main/java/org/sophy/sophy/exception/ErrorStatus.java +++ b/src/main/java/org/sophy/sophy/exception/ErrorStatus.java @@ -28,6 +28,7 @@ public enum ErrorStatus { NOT_FOUND_USER_EXCEPTION(HttpStatus.NOT_FOUND, "존재하지 않는 유저입니다"), NOT_FOUND_SAVE_IMAGE_EXCEPTION(HttpStatus.NOT_FOUND, "이미지 저장에 실패했습니다"), NOT_FOUND_IMAGE_EXCEPTION(HttpStatus.NOT_FOUND, "이미지 이름을 찾을 수 없습니다"), + NOT_FOUND_CITY_EXCEPTION(HttpStatus.NOT_FOUND, "존재하지 않는 지역입니다"), /** * 409 CONFLICT diff --git a/src/main/java/org/sophy/sophy/exception/SuccessStatus.java b/src/main/java/org/sophy/sophy/exception/SuccessStatus.java index d0495db..705293d 100644 --- a/src/main/java/org/sophy/sophy/exception/SuccessStatus.java +++ b/src/main/java/org/sophy/sophy/exception/SuccessStatus.java @@ -18,6 +18,7 @@ public enum SuccessStatus { GET_MYINFO_SUCCESS(HttpStatus.OK, "내 정보를 성공적으로 불러왔습니다."), PATCH_MYINFO_SUCCESS(HttpStatus.OK, "내 정보를 성공적으로 수정했습니다."), POST_ADDITIONALINFO_SUCCESS(HttpStatus.OK, "내 정보를 성공적으로 추가했습니다."), + GET_PLACES_BY_CITY_SUCCESS(HttpStatus.OK, "지역으로 공간 리스트를 성공적으로 불러왔습니다"), TEST_SUCCESS(HttpStatus.OK, "Test :: OK"), /* * 201 created diff --git a/src/main/java/org/sophy/sophy/infrastructure/PlaceRepository.java b/src/main/java/org/sophy/sophy/infrastructure/PlaceRepository.java new file mode 100644 index 0000000..8e6e160 --- /dev/null +++ b/src/main/java/org/sophy/sophy/infrastructure/PlaceRepository.java @@ -0,0 +1,15 @@ +package org.sophy.sophy.infrastructure; + +import org.sophy.sophy.domain.City; +import org.sophy.sophy.domain.Place; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface PlaceRepository extends JpaRepository { + List findAllByCity(City city); + + List findAll(); +} diff --git a/src/main/java/org/sophy/sophy/service/PlaceService.java b/src/main/java/org/sophy/sophy/service/PlaceService.java new file mode 100644 index 0000000..6799789 --- /dev/null +++ b/src/main/java/org/sophy/sophy/service/PlaceService.java @@ -0,0 +1,30 @@ +package org.sophy.sophy.service; + +import lombok.RequiredArgsConstructor; +import org.sophy.sophy.controller.dto.request.PlaceRequestDto; +import org.sophy.sophy.controller.dto.response.PlaceResponseDto; +import org.sophy.sophy.domain.City; +import org.sophy.sophy.infrastructure.PlaceRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class PlaceService { + private final PlaceRepository placeRepository; + + public List getPlacesByCity(PlaceRequestDto placeRequestDto) { + City city = placeRequestDto.getCity(); + //의정부 시이면 전체 조회 + if (city == City.UIJEONGBU_SI) { + return placeRepository.findAll().stream() + .map(PlaceResponseDto::of) + .collect(Collectors.toList()); + } + return placeRepository.findAllByCity(city).stream() + .map(PlaceResponseDto::of) + .collect(Collectors.toList()); + } +}