Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] FE 통합에 따른 API 변경 (#65) #66

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
@Builder
public record BusRouteStationRespDto(
Long busRouteStationId,
BusRouteRespDto route,
BusStationRespDto station,
Short stationSeq,
String stationName,
Long routeId,
String routeName,
Long stationId,
LocalDateTime createdAt
) {
public static BusRouteStationRespDto of(BusRouteStationEntity busRouteStationEntity) {
return new BusRouteStationRespDto(
busRouteStationEntity.getId(),
BusRouteRespDto.of(busRouteStationEntity.getRoute()),
BusStationRespDto.of(busRouteStationEntity.getStation()),
busRouteStationEntity.getStationSeq(),
busRouteStationEntity.getStationName(),
busRouteStationEntity.getStation().getStationName(),
busRouteStationEntity.getRoute().getId(),
busRouteStationEntity.getRoute().getRouteName(),
busRouteStationEntity.getStation().getId(),
busRouteStationEntity.getCreatedAt()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public class BusReviewController {

@GetMapping("")
public ResponseEntity<ApiRespDto<List<BusReviewRespDto>>> getBusReviewList(
@AuthenticationPrincipal OAuth2UserInfo oAuth2UserInfo,
@RequestParam Long routeId,
@RequestParam Long busRouteStationId,
@RequestParam String timeSlot
@RequestParam(required = true) Long routeId,
@RequestParam(required = false) Long busRouteStationId,
@RequestParam(required = false) String timeSlot
) {
List<BusReviewRespDto> reviewData = busReviewService.getBusReviewList(oAuth2UserInfo.getUserId(), routeId,
busRouteStationId, timeSlot);
List<BusReviewRespDto> 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);
}

return ResponseEntity.ok(
ApiRespDto.<List<BusReviewRespDto>>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@

@Repository
public interface BusReviewRepository extends JpaRepository<BusReviewEntity, Long> {
List<BusReviewEntity> findAllByWriterIdAndRouteIdAndStationIdAndTimeSlot(

List<BusReviewEntity> findAllByWriterIdAndRouteIdAndStationIdAndTimeSlotOrderByUpdatedAtDesc(
Long userId,
Long routeId,
Long busRouteStationId,
TimeSlot timeSlot);

List<BusReviewEntity> findAllByRouteIdOrderByCreatedAtDesc(Long routeId);

List<BusReviewEntity> findAllByRouteIdAndStationIdOrderByCreatedAtDesc(Long routeId, Long stationId);

List<BusReviewEntity> findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(
Long routeId,
Long stationId,
TimeSlot timeSlot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
@Builder
public record BusReviewRespDto(
Long userId,
String userName,
Long routeId,
String routeName,
Long busRouteStationId,
String stationName,
String content,
String timeSlot,
Integer rating
) {
public static BusReviewRespDto of(BusReviewEntity busEntity) {
return new BusReviewRespDto(
busEntity.getWriter().getId(),
busEntity.getWriter().getName(),
busEntity.getRoute().getId(),
busEntity.getRoute().getRouteName(),
busEntity.getStation().getId(),
busEntity.getStation().getStationName(),
busEntity.getContent(),
busEntity.getTimeSlot().getCode(),
busEntity.getRating()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,42 @@ public class BusReviewService {
private final BusRouteStationRepository busRouteStationRepository;
private final BusRouteRepository busRouteRepository;

public List<BusReviewRespDto> getBusReviewList(
public List<BusReviewRespDto> getUsersBusReviewList(
Long userId, Long routeId, Long busRouteStationId, String timeSlot
) {
List<BusReviewEntity> reviewList = busReviewRepository.findAllByWriterIdAndRouteIdAndStationIdAndTimeSlot(
// request param의 존재 유무에 따라 response가 변경되어야합니다.
List<BusReviewEntity> reviewList = busReviewRepository.findAllByWriterIdAndRouteIdAndStationIdAndTimeSlotOrderByUpdatedAtDesc(
userId, routeId, busRouteStationId, EnumCodeConverterUtils.fromCode(TimeSlot.class, timeSlot));

return reviewList.stream()
.map(BusReviewRespDto::of)
.toList();
}

public List<BusReviewRespDto> getBusReviewList(Long routeId) {
List<BusReviewEntity> reviewEntityList = busReviewRepository.findAllByRouteIdOrderByCreatedAtDesc(routeId);
return reviewEntityList.stream()
.map(BusReviewRespDto::of)
.toList();
}

public List<BusReviewRespDto> getBusReviewList(Long routeId, Long busRouteStationId) {
List<BusReviewEntity> reviewEntityList = busReviewRepository.findAllByRouteIdAndStationIdOrderByCreatedAtDesc(
routeId, busRouteStationId);
return reviewEntityList.stream()
.map(BusReviewRespDto::of)
.toList();
}

public List<BusReviewRespDto> getBusReviewList(Long routeId, Long busRouteStationId, String timeSlot) {
List<BusReviewEntity> reviewEntityList = busReviewRepository.findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(
routeId, busRouteStationId, EnumCodeConverterUtils.fromCode(TimeSlot.class, timeSlot));
return reviewEntityList.stream()
.map(BusReviewRespDto::of)
.toList();
}

@Transactional
public BusReviewRespDto createBusReview(Long userId, BusReviewReqDto busReviewReqDto) {
UserEntity user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 유저입니다."));
Expand Down Expand Up @@ -77,6 +102,7 @@ public BusReviewRespDto updateBusReview(Long userId, Long busReviewId, BusReview
return BusReviewRespDto.of(review);
}

@Transactional
public Long deleteBusReview(Long userId, Long busReviewId) {
BusReviewEntity review = busReviewRepository.findById(busReviewId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 리뷰입니다."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ public static BusRouteStationRespDto getBusRouteStationRespDto(Long id, BusRoute
BusStationRespDto stationRespDto) {
return BusRouteStationRespDto.builder()
.busRouteStationId(id)
.route(routeRespDto)
.station(stationRespDto)
.stationName("정거장" + id)
.stationId(stationRespDto.stationId())
.stationName("정거장" + stationRespDto.stationId())
.stationSeq(Short.valueOf(String.valueOf(id)))
.routeId(routeRespDto.routeId())
.routeName(routeRespDto.routeName())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -49,8 +51,11 @@ public class BusReviewServiceTest {
private BusReviewRespDto busReviewRespDtoFixture(Long userId) {
return BusReviewRespDto.builder()
.userId(userId)
.userName("유저 이름")
.routeId(236000050L)
.routeName("노선 이름")
.busRouteStationId(1L)
.stationName("정류장 이름")
.content("리뷰 내용")
.timeSlot(TimeSlot.T_00_00.getCode())
.rating(4)
Expand All @@ -60,27 +65,165 @@ private BusReviewRespDto busReviewRespDtoFixture(Long userId) {
private UserEntity getUserFixture(Long userId) {
return UserEntity.builder()
.id(userId)
.name("유저 이름")
.build();
}

private BusRouteStationEntity getBusRouteStationFixture(Long busRouteStationId) {
return BusRouteStationEntity.builder()
.id(busRouteStationId)
.stationName("정류장 이름")
.build();
}

private BusRouteEntity getBusRouteFixture(Long routeId) {
return BusRouteEntity.builder()
.id(routeId)
.routeName("노선 이름")
.build();
}

private List<BusReviewEntity> getReviewEntity(Long routeId, Long busRouteStationId, TimeSlot timeSlot) {
if (busRouteStationId == null) {
return List.of(
BusReviewEntity.builder()
.id(1L)
.content("리뷰 내용")
.timeSlot(TimeSlot.T_00_00)
.rating(4)
.writer(getUserFixture(1L))
.station(getBusRouteStationFixture(1L))
.route(getBusRouteFixture(routeId))
.createdAt(LocalDateTime.now().plusMinutes(1))
.updatedAt(LocalDateTime.now())
.build(),
BusReviewEntity.builder()
.id(2L)
.content("리뷰 내용")
.timeSlot(TimeSlot.T_00_00)
.rating(5)
.writer(getUserFixture(1L))
.station(getBusRouteStationFixture(1L))
.route(getBusRouteFixture(routeId))
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.build()
);
} else if (timeSlot == null) {
return List.of(
BusReviewEntity.builder()
.id(1L)
.content("리뷰 내용")
.timeSlot(TimeSlot.T_00_00)
.rating(4)
.writer(getUserFixture(1L))
.station(getBusRouteStationFixture(busRouteStationId))
.route(getBusRouteFixture(routeId))
.createdAt(LocalDateTime.now().plusMinutes(1))
.updatedAt(LocalDateTime.now())
.build(),
BusReviewEntity.builder()
.id(2L)
.content("리뷰 내용")
.timeSlot(TimeSlot.T_00_00)
.rating(5)
.writer(getUserFixture(1L))
.station(getBusRouteStationFixture(busRouteStationId))
.route(getBusRouteFixture(routeId))
.createdAt(LocalDateTime.now().plusMinutes(1))
.updatedAt(LocalDateTime.now())
.build()
);
} else {
return List.of(
BusReviewEntity.builder()
.id(1L)
.content("리뷰 내용")
.timeSlot(timeSlot)
.rating(4)
.writer(getUserFixture(1L))
.station(getBusRouteStationFixture(busRouteStationId))
.route(getBusRouteFixture(routeId))
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.build(),
BusReviewEntity.builder()
.id(2L)
.content("리뷰 내용")
.timeSlot(timeSlot)
.rating(5)
.writer(getUserFixture(1L))
.station(getBusRouteStationFixture(busRouteStationId))
.route(getBusRouteFixture(routeId))
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.build()
);
}
}

@Nested
@DisplayName("getBusReviewList 메서드 테스트")
public class GetBusReviewListTest {
@Test
void 버스_노선_아이디_대한_리뷰를_조회한다() {
// given
Long routeId = 236000050L;
List<BusReviewEntity> reviewEntityList = getReviewEntity(routeId, null, null);
var expected = reviewEntityList.stream().map(BusReviewRespDto::of).toList();

given(busReviewRepository.findAllByRouteIdOrderByCreatedAtDesc(anyLong())).willReturn(reviewEntityList);
// when
var result = busReviewService.getBusReviewList(routeId);
// then
verify(busReviewRepository, times(1)).findAllByRouteIdOrderByCreatedAtDesc(routeId);
assertThat(result).isEqualTo(expected);
}

@Test
void 버스_노선_아이디와_정거장에_대한_리뷰를_조회한다() {
// given
Long routeId = 236000050L;
Long busRouteStationId = 1L;

List<BusReviewEntity> reviewEntityList = getReviewEntity(routeId, busRouteStationId, null);
var expected = reviewEntityList.stream().map(BusReviewRespDto::of).toList();
given(busReviewRepository.findAllByRouteIdAndStationIdOrderByCreatedAtDesc(anyLong(), anyLong()))
.willReturn(reviewEntityList);
// when
var result = busReviewService.getBusReviewList(routeId, busRouteStationId);
// then
verify(busReviewRepository, times(1)).findAllByRouteIdAndStationIdOrderByCreatedAtDesc(routeId,
busRouteStationId);
assertThat(result).isEqualTo(expected);
}

@Test
void 버스_노선_아이디와_정거장_그리고_타임슬롯에_대한_리뷰를_조회한다() {
// given
Long routeId = 236000050L;
Long busRouteStationId = 1L;
TimeSlot timeSlot = EnumCodeConverterUtils.fromCode(TimeSlot.class, TimeSlot.T_00_00.getCode());

List<BusReviewEntity> reviewEntityList = getReviewEntity(routeId, busRouteStationId, timeSlot);
var expected = reviewEntityList.stream().map(BusReviewRespDto::of).toList();
given(busReviewRepository.findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(anyLong(), anyLong(),
any(TimeSlot.class))).willReturn(reviewEntityList);
// when
var result = busReviewService.getBusReviewList(routeId, busRouteStationId, TimeSlot.T_00_00.getCode());
// then
verify(busReviewRepository, times(1)).findAllByRouteIdAndStationIdAndTimeSlotOrderByCreatedAtDesc(routeId,
busRouteStationId, timeSlot);
assertThat(result).isEqualTo(expected);
}
}

@Nested
@DisplayName("createBusReview 메서드 테스트")
public class CreateBusReviewTest {

@Test
void 제한된_요청에_따라_버스리뷰를_생성한다() {
void 제안된_요청에_따라_버스리뷰를_생성한다() {
//given
UserEntity user = getUserFixture(1L);
BusRouteStationEntity station = getBusRouteStationFixture(1L);
Expand Down
Loading