diff --git a/server/src/main/java/com/talkka/server/review/controller/BusReviewController.java b/server/src/main/java/com/talkka/server/review/controller/BusReviewController.java index e51640d9..7f0f6ab0 100644 --- a/server/src/main/java/com/talkka/server/review/controller/BusReviewController.java +++ b/server/src/main/java/com/talkka/server/review/controller/BusReviewController.java @@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController; import com.talkka.server.common.exception.InvalidTypeException; +import com.talkka.server.common.exception.enums.InvalidTimeSlotEnumException; import com.talkka.server.oauth.domain.OAuth2UserInfo; import com.talkka.server.review.dto.BusReviewDto; import com.talkka.server.review.dto.BusReviewReqDto; @@ -38,19 +39,23 @@ public class BusReviewController { private final BusReviewService busReviewService; @GetMapping("") - public ResponseEntity> getBusReviewList( + public ResponseEntity getBusReviewList( @RequestParam Long routeId, @RequestParam(required = false) Long busRouteStationId, @RequestParam(required = false) String timeSlot ) { List reviewData; - if (busRouteStationId != null && timeSlot != null) { - reviewData = busReviewService.getBusReviewList(routeId, busRouteStationId, timeSlot); - } else if (busRouteStationId != null) { - reviewData = busReviewService.getBusReviewList(routeId, busRouteStationId); - } else { - reviewData = busReviewService.getBusReviewList(routeId); + try { + if (busRouteStationId != null && timeSlot != null) { + reviewData = busReviewService.getBusReviewList(routeId, busRouteStationId, timeSlot); + } else if (busRouteStationId != null) { + reviewData = busReviewService.getBusReviewList(routeId, busRouteStationId); + } else { + reviewData = busReviewService.getBusReviewList(routeId); + } + } catch (InvalidTimeSlotEnumException exception) { + return ResponseEntity.badRequest().body(exception.getMessage()); } return ResponseEntity.ok(reviewData); } diff --git a/server/src/main/java/com/talkka/server/review/service/BusReviewService.java b/server/src/main/java/com/talkka/server/review/service/BusReviewService.java index 82a6e720..c05f8e0b 100644 --- a/server/src/main/java/com/talkka/server/review/service/BusReviewService.java +++ b/server/src/main/java/com/talkka/server/review/service/BusReviewService.java @@ -10,7 +10,7 @@ import com.talkka.server.bus.dao.BusRouteStationEntity; import com.talkka.server.bus.dao.BusRouteStationRepository; import com.talkka.server.common.enums.TimeSlot; -import com.talkka.server.common.util.EnumCodeConverterUtils; +import com.talkka.server.common.exception.enums.InvalidTimeSlotEnumException; import com.talkka.server.common.validator.ContentAccessValidator; import com.talkka.server.review.dao.BusReviewEntity; import com.talkka.server.review.dao.BusReviewRepository; @@ -38,9 +38,10 @@ public class BusReviewService { public List getUsersBusReviewList( Long userId, Long routeId, Long busRouteStationId, String timeSlot - ) { - List reviewList = busReviewRepository.findAllByWriterIdAndRouteIdAndStationIdAndTimeSlotOrderByUpdatedAtDesc( - userId, routeId, busRouteStationId, EnumCodeConverterUtils.fromCode(TimeSlot.class, timeSlot)); + ) throws InvalidTimeSlotEnumException { + List reviewList = busReviewRepository + .findAllByWriterIdAndRouteIdAndStationIdAndTimeSlotOrderByUpdatedAtDesc( + userId, routeId, busRouteStationId, TimeSlot.valueOfEnumString(timeSlot)); return reviewList.stream() .map(BusReviewRespDto::of) @@ -62,9 +63,11 @@ public List getBusReviewList(Long routeId, Long busRouteStatio .toList(); } - public List getBusReviewList(Long routeId, Long busRouteStationId, String timeSlot) { - List reviewEntityList = busReviewRepository.findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc( - routeId, busRouteStationId, EnumCodeConverterUtils.fromCode(TimeSlot.class, timeSlot)); + public List getBusReviewList(Long routeId, Long busRouteStationId, String timeSlot) throws + InvalidTimeSlotEnumException { + List reviewEntityList = busReviewRepository + .findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc( + routeId, busRouteStationId, TimeSlot.valueOfEnumString(timeSlot)); return reviewEntityList.stream() .map(BusReviewRespDto::of) .toList(); diff --git a/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java b/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java index d35d44b5..635bf556 100644 --- a/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java +++ b/server/src/test/java/com/talkka/server/review/service/BusReviewServiceTest.java @@ -20,6 +20,7 @@ import com.talkka.server.bus.dao.BusRouteStationEntity; import com.talkka.server.bus.dao.BusRouteStationRepository; import com.talkka.server.common.enums.TimeSlot; +import com.talkka.server.common.exception.enums.InvalidTimeSlotEnumException; import com.talkka.server.common.validator.ContentAccessValidator; import com.talkka.server.review.dao.BusReviewEntity; import com.talkka.server.review.dao.BusReviewRepository; @@ -156,11 +157,21 @@ void testGetBusReviewListWithRouteIdAndBusRouteStationIdAndTimeSlot() { given(busReviewRepository.findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(anyLong(), anyLong(), any(TimeSlot.class))).willReturn(reviewEntityList); // when - busReviewService.getBusReviewList(236000050L, 1L, TimeSlot.T_00_00.getCode()); + busReviewService.getBusReviewList(236000050L, 1L, "T_00_00"); // then verify(busReviewRepository, times(1)).findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(anyLong(), anyLong(), any(TimeSlot.class)); } + + @Test + @DisplayName("TimeSlot이 잘못된 경우 InvalidTimeSlotEnumException을 발생시킨다.") + void testInvalidTimeSlotEnumException() { + // given + // when + // then + assertThatThrownBy(() -> busReviewService.getBusReviewList(236000050L, 1L, "T_00_00_00")) + .isInstanceOf(InvalidTimeSlotEnumException.class); + } } @Nested