From 1eeb1c751855f0f4163f16f4524bb525af71f1ce Mon Sep 17 00:00:00 2001 From: seokjin8678 Date: Tue, 14 Nov 2023 13:57:03 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20refactor:=20/stages/{stageId}/tickets=20?= =?UTF-8?q?API=EC=9D=98=20N+1=EC=9D=84=20=ED=95=B4=EA=B2=B0=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.(#517)=20(#578)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: findAllByStageId fetch 조인으로 변경 * refactor: fetch join 메서드 사용하도록 변경 * chore: Inner 조건 명시하도록 변경 --- .../java/com/festago/ticket/application/TicketService.java | 2 +- .../com/festago/ticket/repository/TicketRepository.java | 7 ++++++- .../java/com/festago/application/TicketServiceTest.java | 2 +- .../test/java/com/festago/domain/TicketRepositoryTest.java | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/com/festago/ticket/application/TicketService.java b/backend/src/main/java/com/festago/ticket/application/TicketService.java index 74e72d5f4..fe9424e52 100644 --- a/backend/src/main/java/com/festago/ticket/application/TicketService.java +++ b/backend/src/main/java/com/festago/ticket/application/TicketService.java @@ -46,6 +46,6 @@ private Stage findStageById(Long stageId) { @Transactional(readOnly = true) public StageTicketsResponse findStageTickets(Long stageId) { - return StageTicketsResponse.from(ticketRepository.findAllByStageId(stageId)); + return StageTicketsResponse.from(ticketRepository.findAllByStageIdWithFetch(stageId)); } } diff --git a/backend/src/main/java/com/festago/ticket/repository/TicketRepository.java b/backend/src/main/java/com/festago/ticket/repository/TicketRepository.java index 8c5ab50af..06d15247b 100644 --- a/backend/src/main/java/com/festago/ticket/repository/TicketRepository.java +++ b/backend/src/main/java/com/festago/ticket/repository/TicketRepository.java @@ -11,7 +11,12 @@ public interface TicketRepository extends JpaRepository { - List findAllByStageId(Long stageId); + @Query(""" + SELECT t from Ticket t + INNER JOIN FETCH t.ticketAmount + WHERE t.stage.id = :stageId + """) + List findAllByStageIdWithFetch(@Param("stageId") Long stageId); Optional findByTicketTypeAndStage(TicketType ticketType, Stage stage); diff --git a/backend/src/test/java/com/festago/application/TicketServiceTest.java b/backend/src/test/java/com/festago/application/TicketServiceTest.java index cca7c198a..a6a8a1a6a 100644 --- a/backend/src/test/java/com/festago/application/TicketServiceTest.java +++ b/backend/src/test/java/com/festago/application/TicketServiceTest.java @@ -41,7 +41,7 @@ class TicketServiceTest { TicketFixture.ticket().id(1L).ticketType(TicketType.STUDENT).stage(stage).build(), TicketFixture.ticket().id(2L).ticketType(TicketType.VISITOR).stage(stage).build() ); - given(ticketRepository.findAllByStageId(stageId)) + given(ticketRepository.findAllByStageIdWithFetch(stageId)) .willReturn(tickets); // when diff --git a/backend/src/test/java/com/festago/domain/TicketRepositoryTest.java b/backend/src/test/java/com/festago/domain/TicketRepositoryTest.java index 9c9361b71..22ab05712 100644 --- a/backend/src/test/java/com/festago/domain/TicketRepositoryTest.java +++ b/backend/src/test/java/com/festago/domain/TicketRepositoryTest.java @@ -52,7 +52,7 @@ class TicketRepositoryTest { ticketRepository.save(TicketFixture.ticket().stage(otherStage).build()); // when - List actual = ticketRepository.findAllByStageId(stage.getId()); + List actual = ticketRepository.findAllByStageIdWithFetch(stage.getId()); // then assertThat(actual).hasSize(2);